blob: b107531e144e4af28444c45ddb656036b03bf2b4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the NumericLiteralParser, CharLiteralParser, and
11// StringLiteralParser interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/LiteralSupport.h"
16#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Lex/LexDiagnostic.h"
Chris Lattner136f93a2007-07-16 06:55:01 +000018#include "clang/Basic/TargetInfo.h"
Eli Friedmanf74a4582011-11-01 02:14:50 +000019#include "clang/Basic/ConvertUTF.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000021#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
25/// not valid.
26static int HexDigitValue(char C) {
27 if (C >= '0' && C <= '9') return C-'0';
28 if (C >= 'a' && C <= 'f') return C-'a'+10;
29 if (C >= 'A' && C <= 'F') return C-'A'+10;
30 return -1;
31}
32
Douglas Gregor5cee1192011-07-27 05:40:30 +000033static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
34 switch (kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000035 default: llvm_unreachable("Unknown token type!");
Douglas Gregor5cee1192011-07-27 05:40:30 +000036 case tok::char_constant:
37 case tok::string_literal:
38 case tok::utf8_string_literal:
39 return Target.getCharWidth();
40 case tok::wide_char_constant:
41 case tok::wide_string_literal:
42 return Target.getWCharWidth();
43 case tok::utf16_char_constant:
44 case tok::utf16_string_literal:
45 return Target.getChar16Width();
46 case tok::utf32_char_constant:
47 case tok::utf32_string_literal:
48 return Target.getChar32Width();
49 }
50}
51
Reid Spencer5f016e22007-07-11 17:01:13 +000052/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
53/// either a character or a string literal.
54static unsigned ProcessCharEscape(const char *&ThisTokBuf,
55 const char *ThisTokEnd, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +000056 FullSourceLoc Loc, unsigned CharWidth,
David Blaikied6471f72011-09-25 23:23:43 +000057 DiagnosticsEngine *Diags) {
Reid Spencer5f016e22007-07-11 17:01:13 +000058 // Skip the '\' char.
59 ++ThisTokBuf;
60
61 // We know that this character can't be off the end of the buffer, because
62 // that would have been \", which would not have been the end of string.
63 unsigned ResultChar = *ThisTokBuf++;
64 switch (ResultChar) {
65 // These map to themselves.
66 case '\\': case '\'': case '"': case '?': break;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Reid Spencer5f016e22007-07-11 17:01:13 +000068 // These have fixed mappings.
69 case 'a':
70 // TODO: K&R: the meaning of '\\a' is different in traditional C
71 ResultChar = 7;
72 break;
73 case 'b':
74 ResultChar = 8;
75 break;
76 case 'e':
Chris Lattner91f54ce2010-11-17 06:26:08 +000077 if (Diags)
78 Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +000079 ResultChar = 27;
80 break;
Eli Friedman3c548012009-06-10 01:32:39 +000081 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +000082 if (Diags)
83 Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +000084 ResultChar = 27;
85 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 case 'f':
87 ResultChar = 12;
88 break;
89 case 'n':
90 ResultChar = 10;
91 break;
92 case 'r':
93 ResultChar = 13;
94 break;
95 case 't':
96 ResultChar = 9;
97 break;
98 case 'v':
99 ResultChar = 11;
100 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 case 'x': { // Hex escape.
102 ResultChar = 0;
103 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000104 if (Diags)
105 Diags->Report(Loc, diag::err_hex_escape_no_digits);
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 HadError = 1;
107 break;
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // Hex escapes are a maximal series of hex digits.
111 bool Overflow = false;
112 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
113 int CharVal = HexDigitValue(ThisTokBuf[0]);
114 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000115 // About to shift out a digit?
116 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 ResultChar <<= 4;
118 ResultChar |= CharVal;
119 }
120
121 // See if any bits will be truncated when evaluated as a character.
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
123 Overflow = true;
124 ResultChar &= ~0U >> (32-CharWidth);
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // Check for overflow.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000128 if (Overflow && Diags) // Too many digits to fit in
129 Diags->Report(Loc, diag::warn_hex_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 break;
131 }
132 case '0': case '1': case '2': case '3':
133 case '4': case '5': case '6': case '7': {
134 // Octal escapes.
135 --ThisTokBuf;
136 ResultChar = 0;
137
138 // Octal escapes are a series of octal digits with maximum length 3.
139 // "\0123" is a two digit sequence equal to "\012" "3".
140 unsigned NumDigits = 0;
141 do {
142 ResultChar <<= 3;
143 ResultChar |= *ThisTokBuf++ - '0';
144 ++NumDigits;
145 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
146 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 // Check for overflow. Reject '\777', but not L'\777'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000150 if (Diags)
151 Diags->Report(Loc, diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ResultChar &= ~0U >> (32-CharWidth);
153 }
154 break;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // Otherwise, these are not valid escapes.
158 case '(': case '{': case '[': case '%':
159 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000160 if (Diags)
161 Diags->Report(Loc, diag::ext_nonstandard_escape)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000162 << std::string()+(char)ResultChar;
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000163 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000165 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000166 break;
167
Ted Kremenek23ef69d2010-12-03 00:09:56 +0000168 if (isgraph(ResultChar))
Chris Lattner91f54ce2010-11-17 06:26:08 +0000169 Diags->Report(Loc, diag::ext_unknown_escape)
170 << std::string()+(char)ResultChar;
Chris Lattnerac92d822008-11-22 07:23:31 +0000171 else
Chris Lattner91f54ce2010-11-17 06:26:08 +0000172 Diags->Report(Loc, diag::ext_unknown_escape)
173 << "x"+llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 break;
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 return ResultChar;
178}
179
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000180/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-10-09 00:27:47 +0000181/// return the UTF32.
182static bool ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
183 uint32_t &UcnVal, unsigned short &UcnLen,
David Blaikied6471f72011-09-25 23:23:43 +0000184 FullSourceLoc Loc, DiagnosticsEngine *Diags,
Chris Lattner6c66f072010-11-17 06:46:14 +0000185 const LangOptions &Features) {
186 if (!Features.CPlusPlus && !Features.C99 && Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000187 Diags->Report(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Steve Naroff4e93b342009-04-01 11:09:15 +0000189 // Save the beginning of the string (for error diagnostics).
190 const char *ThisTokBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000192 // Skip the '\u' char's.
193 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000194
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000195 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner6c66f072010-11-17 06:46:14 +0000196 if (Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000197 Diags->Report(Loc, diag::err_ucn_escape_no_digits);
Nico Weber59705ae2010-10-09 00:27:47 +0000198 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000199 }
Nico Weber59705ae2010-10-09 00:27:47 +0000200 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000201 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000202 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000203 int CharVal = HexDigitValue(ThisTokBuf[0]);
204 if (CharVal == -1) break;
205 UcnVal <<= 4;
206 UcnVal |= CharVal;
207 }
208 // If we didn't consume the proper number of digits, there is a problem.
Nico Weber59705ae2010-10-09 00:27:47 +0000209 if (UcnLenSave) {
Chris Lattner872a45e2010-11-17 06:55:10 +0000210 if (Diags) {
Chris Lattner7ef5c272010-11-17 07:05:50 +0000211 SourceLocation L =
212 Lexer::AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin,
213 Loc.getManager(), Features);
214 Diags->Report(FullSourceLoc(L, Loc.getManager()),
215 diag::err_ucn_escape_incomplete);
Chris Lattner872a45e2010-11-17 06:55:10 +0000216 }
Nico Weber59705ae2010-10-09 00:27:47 +0000217 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000220 if ((UcnVal < 0xa0 &&
221 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump1eb44332009-09-09 15:08:12 +0000222 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000223 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Chris Lattner6c66f072010-11-17 06:46:14 +0000224 if (Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000225 Diags->Report(Loc, diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000226 return false;
227 }
228 return true;
229}
230
231/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
232/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
233/// StringLiteralParser. When we decide to implement UCN's for identifiers,
234/// we will likely rework our support for UCN's.
235static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
Chris Lattnera95880d2010-11-17 07:12:42 +0000236 char *&ResultBuf, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000237 FullSourceLoc Loc, unsigned CharByteWidth,
David Blaikied6471f72011-09-25 23:23:43 +0000238 DiagnosticsEngine *Diags,
239 const LangOptions &Features) {
Nico Weber59705ae2010-10-09 00:27:47 +0000240 typedef uint32_t UTF32;
241 UTF32 UcnVal = 0;
242 unsigned short UcnLen = 0;
Chris Lattnera95880d2010-11-17 07:12:42 +0000243 if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, Loc, Diags,
244 Features)) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000245 HadError = 1;
246 return;
247 }
Nico Weber59705ae2010-10-09 00:27:47 +0000248
Douglas Gregor5cee1192011-07-27 05:40:30 +0000249 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) &&
250 "only character widths of 1, 2, or 4 bytes supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000251
Douglas Gregor5cee1192011-07-27 05:40:30 +0000252 (void)UcnLen;
253 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000254
Douglas Gregor5cee1192011-07-27 05:40:30 +0000255 if (CharByteWidth == 4) {
256 // Note: our internal rep of wide char tokens is always little-endian.
257 *ResultBuf++ = (UcnVal & 0x000000FF);
258 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
259 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
260 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
261 return;
262 }
263
264 if (CharByteWidth == 2) {
Nico Webera0f15b02010-10-06 04:57:26 +0000265 // Convert to UTF16.
266 if (UcnVal < (UTF32)0xFFFF) {
267 *ResultBuf++ = (UcnVal & 0x000000FF);
268 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
269 return;
270 }
Chris Lattnera95880d2010-11-17 07:12:42 +0000271 if (Diags) Diags->Report(Loc, diag::warn_ucn_escape_too_large);
Nico Webera0f15b02010-10-06 04:57:26 +0000272
273 typedef uint16_t UTF16;
274 UcnVal -= 0x10000;
275 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
276 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
277 *ResultBuf++ = (surrogate1 & 0x000000FF);
278 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
279 *ResultBuf++ = (surrogate2 & 0x000000FF);
280 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000281 return;
282 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000283
284 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
285
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000286 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
287 // The conversion below was inspired by:
288 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000289 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000290 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000291
292 unsigned short bytesToWrite = 0;
293 if (UcnVal < (UTF32)0x80)
294 bytesToWrite = 1;
295 else if (UcnVal < (UTF32)0x800)
296 bytesToWrite = 2;
297 else if (UcnVal < (UTF32)0x10000)
298 bytesToWrite = 3;
299 else
300 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000302 const unsigned byteMask = 0xBF;
303 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000305 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000306 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000307 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000308 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000309 };
310 // Finally, we write the bytes into ResultBuf.
311 ResultBuf += bytesToWrite;
312 switch (bytesToWrite) { // note: everything falls through.
313 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
314 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
315 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
316 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
317 }
318 // Update the buffer.
319 ResultBuf += bytesToWrite;
320}
Reid Spencer5f016e22007-07-11 17:01:13 +0000321
322
323/// integer-constant: [C99 6.4.4.1]
324/// decimal-constant integer-suffix
325/// octal-constant integer-suffix
326/// hexadecimal-constant integer-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000327/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000328/// nonzero-digit
329/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000330/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000331/// 0
332/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000333/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000334/// hexadecimal-prefix hexadecimal-digit
335/// hexadecimal-constant hexadecimal-digit
336/// hexadecimal-prefix: one of
337/// 0x 0X
338/// integer-suffix:
339/// unsigned-suffix [long-suffix]
340/// unsigned-suffix [long-long-suffix]
341/// long-suffix [unsigned-suffix]
342/// long-long-suffix [unsigned-sufix]
343/// nonzero-digit:
344/// 1 2 3 4 5 6 7 8 9
345/// octal-digit:
346/// 0 1 2 3 4 5 6 7
347/// hexadecimal-digit:
348/// 0 1 2 3 4 5 6 7 8 9
349/// a b c d e f
350/// A B C D E F
351/// unsigned-suffix: one of
352/// u U
353/// long-suffix: one of
354/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000355/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000356/// ll LL
357///
358/// floating-constant: [C99 6.4.4.2]
359/// TODO: add rules...
360///
Reid Spencer5f016e22007-07-11 17:01:13 +0000361NumericLiteralParser::
362NumericLiteralParser(const char *begin, const char *end,
363 SourceLocation TokLoc, Preprocessor &pp)
364 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000366 // This routine assumes that the range begin/end matches the regex for integer
367 // and FP constants (specifically, the 'pp-number' regex), and assumes that
368 // the byte at "*end" is both valid and not part of the regex. Because of
369 // this, it doesn't have to check for 'overscan' in various places.
370 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
371 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 s = DigitsBegin = begin;
374 saw_exponent = false;
375 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 isLong = false;
377 isUnsigned = false;
378 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000379 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000380 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000381 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000385 ParseNumberStartingWithZero(TokLoc);
386 if (hadError)
387 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 } else { // the first digit is non-zero
389 radix = 10;
390 s = SkipDigits(s);
391 if (s == ThisTokEnd) {
392 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000393 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000394 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000395 diag::err_invalid_decimal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000396 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 return;
398 } else if (*s == '.') {
399 s++;
400 saw_period = true;
401 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000402 }
Chris Lattner4411f462008-09-29 23:12:31 +0000403 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000404 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 s++;
406 saw_exponent = true;
407 if (*s == '+' || *s == '-') s++; // sign
408 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000409 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000411 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000412 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
413 diag::err_exponent_has_no_digits);
414 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000415 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 }
417 }
418 }
419
420 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner506b8de2007-08-26 01:58:14 +0000422 // Parse the suffix. At this point we can classify whether we have an FP or
423 // integer constant.
424 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner506b8de2007-08-26 01:58:14 +0000426 // Loop over all of the characters of the suffix. If we see something bad,
427 // we break out of the loop.
428 for (; s != ThisTokEnd; ++s) {
429 switch (*s) {
430 case 'f': // FP Suffix for "float"
431 case 'F':
432 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000433 if (isFloat || isLong) break; // FF, LF invalid.
434 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000435 continue; // Success.
436 case 'u':
437 case 'U':
438 if (isFPConstant) break; // Error for floating constant.
439 if (isUnsigned) break; // Cannot be repeated.
440 isUnsigned = true;
441 continue; // Success.
442 case 'l':
443 case 'L':
444 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000445 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner506b8de2007-08-26 01:58:14 +0000447 // Check for long long. The L's need to be adjacent and the same case.
448 if (s+1 != ThisTokEnd && s[1] == s[0]) {
449 if (isFPConstant) break; // long long invalid for floats.
450 isLongLong = true;
451 ++s; // Eat both of them.
452 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000455 continue; // Success.
456 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000457 case 'I':
Francois Pichet62ec1f22011-09-17 17:15:52 +0000458 if (PP.getLangOptions().MicrosoftExt) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000459 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000460
Steve Naroff0c29b222008-04-04 21:02:54 +0000461 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000462 if (s + 1 != ThisTokEnd) {
463 switch (s[1]) {
464 case '8':
465 s += 2; // i8 suffix
466 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000467 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000468 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000469 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000470 if (s[2] == '6') {
471 s += 3; // i16 suffix
472 isMicrosoftInteger = true;
473 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000474 else if (s[2] == '2') {
475 if (s + 3 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000476 if (s[3] == '8') {
477 s += 4; // i128 suffix
478 isMicrosoftInteger = true;
479 }
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000480 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000481 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000482 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000483 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000484 if (s[2] == '2') {
485 s += 3; // i32 suffix
486 isLong = true;
487 isMicrosoftInteger = true;
488 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000489 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000490 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000491 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000492 if (s[2] == '4') {
493 s += 3; // i64 suffix
494 isLongLong = true;
495 isMicrosoftInteger = true;
496 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000497 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000498 default:
499 break;
500 }
501 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000502 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000503 }
504 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000505 case 'j':
506 case 'J':
507 if (isImaginary) break; // Cannot be repeated.
508 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
509 diag::ext_imaginary_constant);
510 isImaginary = true;
511 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000513 // If we reached here, there was an error.
514 break;
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner506b8de2007-08-26 01:58:14 +0000517 // Report an error if there are any.
518 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000519 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
520 isFPConstant ? diag::err_invalid_suffix_float_constant :
521 diag::err_invalid_suffix_integer_constant)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000522 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000523 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000524 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
526}
527
Chris Lattner368328c2008-06-30 06:39:54 +0000528/// ParseNumberStartingWithZero - This method is called when the first character
529/// of the number is found to be a zero. This means it is either an octal
530/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000531/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000532/// radix etc.
533void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
534 assert(s[0] == '0' && "Invalid method call");
535 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattner368328c2008-06-30 06:39:54 +0000537 // Handle a hex number like 0x1234.
538 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
539 s++;
540 radix = 16;
541 DigitsBegin = s;
542 s = SkipHexDigits(s);
543 if (s == ThisTokEnd) {
544 // Done.
545 } else if (*s == '.') {
546 s++;
547 saw_period = true;
548 s = SkipHexDigits(s);
549 }
550 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000551 // binary exponent is required.
Douglas Gregor1155c422011-08-30 22:40:35 +0000552 if (*s == 'p' || *s == 'P') {
Chris Lattner368328c2008-06-30 06:39:54 +0000553 const char *Exponent = s;
554 s++;
555 saw_exponent = true;
556 if (*s == '+' || *s == '-') s++; // sign
557 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000558 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000559 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
560 diag::err_exponent_has_no_digits);
561 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000562 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000563 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000564 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregor1155c422011-08-30 22:40:35 +0000566 if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000567 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000568 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000569 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
570 diag::err_hexconstant_requires_exponent);
571 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000572 }
573 return;
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Chris Lattner368328c2008-06-30 06:39:54 +0000576 // Handle simple binary numbers 0b01010
577 if (*s == 'b' || *s == 'B') {
578 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000579 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000580 ++s;
581 radix = 2;
582 DigitsBegin = s;
583 s = SkipBinaryDigits(s);
584 if (s == ThisTokEnd) {
585 // Done.
586 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000587 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000588 diag::err_invalid_binary_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000589 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000590 }
Chris Lattner413d3552008-06-30 06:44:49 +0000591 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000592 return;
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattner368328c2008-06-30 06:39:54 +0000595 // For now, the radix is set to 8. If we discover that we have a
596 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000597 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000598 radix = 8;
599 DigitsBegin = s;
600 s = SkipOctalDigits(s);
601 if (s == ThisTokEnd)
602 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattner413d3552008-06-30 06:44:49 +0000604 // If we have some other non-octal digit that *is* a decimal digit, see if
605 // this is part of a floating point number like 094.123 or 09e1.
606 if (isdigit(*s)) {
607 const char *EndDecimal = SkipDigits(s);
608 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
609 s = EndDecimal;
610 radix = 10;
611 }
612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattner413d3552008-06-30 06:44:49 +0000614 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
615 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000616 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000617 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000618 diag::err_invalid_octal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000619 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000620 return;
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Chris Lattner368328c2008-06-30 06:39:54 +0000623 if (*s == '.') {
624 s++;
625 radix = 10;
626 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000627 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000628 }
629 if (*s == 'e' || *s == 'E') { // exponent
630 const char *Exponent = s;
631 s++;
632 radix = 10;
633 saw_exponent = true;
634 if (*s == '+' || *s == '-') s++; // sign
635 const char *first_non_digit = SkipDigits(s);
636 if (first_non_digit != s) {
637 s = first_non_digit;
638 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000639 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000640 diag::err_exponent_has_no_digits);
641 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000642 return;
643 }
644 }
645}
646
647
Reid Spencer5f016e22007-07-11 17:01:13 +0000648/// GetIntegerValue - Convert this numeric literal value to an APInt that
649/// matches Val's input width. If there is an overflow, set Val to the low bits
650/// of the result and return true. Otherwise, return false.
651bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000652 // Fast path: Compute a conservative bound on the maximum number of
653 // bits per digit in this radix. If we can't possibly overflow a
654 // uint64 based on that bound then do the simple conversion to
655 // integer. This avoids the expensive overflow checking below, and
656 // handles the common cases that matter (small decimal integers and
657 // hex/octal values which don't overflow).
658 unsigned MaxBitsPerDigit = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000659 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbara179be32008-10-16 07:32:01 +0000660 MaxBitsPerDigit += 1;
661 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
662 uint64_t N = 0;
663 for (s = DigitsBegin; s != SuffixBegin; ++s)
664 N = N*radix + HexDigitValue(*s);
665
666 // This will truncate the value to Val's input width. Simply check
667 // for overflow by comparing.
668 Val = N;
669 return Val.getZExtValue() != N;
670 }
671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 Val = 0;
673 s = DigitsBegin;
674
675 llvm::APInt RadixVal(Val.getBitWidth(), radix);
676 llvm::APInt CharVal(Val.getBitWidth(), 0);
677 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 bool OverflowOccurred = false;
680 while (s < SuffixBegin) {
681 unsigned C = HexDigitValue(*s++);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 // If this letter is out of bound for this radix, reject it.
684 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // Add the digit to the value in the appropriate radix. If adding in digits
689 // made the value smaller, then this overflowed.
690 OldVal = Val;
691
692 // Multiply by radix, did overflow occur on the multiply?
693 Val *= RadixVal;
694 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000697 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 OverflowOccurred |= Val.ult(CharVal);
700 }
701 return OverflowOccurred;
702}
703
John McCall94c939d2009-12-24 09:08:04 +0000704llvm::APFloat::opStatus
705NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000706 using llvm::APFloat;
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000708 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000709 return Result.convertFromString(StringRef(ThisTokBegin, n),
710 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000711}
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713
Craig Topper2fa4e862011-08-11 04:06:15 +0000714/// character-literal: [C++0x lex.ccon]
715/// ' c-char-sequence '
716/// u' c-char-sequence '
717/// U' c-char-sequence '
718/// L' c-char-sequence '
719/// c-char-sequence:
720/// c-char
721/// c-char-sequence c-char
722/// c-char:
723/// any member of the source character set except the single-quote ',
724/// backslash \, or new-line character
725/// escape-sequence
726/// universal-character-name
727/// escape-sequence: [C++0x lex.ccon]
728/// simple-escape-sequence
729/// octal-escape-sequence
730/// hexadecimal-escape-sequence
731/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000732/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000733/// octal-escape-sequence:
734/// \ octal-digit
735/// \ octal-digit octal-digit
736/// \ octal-digit octal-digit octal-digit
737/// hexadecimal-escape-sequence:
738/// \x hexadecimal-digit
739/// hexadecimal-escape-sequence hexadecimal-digit
740/// universal-character-name:
741/// \u hex-quad
742/// \U hex-quad hex-quad
743/// hex-quad:
744/// hex-digit hex-digit hex-digit hex-digit
745///
Reid Spencer5f016e22007-07-11 17:01:13 +0000746CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000747 SourceLocation Loc, Preprocessor &PP,
748 tok::TokenKind kind) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 // At this point we know that the character matches the regex "L?'.*'".
750 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor5cee1192011-07-27 05:40:30 +0000752 Kind = kind;
753
754 // Determine if this is a wide or UTF character.
755 if (Kind == tok::wide_char_constant || Kind == tok::utf16_char_constant ||
756 Kind == tok::utf32_char_constant) {
757 ++begin;
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 // Skip over the entry quote.
761 assert(begin[0] == '\'' && "Invalid token lexed");
762 ++begin;
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000765 // up to 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000767 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000769 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
770 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
771 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
772 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
773 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000774
Mike Stump1eb44332009-09-09 15:08:12 +0000775 // This is what we will use for overflow detection
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000776 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattnere3ad8812009-04-28 21:51:46 +0000778 unsigned NumCharsSoFar = 0;
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000779 bool Warned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 while (begin[0] != '\'') {
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000781 uint64_t ResultChar;
Nico Weber59705ae2010-10-09 00:27:47 +0000782
783 // Is this a Universal Character Name escape?
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 if (begin[0] != '\\') // If this is a normal character, consume it.
Douglas Gregor17c8c842011-09-27 17:00:18 +0000785 ResultChar = (unsigned char)*begin++;
Nico Weber59705ae2010-10-09 00:27:47 +0000786 else { // Otherwise, this is an escape character.
Craig Topper0473cd52011-08-19 03:20:12 +0000787 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
Nico Weber59705ae2010-10-09 00:27:47 +0000788 // Check for UCN.
789 if (begin[1] == 'u' || begin[1] == 'U') {
790 uint32_t utf32 = 0;
791 unsigned short UcnLen = 0;
Chris Lattner872a45e2010-11-17 06:55:10 +0000792 if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
793 FullSourceLoc(Loc, PP.getSourceManager()),
Chris Lattner6c66f072010-11-17 06:46:14 +0000794 &PP.getDiagnostics(), PP.getLangOptions())) {
Nico Weber59705ae2010-10-09 00:27:47 +0000795 HadError = 1;
796 }
797 ResultChar = utf32;
Craig Topper0473cd52011-08-19 03:20:12 +0000798 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
799 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
800 ResultChar &= ~0U >> (32-CharWidth);
801 }
Nico Weber59705ae2010-10-09 00:27:47 +0000802 } else {
803 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000804 ResultChar = ProcessCharEscape(begin, end, HadError,
805 FullSourceLoc(Loc,PP.getSourceManager()),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000806 CharWidth, &PP.getDiagnostics());
Nico Weber59705ae2010-10-09 00:27:47 +0000807 }
808 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000809
810 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
811 // implementation defined (C99 6.4.4.4p10).
Chris Lattnere3ad8812009-04-28 21:51:46 +0000812 if (NumCharsSoFar) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000813 if (!isAscii()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000815 LitVal = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 } else {
817 // Narrow character literals act as though their value is concatenated
Chris Lattnere3ad8812009-04-28 21:51:46 +0000818 // in this implementation, but warn on overflow.
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000819 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000821 Warned = true;
822 }
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000823 LitVal <<= 8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 }
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000827 LitVal = LitVal + ResultChar;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000828 ++NumCharsSoFar;
829 }
830
831 // If this is the second character being processed, do special handling.
832 if (NumCharsSoFar > 1) {
833 // Warn about discarding the top bits for multi-char wide-character
834 // constants (L'abcd').
Douglas Gregor5cee1192011-07-27 05:40:30 +0000835 if (!isAscii())
836 PP.Diag(Loc, diag::warn_extraneous_char_constant);
Chris Lattnere3ad8812009-04-28 21:51:46 +0000837 else if (NumCharsSoFar != 4)
838 PP.Diag(Loc, diag::ext_multichar_character_literal);
839 else
840 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +0000841 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +0000842 } else
843 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000844
845 // Transfer the value from APInt to uint64_t
846 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
849 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
850 // character constants are not sign extended in the this implementation:
851 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Douglas Gregor5cee1192011-07-27 05:40:30 +0000852 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman15b91762009-06-05 07:05:05 +0000853 PP.getLangOptions().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 Value = (signed char)Value;
855}
856
857
Craig Topper2fa4e862011-08-11 04:06:15 +0000858/// string-literal: [C++0x lex.string]
859/// encoding-prefix " [s-char-sequence] "
860/// encoding-prefix R raw-string
861/// encoding-prefix:
862/// u8
863/// u
864/// U
865/// L
Reid Spencer5f016e22007-07-11 17:01:13 +0000866/// s-char-sequence:
867/// s-char
868/// s-char-sequence s-char
869/// s-char:
Craig Topper2fa4e862011-08-11 04:06:15 +0000870/// any member of the source character set except the double-quote ",
871/// backslash \, or new-line character
872/// escape-sequence
Reid Spencer5f016e22007-07-11 17:01:13 +0000873/// universal-character-name
Craig Topper2fa4e862011-08-11 04:06:15 +0000874/// raw-string:
875/// " d-char-sequence ( r-char-sequence ) d-char-sequence "
876/// r-char-sequence:
877/// r-char
878/// r-char-sequence r-char
879/// r-char:
880/// any member of the source character set, except a right parenthesis )
881/// followed by the initial d-char-sequence (which may be empty)
882/// followed by a double quote ".
883/// d-char-sequence:
884/// d-char
885/// d-char-sequence d-char
886/// d-char:
887/// any member of the basic source character set except:
888/// space, the left parenthesis (, the right parenthesis ),
889/// the backslash \, and the control characters representing horizontal
890/// tab, vertical tab, form feed, and newline.
891/// escape-sequence: [C++0x lex.ccon]
892/// simple-escape-sequence
893/// octal-escape-sequence
894/// hexadecimal-escape-sequence
895/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000896/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000897/// octal-escape-sequence:
898/// \ octal-digit
899/// \ octal-digit octal-digit
900/// \ octal-digit octal-digit octal-digit
901/// hexadecimal-escape-sequence:
902/// \x hexadecimal-digit
903/// hexadecimal-escape-sequence hexadecimal-digit
Reid Spencer5f016e22007-07-11 17:01:13 +0000904/// universal-character-name:
905/// \u hex-quad
906/// \U hex-quad hex-quad
907/// hex-quad:
908/// hex-digit hex-digit hex-digit hex-digit
909///
910StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000911StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner0833dd02010-11-17 07:21:13 +0000912 Preprocessor &PP, bool Complain)
913 : SM(PP.getSourceManager()), Features(PP.getLangOptions()),
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000914 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000915 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
916 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
Chris Lattner0833dd02010-11-17 07:21:13 +0000917 init(StringToks, NumStringToks);
918}
919
920void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000921 // The literal token may have come from an invalid source location (e.g. due
922 // to a PCH error), in which case the token length will be 0.
923 if (NumStringToks == 0 || StringToks[0].getLength() < 2) {
924 hadError = true;
925 return;
926 }
927
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 // Scan all of the string portions, remember the max individual token length,
929 // computing a bound on the concatenated string length, and see whether any
930 // piece is a wide-string. If any of the string portions is a wide-string
931 // literal, the result is a wide-string literal [C99 6.4.5p4].
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000932 assert(NumStringToks && "expected at least one token");
Sean Hunt6cf75022010-08-30 17:47:05 +0000933 MaxTokenLength = StringToks[0].getLength();
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000934 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +0000935 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Douglas Gregor5cee1192011-07-27 05:40:30 +0000936 Kind = StringToks[0].getKind();
Sean Hunt6cf75022010-08-30 17:47:05 +0000937
938 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000939
940 // Implement Translation Phase #6: concatenation of string literals
941 /// (C99 5.1.1.2p1). The common case is only one string fragment.
942 for (unsigned i = 1; i != NumStringToks; ++i) {
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000943 if (StringToks[i].getLength() < 2) {
944 hadError = true;
945 return;
946 }
947
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 // The string could be shorter than this if it needs cleaning, but this is a
949 // reasonable bound, which is all we need.
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000950 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +0000951 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +0000954 if (StringToks[i].getLength() > MaxTokenLength)
955 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregor5cee1192011-07-27 05:40:30 +0000957 // Remember if we see any wide or utf-8/16/32 strings.
958 // Also check for illegal concatenations.
959 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
960 if (isAscii()) {
961 Kind = StringToks[i].getKind();
962 } else {
963 if (Diags)
964 Diags->Report(FullSourceLoc(StringToks[i].getLocation(), SM),
965 diag::err_unsupported_string_concat);
966 hadError = true;
967 }
968 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +0000970
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 // Include space for the null terminator.
972 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor5cee1192011-07-27 05:40:30 +0000976 // Get the width in bytes of char/wchar_t/char16_t/char32_t
977 CharByteWidth = getCharWidth(Kind, Target);
978 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
979 CharByteWidth /= 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 // The output buffer size needs to be large enough to hold wide characters.
982 // This is a worst-case assumption which basically corresponds to L"" "long".
Douglas Gregor5cee1192011-07-27 05:40:30 +0000983 SizeBound *= CharByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 // Size the temporary buffer to hold the result string data.
986 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 // Likewise, but for each string piece.
989 llvm::SmallString<512> TokenBuf;
990 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 // Loop over all the strings, getting their spelling, and expanding them to
993 // wide strings as appropriate.
994 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Anders Carlssonee98ac52007-10-15 02:50:23 +0000996 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
999 const char *ThisTokBuf = &TokenBuf[0];
1000 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1001 // that ThisTokBuf points to a buffer that is big enough for the whole token
1002 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001003 bool StringInvalid = false;
Chris Lattner0833dd02010-11-17 07:21:13 +00001004 unsigned ThisTokLen =
Chris Lattnerb0607272010-11-17 07:26:20 +00001005 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1006 &StringInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +00001007 if (StringInvalid) {
Douglas Gregor5cee1192011-07-27 05:40:30 +00001008 hadError = true;
Douglas Gregor50f6af72010-03-16 05:20:39 +00001009 continue;
1010 }
1011
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Craig Topper1661d712011-08-08 06:10:39 +00001015 // Skip marker for wide or unicode strings.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001016 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 ++ThisTokBuf;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001018 // Skip 8 of u8 marker for utf8 strings.
1019 if (ThisTokBuf[0] == '8')
1020 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +00001021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Craig Topper2fa4e862011-08-11 04:06:15 +00001023 // Check for raw string
1024 if (ThisTokBuf[0] == 'R') {
1025 ThisTokBuf += 2; // skip R"
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Craig Topper2fa4e862011-08-11 04:06:15 +00001027 const char *Prefix = ThisTokBuf;
1028 while (ThisTokBuf[0] != '(')
Anders Carlssonee98ac52007-10-15 02:50:23 +00001029 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001030 ++ThisTokBuf; // skip '('
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Craig Topper2fa4e862011-08-11 04:06:15 +00001032 // remove same number of characters from the end
1033 if (ThisTokEnd >= ThisTokBuf + (ThisTokBuf - Prefix))
1034 ThisTokEnd -= (ThisTokBuf - Prefix);
1035
1036 // Copy the string over
Eli Friedmanf74a4582011-11-01 02:14:50 +00001037 if (CopyStringFragment(StringRef(ThisTokBuf,ThisTokEnd-ThisTokBuf)))
1038 {
1039 if (Diags)
1040 Diags->Report(FullSourceLoc(StringToks[i].getLocation(), SM),
1041 diag::err_bad_string_encoding);
1042 hadError = true;
1043 }
1044
Craig Topper2fa4e862011-08-11 04:06:15 +00001045 } else {
1046 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
1047 ++ThisTokBuf; // skip "
1048
1049 // Check if this is a pascal string
1050 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1051 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1052
1053 // If the \p sequence is found in the first token, we have a pascal string
1054 // Otherwise, if we already have a pascal string, ignore the first \p
1055 if (i == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001057 Pascal = true;
1058 } else if (Pascal)
1059 ThisTokBuf += 2;
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Craig Topper2fa4e862011-08-11 04:06:15 +00001062 while (ThisTokBuf != ThisTokEnd) {
1063 // Is this a span of non-escape characters?
1064 if (ThisTokBuf[0] != '\\') {
1065 const char *InStart = ThisTokBuf;
1066 do {
1067 ++ThisTokBuf;
1068 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1069
1070 // Copy the character span over.
Eli Friedmanf74a4582011-11-01 02:14:50 +00001071 if (CopyStringFragment(StringRef(InStart,ThisTokBuf-InStart)))
1072 {
1073 if (Diags)
1074 Diags->Report(FullSourceLoc(StringToks[i].getLocation(), SM),
1075 diag::err_bad_string_encoding);
1076 hadError = true;
1077 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001078 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001080 // Is this a Universal Character Name escape?
1081 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
1082 EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
1083 hadError, FullSourceLoc(StringToks[i].getLocation(),SM),
1084 CharByteWidth, Diags, Features);
1085 continue;
1086 }
1087 // Otherwise, this is a non-UCN escape character. Process it.
1088 unsigned ResultChar =
1089 ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
1090 FullSourceLoc(StringToks[i].getLocation(), SM),
1091 CharByteWidth*8, Diags);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Craig Topper2fa4e862011-08-11 04:06:15 +00001093 // Note: our internal rep of wide char tokens is always little-endian.
1094 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Craig Topper2fa4e862011-08-11 04:06:15 +00001096 for (unsigned i = 1, e = CharByteWidth; i != e; ++i)
1097 *ResultPtr++ = ResultChar >> i*8;
1098 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 }
1100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001102 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +00001103 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001104 ResultBuf[0] /= CharByteWidth;
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001105
1106 // Verify that pascal strings aren't too large.
Chris Lattner0833dd02010-11-17 07:21:13 +00001107 if (GetStringLength() > 256) {
1108 if (Diags)
1109 Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1110 diag::err_pascal_string_too_long)
1111 << SourceRange(StringToks[0].getLocation(),
1112 StringToks[NumStringToks-1].getLocation());
Douglas Gregor5cee1192011-07-27 05:40:30 +00001113 hadError = true;
Eli Friedman57d7dde2009-04-01 03:17:08 +00001114 return;
1115 }
Chris Lattner0833dd02010-11-17 07:21:13 +00001116 } else if (Diags) {
Douglas Gregor427c4922010-07-20 14:33:20 +00001117 // Complain if this string literal has too many characters.
Chris Lattnera95880d2010-11-17 07:12:42 +00001118 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Douglas Gregor427c4922010-07-20 14:33:20 +00001119
1120 if (GetNumStringChars() > MaxChars)
Chris Lattner0833dd02010-11-17 07:21:13 +00001121 Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1122 diag::ext_string_too_long)
Douglas Gregor427c4922010-07-20 14:33:20 +00001123 << GetNumStringChars() << MaxChars
Chris Lattnera95880d2010-11-17 07:12:42 +00001124 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregor427c4922010-07-20 14:33:20 +00001125 << SourceRange(StringToks[0].getLocation(),
1126 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001127 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001128}
Chris Lattner719e6152009-02-18 19:21:10 +00001129
1130
Craig Topper2fa4e862011-08-11 04:06:15 +00001131/// copyStringFragment - This function copies from Start to End into ResultPtr.
1132/// Performs widening for multi-byte characters.
Eli Friedmanf74a4582011-11-01 02:14:50 +00001133bool StringLiteralParser::CopyStringFragment(StringRef Fragment) {
1134 assert(CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4);
1135 ConversionResult result = conversionOK;
Craig Topper2fa4e862011-08-11 04:06:15 +00001136 // Copy the character span over.
1137 if (CharByteWidth == 1) {
1138 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1139 ResultPtr += Fragment.size();
Eli Friedmanf74a4582011-11-01 02:14:50 +00001140 } else if (CharByteWidth == 2) {
1141 UTF8 const *sourceStart = (UTF8 const *)Fragment.data();
1142 // FIXME: Make the type of the result buffer correct instead of
1143 // using reinterpret_cast.
1144 UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
1145 ConversionFlags flags = lenientConversion;
1146 result = ConvertUTF8toUTF16(
1147 &sourceStart,sourceStart + Fragment.size(),
1148 &targetStart,targetStart + 2*Fragment.size(),flags);
1149 if (result==conversionOK)
1150 ResultPtr = reinterpret_cast<char*>(targetStart);
1151 } else if (CharByteWidth == 4) {
1152 UTF8 const *sourceStart = (UTF8 const *)Fragment.data();
1153 // FIXME: Make the type of the result buffer correct instead of
1154 // using reinterpret_cast.
1155 UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
1156 ConversionFlags flags = lenientConversion;
1157 result = ConvertUTF8toUTF32(
1158 &sourceStart,sourceStart + Fragment.size(),
1159 &targetStart,targetStart + 4*Fragment.size(),flags);
1160 if (result==conversionOK)
1161 ResultPtr = reinterpret_cast<char*>(targetStart);
Craig Topper2fa4e862011-08-11 04:06:15 +00001162 }
Eli Friedmanf74a4582011-11-01 02:14:50 +00001163 assert((result != targetExhausted)
1164 && "ConvertUTF8toUTFXX exhausted target buffer");
1165 return result != conversionOK;
Craig Topper2fa4e862011-08-11 04:06:15 +00001166}
1167
1168
Chris Lattner719e6152009-02-18 19:21:10 +00001169/// getOffsetOfStringByte - This function returns the offset of the
1170/// specified byte of the string data represented by Token. This handles
1171/// advancing over escape sequences in the string.
1172unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattner6c66f072010-11-17 06:46:14 +00001173 unsigned ByteNo) const {
Chris Lattner719e6152009-02-18 19:21:10 +00001174 // Get the spelling of the token.
Chris Lattnerca1475e2010-11-17 06:35:43 +00001175 llvm::SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001176 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor50f6af72010-03-16 05:20:39 +00001178 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001179 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerb0607272010-11-17 07:26:20 +00001180 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1181 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001182 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001183 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001184
Douglas Gregor5cee1192011-07-27 05:40:30 +00001185 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1186 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
Chris Lattner719e6152009-02-18 19:21:10 +00001187
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Chris Lattner719e6152009-02-18 19:21:10 +00001189 const char *SpellingStart = SpellingPtr;
1190 const char *SpellingEnd = SpellingPtr+TokLen;
1191
1192 // Skip over the leading quote.
1193 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1194 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Chris Lattner719e6152009-02-18 19:21:10 +00001196 // Skip over bytes until we find the offset we're looking for.
1197 while (ByteNo) {
1198 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner719e6152009-02-18 19:21:10 +00001200 // Step over non-escapes simply.
1201 if (*SpellingPtr != '\\') {
1202 ++SpellingPtr;
1203 --ByteNo;
1204 continue;
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Chris Lattner719e6152009-02-18 19:21:10 +00001207 // Otherwise, this is an escape character. Advance over it.
1208 bool HadError = false;
1209 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Chris Lattnerca1475e2010-11-17 06:35:43 +00001210 FullSourceLoc(Tok.getLocation(), SM),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001211 CharByteWidth*8, Diags);
Chris Lattner719e6152009-02-18 19:21:10 +00001212 assert(!HadError && "This method isn't valid on erroneous strings");
1213 --ByteNo;
1214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Chris Lattner719e6152009-02-18 19:21:10 +00001216 return SpellingPtr-SpellingStart;
1217}