blob: e780d694ed50d506169a16745b9104aa812a5911 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/StringExtras.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
24/// not valid.
25static int HexDigitValue(char C) {
26 if (C >= '0' && C <= '9') return C-'0';
27 if (C >= 'a' && C <= 'f') return C-'a'+10;
28 if (C >= 'A' && C <= 'F') return C-'A'+10;
29 return -1;
30}
31
Douglas Gregor5cee1192011-07-27 05:40:30 +000032static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
33 switch (kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000034 default: llvm_unreachable("Unknown token type!");
Douglas Gregor5cee1192011-07-27 05:40:30 +000035 case tok::char_constant:
36 case tok::string_literal:
37 case tok::utf8_string_literal:
38 return Target.getCharWidth();
39 case tok::wide_char_constant:
40 case tok::wide_string_literal:
41 return Target.getWCharWidth();
42 case tok::utf16_char_constant:
43 case tok::utf16_string_literal:
44 return Target.getChar16Width();
45 case tok::utf32_char_constant:
46 case tok::utf32_string_literal:
47 return Target.getChar32Width();
48 }
49}
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
52/// either a character or a string literal.
53static unsigned ProcessCharEscape(const char *&ThisTokBuf,
54 const char *ThisTokEnd, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +000055 FullSourceLoc Loc, unsigned CharWidth,
56 Diagnostic *Diags) {
Reid Spencer5f016e22007-07-11 17:01:13 +000057 // Skip the '\' char.
58 ++ThisTokBuf;
59
60 // We know that this character can't be off the end of the buffer, because
61 // that would have been \", which would not have been the end of string.
62 unsigned ResultChar = *ThisTokBuf++;
63 switch (ResultChar) {
64 // These map to themselves.
65 case '\\': case '\'': case '"': case '?': break;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067 // These have fixed mappings.
68 case 'a':
69 // TODO: K&R: the meaning of '\\a' is different in traditional C
70 ResultChar = 7;
71 break;
72 case 'b':
73 ResultChar = 8;
74 break;
75 case 'e':
Chris Lattner91f54ce2010-11-17 06:26:08 +000076 if (Diags)
77 Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +000078 ResultChar = 27;
79 break;
Eli Friedman3c548012009-06-10 01:32:39 +000080 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +000081 if (Diags)
82 Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +000083 ResultChar = 27;
84 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 case 'f':
86 ResultChar = 12;
87 break;
88 case 'n':
89 ResultChar = 10;
90 break;
91 case 'r':
92 ResultChar = 13;
93 break;
94 case 't':
95 ResultChar = 9;
96 break;
97 case 'v':
98 ResultChar = 11;
99 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 case 'x': { // Hex escape.
101 ResultChar = 0;
102 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000103 if (Diags)
104 Diags->Report(Loc, diag::err_hex_escape_no_digits);
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 HadError = 1;
106 break;
107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // Hex escapes are a maximal series of hex digits.
110 bool Overflow = false;
111 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
112 int CharVal = HexDigitValue(ThisTokBuf[0]);
113 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000114 // About to shift out a digit?
115 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ResultChar <<= 4;
117 ResultChar |= CharVal;
118 }
119
120 // See if any bits will be truncated when evaluated as a character.
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
122 Overflow = true;
123 ResultChar &= ~0U >> (32-CharWidth);
124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 // Check for overflow.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000127 if (Overflow && Diags) // Too many digits to fit in
128 Diags->Report(Loc, diag::warn_hex_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 break;
130 }
131 case '0': case '1': case '2': case '3':
132 case '4': case '5': case '6': case '7': {
133 // Octal escapes.
134 --ThisTokBuf;
135 ResultChar = 0;
136
137 // Octal escapes are a series of octal digits with maximum length 3.
138 // "\0123" is a two digit sequence equal to "\012" "3".
139 unsigned NumDigits = 0;
140 do {
141 ResultChar <<= 3;
142 ResultChar |= *ThisTokBuf++ - '0';
143 ++NumDigits;
144 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
145 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // Check for overflow. Reject '\777', but not L'\777'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000149 if (Diags)
150 Diags->Report(Loc, diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 ResultChar &= ~0U >> (32-CharWidth);
152 }
153 break;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 // Otherwise, these are not valid escapes.
157 case '(': case '{': case '[': case '%':
158 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000159 if (Diags)
160 Diags->Report(Loc, diag::ext_nonstandard_escape)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000161 << std::string()+(char)ResultChar;
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000162 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000164 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000165 break;
166
Ted Kremenek23ef69d2010-12-03 00:09:56 +0000167 if (isgraph(ResultChar))
Chris Lattner91f54ce2010-11-17 06:26:08 +0000168 Diags->Report(Loc, diag::ext_unknown_escape)
169 << std::string()+(char)ResultChar;
Chris Lattnerac92d822008-11-22 07:23:31 +0000170 else
Chris Lattner91f54ce2010-11-17 06:26:08 +0000171 Diags->Report(Loc, diag::ext_unknown_escape)
172 << "x"+llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 break;
174 }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 return ResultChar;
177}
178
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000179/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-10-09 00:27:47 +0000180/// return the UTF32.
181static bool ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
182 uint32_t &UcnVal, unsigned short &UcnLen,
Chris Lattner872a45e2010-11-17 06:55:10 +0000183 FullSourceLoc Loc, Diagnostic *Diags,
Chris Lattner6c66f072010-11-17 06:46:14 +0000184 const LangOptions &Features) {
185 if (!Features.CPlusPlus && !Features.C99 && Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000186 Diags->Report(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Steve Naroff4e93b342009-04-01 11:09:15 +0000188 // Save the beginning of the string (for error diagnostics).
189 const char *ThisTokBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000191 // Skip the '\u' char's.
192 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000193
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000194 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner6c66f072010-11-17 06:46:14 +0000195 if (Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000196 Diags->Report(Loc, diag::err_ucn_escape_no_digits);
Nico Weber59705ae2010-10-09 00:27:47 +0000197 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000198 }
Nico Weber59705ae2010-10-09 00:27:47 +0000199 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000200 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000201 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000202 int CharVal = HexDigitValue(ThisTokBuf[0]);
203 if (CharVal == -1) break;
204 UcnVal <<= 4;
205 UcnVal |= CharVal;
206 }
207 // If we didn't consume the proper number of digits, there is a problem.
Nico Weber59705ae2010-10-09 00:27:47 +0000208 if (UcnLenSave) {
Chris Lattner872a45e2010-11-17 06:55:10 +0000209 if (Diags) {
Chris Lattner7ef5c272010-11-17 07:05:50 +0000210 SourceLocation L =
211 Lexer::AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin,
212 Loc.getManager(), Features);
213 Diags->Report(FullSourceLoc(L, Loc.getManager()),
214 diag::err_ucn_escape_incomplete);
Chris Lattner872a45e2010-11-17 06:55:10 +0000215 }
Nico Weber59705ae2010-10-09 00:27:47 +0000216 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000219 if ((UcnVal < 0xa0 &&
220 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump1eb44332009-09-09 15:08:12 +0000221 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000222 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Chris Lattner6c66f072010-11-17 06:46:14 +0000223 if (Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000224 Diags->Report(Loc, diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000225 return false;
226 }
227 return true;
228}
229
230/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
231/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
232/// StringLiteralParser. When we decide to implement UCN's for identifiers,
233/// we will likely rework our support for UCN's.
234static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
Chris Lattnera95880d2010-11-17 07:12:42 +0000235 char *&ResultBuf, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000236 FullSourceLoc Loc, unsigned CharByteWidth,
237 Diagnostic *Diags, const LangOptions &Features) {
Nico Weber59705ae2010-10-09 00:27:47 +0000238 typedef uint32_t UTF32;
239 UTF32 UcnVal = 0;
240 unsigned short UcnLen = 0;
Chris Lattnera95880d2010-11-17 07:12:42 +0000241 if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, Loc, Diags,
242 Features)) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000243 HadError = 1;
244 return;
245 }
Nico Weber59705ae2010-10-09 00:27:47 +0000246
Douglas Gregor5cee1192011-07-27 05:40:30 +0000247 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) &&
248 "only character widths of 1, 2, or 4 bytes supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000249
Douglas Gregor5cee1192011-07-27 05:40:30 +0000250 (void)UcnLen;
251 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000252
Douglas Gregor5cee1192011-07-27 05:40:30 +0000253 if (CharByteWidth == 4) {
254 // Note: our internal rep of wide char tokens is always little-endian.
255 *ResultBuf++ = (UcnVal & 0x000000FF);
256 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
257 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
258 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
259 return;
260 }
261
262 if (CharByteWidth == 2) {
Nico Webera0f15b02010-10-06 04:57:26 +0000263 // Convert to UTF16.
264 if (UcnVal < (UTF32)0xFFFF) {
265 *ResultBuf++ = (UcnVal & 0x000000FF);
266 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
267 return;
268 }
Chris Lattnera95880d2010-11-17 07:12:42 +0000269 if (Diags) Diags->Report(Loc, diag::warn_ucn_escape_too_large);
Nico Webera0f15b02010-10-06 04:57:26 +0000270
271 typedef uint16_t UTF16;
272 UcnVal -= 0x10000;
273 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
274 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
275 *ResultBuf++ = (surrogate1 & 0x000000FF);
276 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
277 *ResultBuf++ = (surrogate2 & 0x000000FF);
278 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000279 return;
280 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000281
282 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
283
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000284 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
285 // The conversion below was inspired by:
286 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000287 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000288 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000289
290 unsigned short bytesToWrite = 0;
291 if (UcnVal < (UTF32)0x80)
292 bytesToWrite = 1;
293 else if (UcnVal < (UTF32)0x800)
294 bytesToWrite = 2;
295 else if (UcnVal < (UTF32)0x10000)
296 bytesToWrite = 3;
297 else
298 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000300 const unsigned byteMask = 0xBF;
301 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000303 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000304 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000305 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000306 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000307 };
308 // Finally, we write the bytes into ResultBuf.
309 ResultBuf += bytesToWrite;
310 switch (bytesToWrite) { // note: everything falls through.
311 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
312 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
313 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
314 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
315 }
316 // Update the buffer.
317 ResultBuf += bytesToWrite;
318}
Reid Spencer5f016e22007-07-11 17:01:13 +0000319
320
321/// integer-constant: [C99 6.4.4.1]
322/// decimal-constant integer-suffix
323/// octal-constant integer-suffix
324/// hexadecimal-constant integer-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000325/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000326/// nonzero-digit
327/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000328/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000329/// 0
330/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000331/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000332/// hexadecimal-prefix hexadecimal-digit
333/// hexadecimal-constant hexadecimal-digit
334/// hexadecimal-prefix: one of
335/// 0x 0X
336/// integer-suffix:
337/// unsigned-suffix [long-suffix]
338/// unsigned-suffix [long-long-suffix]
339/// long-suffix [unsigned-suffix]
340/// long-long-suffix [unsigned-sufix]
341/// nonzero-digit:
342/// 1 2 3 4 5 6 7 8 9
343/// octal-digit:
344/// 0 1 2 3 4 5 6 7
345/// hexadecimal-digit:
346/// 0 1 2 3 4 5 6 7 8 9
347/// a b c d e f
348/// A B C D E F
349/// unsigned-suffix: one of
350/// u U
351/// long-suffix: one of
352/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000353/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000354/// ll LL
355///
356/// floating-constant: [C99 6.4.4.2]
357/// TODO: add rules...
358///
Reid Spencer5f016e22007-07-11 17:01:13 +0000359NumericLiteralParser::
360NumericLiteralParser(const char *begin, const char *end,
361 SourceLocation TokLoc, Preprocessor &pp)
362 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000364 // This routine assumes that the range begin/end matches the regex for integer
365 // and FP constants (specifically, the 'pp-number' regex), and assumes that
366 // the byte at "*end" is both valid and not part of the regex. Because of
367 // this, it doesn't have to check for 'overscan' in various places.
368 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
369 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 s = DigitsBegin = begin;
372 saw_exponent = false;
373 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 isLong = false;
375 isUnsigned = false;
376 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000377 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000378 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000379 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000383 ParseNumberStartingWithZero(TokLoc);
384 if (hadError)
385 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 } else { // the first digit is non-zero
387 radix = 10;
388 s = SkipDigits(s);
389 if (s == ThisTokEnd) {
390 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000391 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000392 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000393 diag::err_invalid_decimal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000394 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 return;
396 } else if (*s == '.') {
397 s++;
398 saw_period = true;
399 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000400 }
Chris Lattner4411f462008-09-29 23:12:31 +0000401 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000402 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 s++;
404 saw_exponent = true;
405 if (*s == '+' || *s == '-') s++; // sign
406 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000407 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000409 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000410 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
411 diag::err_exponent_has_no_digits);
412 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000413 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 }
415 }
416 }
417
418 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner506b8de2007-08-26 01:58:14 +0000420 // Parse the suffix. At this point we can classify whether we have an FP or
421 // integer constant.
422 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner506b8de2007-08-26 01:58:14 +0000424 // Loop over all of the characters of the suffix. If we see something bad,
425 // we break out of the loop.
426 for (; s != ThisTokEnd; ++s) {
427 switch (*s) {
428 case 'f': // FP Suffix for "float"
429 case 'F':
430 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000431 if (isFloat || isLong) break; // FF, LF invalid.
432 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000433 continue; // Success.
434 case 'u':
435 case 'U':
436 if (isFPConstant) break; // Error for floating constant.
437 if (isUnsigned) break; // Cannot be repeated.
438 isUnsigned = true;
439 continue; // Success.
440 case 'l':
441 case 'L':
442 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000443 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner506b8de2007-08-26 01:58:14 +0000445 // Check for long long. The L's need to be adjacent and the same case.
446 if (s+1 != ThisTokEnd && s[1] == s[0]) {
447 if (isFPConstant) break; // long long invalid for floats.
448 isLongLong = true;
449 ++s; // Eat both of them.
450 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000453 continue; // Success.
454 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000455 case 'I':
Francois Pichet62ec1f22011-09-17 17:15:52 +0000456 if (PP.getLangOptions().MicrosoftExt) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000457 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000458
Steve Naroff0c29b222008-04-04 21:02:54 +0000459 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000460 if (s + 1 != ThisTokEnd) {
461 switch (s[1]) {
462 case '8':
463 s += 2; // i8 suffix
464 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000465 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000466 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000467 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000468 if (s[2] == '6') {
469 s += 3; // i16 suffix
470 isMicrosoftInteger = true;
471 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000472 else if (s[2] == '2') {
473 if (s + 3 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000474 if (s[3] == '8') {
475 s += 4; // i128 suffix
476 isMicrosoftInteger = true;
477 }
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000478 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000479 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000480 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000481 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000482 if (s[2] == '2') {
483 s += 3; // i32 suffix
484 isLong = true;
485 isMicrosoftInteger = true;
486 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000487 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000488 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000489 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000490 if (s[2] == '4') {
491 s += 3; // i64 suffix
492 isLongLong = true;
493 isMicrosoftInteger = true;
494 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000495 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000496 default:
497 break;
498 }
499 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000500 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000501 }
502 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000503 case 'j':
504 case 'J':
505 if (isImaginary) break; // Cannot be repeated.
506 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
507 diag::ext_imaginary_constant);
508 isImaginary = true;
509 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000511 // If we reached here, there was an error.
512 break;
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner506b8de2007-08-26 01:58:14 +0000515 // Report an error if there are any.
516 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000517 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
518 isFPConstant ? diag::err_invalid_suffix_float_constant :
519 diag::err_invalid_suffix_integer_constant)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000520 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000521 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000522 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 }
524}
525
Chris Lattner368328c2008-06-30 06:39:54 +0000526/// ParseNumberStartingWithZero - This method is called when the first character
527/// of the number is found to be a zero. This means it is either an octal
528/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000529/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000530/// radix etc.
531void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
532 assert(s[0] == '0' && "Invalid method call");
533 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner368328c2008-06-30 06:39:54 +0000535 // Handle a hex number like 0x1234.
536 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
537 s++;
538 radix = 16;
539 DigitsBegin = s;
540 s = SkipHexDigits(s);
541 if (s == ThisTokEnd) {
542 // Done.
543 } else if (*s == '.') {
544 s++;
545 saw_period = true;
546 s = SkipHexDigits(s);
547 }
548 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000549 // binary exponent is required.
Douglas Gregor1155c422011-08-30 22:40:35 +0000550 if (*s == 'p' || *s == 'P') {
Chris Lattner368328c2008-06-30 06:39:54 +0000551 const char *Exponent = s;
552 s++;
553 saw_exponent = true;
554 if (*s == '+' || *s == '-') s++; // sign
555 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000556 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000557 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
558 diag::err_exponent_has_no_digits);
559 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000560 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000561 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000562 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Sean Hunt8c723402010-01-10 23:37:56 +0000564 // In C++0x, we cannot support hexadecmial floating literals because
565 // they conflict with user-defined literals, so we warn in previous
566 // versions of C++ by default.
Douglas Gregor1155c422011-08-30 22:40:35 +0000567 if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000568 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000569 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000570 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
571 diag::err_hexconstant_requires_exponent);
572 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000573 }
574 return;
575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner368328c2008-06-30 06:39:54 +0000577 // Handle simple binary numbers 0b01010
578 if (*s == 'b' || *s == 'B') {
579 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000580 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000581 ++s;
582 radix = 2;
583 DigitsBegin = s;
584 s = SkipBinaryDigits(s);
585 if (s == ThisTokEnd) {
586 // Done.
587 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000588 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000589 diag::err_invalid_binary_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000590 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000591 }
Chris Lattner413d3552008-06-30 06:44:49 +0000592 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000593 return;
594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattner368328c2008-06-30 06:39:54 +0000596 // For now, the radix is set to 8. If we discover that we have a
597 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000598 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000599 radix = 8;
600 DigitsBegin = s;
601 s = SkipOctalDigits(s);
602 if (s == ThisTokEnd)
603 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Chris Lattner413d3552008-06-30 06:44:49 +0000605 // If we have some other non-octal digit that *is* a decimal digit, see if
606 // this is part of a floating point number like 094.123 or 09e1.
607 if (isdigit(*s)) {
608 const char *EndDecimal = SkipDigits(s);
609 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
610 s = EndDecimal;
611 radix = 10;
612 }
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattner413d3552008-06-30 06:44:49 +0000615 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
616 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000617 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000618 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000619 diag::err_invalid_octal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000620 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000621 return;
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner368328c2008-06-30 06:39:54 +0000624 if (*s == '.') {
625 s++;
626 radix = 10;
627 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000628 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000629 }
630 if (*s == 'e' || *s == 'E') { // exponent
631 const char *Exponent = s;
632 s++;
633 radix = 10;
634 saw_exponent = true;
635 if (*s == '+' || *s == '-') s++; // sign
636 const char *first_non_digit = SkipDigits(s);
637 if (first_non_digit != s) {
638 s = first_non_digit;
639 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000640 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000641 diag::err_exponent_has_no_digits);
642 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000643 return;
644 }
645 }
646}
647
648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649/// GetIntegerValue - Convert this numeric literal value to an APInt that
650/// matches Val's input width. If there is an overflow, set Val to the low bits
651/// of the result and return true. Otherwise, return false.
652bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000653 // Fast path: Compute a conservative bound on the maximum number of
654 // bits per digit in this radix. If we can't possibly overflow a
655 // uint64 based on that bound then do the simple conversion to
656 // integer. This avoids the expensive overflow checking below, and
657 // handles the common cases that matter (small decimal integers and
658 // hex/octal values which don't overflow).
659 unsigned MaxBitsPerDigit = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000660 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbara179be32008-10-16 07:32:01 +0000661 MaxBitsPerDigit += 1;
662 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
663 uint64_t N = 0;
664 for (s = DigitsBegin; s != SuffixBegin; ++s)
665 N = N*radix + HexDigitValue(*s);
666
667 // This will truncate the value to Val's input width. Simply check
668 // for overflow by comparing.
669 Val = N;
670 return Val.getZExtValue() != N;
671 }
672
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 Val = 0;
674 s = DigitsBegin;
675
676 llvm::APInt RadixVal(Val.getBitWidth(), radix);
677 llvm::APInt CharVal(Val.getBitWidth(), 0);
678 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 bool OverflowOccurred = false;
681 while (s < SuffixBegin) {
682 unsigned C = HexDigitValue(*s++);
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 // If this letter is out of bound for this radix, reject it.
685 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // Add the digit to the value in the appropriate radix. If adding in digits
690 // made the value smaller, then this overflowed.
691 OldVal = Val;
692
693 // Multiply by radix, did overflow occur on the multiply?
694 Val *= RadixVal;
695 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000698 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 OverflowOccurred |= Val.ult(CharVal);
701 }
702 return OverflowOccurred;
703}
704
John McCall94c939d2009-12-24 09:08:04 +0000705llvm::APFloat::opStatus
706NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000707 using llvm::APFloat;
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000709 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000710 return Result.convertFromString(StringRef(ThisTokBegin, n),
711 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714
Craig Topper2fa4e862011-08-11 04:06:15 +0000715/// character-literal: [C++0x lex.ccon]
716/// ' c-char-sequence '
717/// u' c-char-sequence '
718/// U' c-char-sequence '
719/// L' c-char-sequence '
720/// c-char-sequence:
721/// c-char
722/// c-char-sequence c-char
723/// c-char:
724/// any member of the source character set except the single-quote ',
725/// backslash \, or new-line character
726/// escape-sequence
727/// universal-character-name
728/// escape-sequence: [C++0x lex.ccon]
729/// simple-escape-sequence
730/// octal-escape-sequence
731/// hexadecimal-escape-sequence
732/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000733/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000734/// octal-escape-sequence:
735/// \ octal-digit
736/// \ octal-digit octal-digit
737/// \ octal-digit octal-digit octal-digit
738/// hexadecimal-escape-sequence:
739/// \x hexadecimal-digit
740/// hexadecimal-escape-sequence hexadecimal-digit
741/// universal-character-name:
742/// \u hex-quad
743/// \U hex-quad hex-quad
744/// hex-quad:
745/// hex-digit hex-digit hex-digit hex-digit
746///
Reid Spencer5f016e22007-07-11 17:01:13 +0000747CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000748 SourceLocation Loc, Preprocessor &PP,
749 tok::TokenKind kind) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // At this point we know that the character matches the regex "L?'.*'".
751 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Douglas Gregor5cee1192011-07-27 05:40:30 +0000753 Kind = kind;
754
755 // Determine if this is a wide or UTF character.
756 if (Kind == tok::wide_char_constant || Kind == tok::utf16_char_constant ||
757 Kind == tok::utf32_char_constant) {
758 ++begin;
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 // Skip over the entry quote.
762 assert(begin[0] == '\'' && "Invalid token lexed");
763 ++begin;
764
Mike Stump1eb44332009-09-09 15:08:12 +0000765 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000766 // up to 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000768 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000770 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
771 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
772 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
773 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
774 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000775
Mike Stump1eb44332009-09-09 15:08:12 +0000776 // This is what we will use for overflow detection
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000777 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnere3ad8812009-04-28 21:51:46 +0000779 unsigned NumCharsSoFar = 0;
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000780 bool Warned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 while (begin[0] != '\'') {
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000782 uint64_t ResultChar;
Nico Weber59705ae2010-10-09 00:27:47 +0000783
784 // Is this a Universal Character Name escape?
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 if (begin[0] != '\\') // If this is a normal character, consume it.
786 ResultChar = *begin++;
Nico Weber59705ae2010-10-09 00:27:47 +0000787 else { // Otherwise, this is an escape character.
Craig Topper0473cd52011-08-19 03:20:12 +0000788 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
Nico Weber59705ae2010-10-09 00:27:47 +0000789 // Check for UCN.
790 if (begin[1] == 'u' || begin[1] == 'U') {
791 uint32_t utf32 = 0;
792 unsigned short UcnLen = 0;
Chris Lattner872a45e2010-11-17 06:55:10 +0000793 if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
794 FullSourceLoc(Loc, PP.getSourceManager()),
Chris Lattner6c66f072010-11-17 06:46:14 +0000795 &PP.getDiagnostics(), PP.getLangOptions())) {
Nico Weber59705ae2010-10-09 00:27:47 +0000796 HadError = 1;
797 }
798 ResultChar = utf32;
Craig Topper0473cd52011-08-19 03:20:12 +0000799 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
800 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
801 ResultChar &= ~0U >> (32-CharWidth);
802 }
Nico Weber59705ae2010-10-09 00:27:47 +0000803 } else {
804 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000805 ResultChar = ProcessCharEscape(begin, end, HadError,
806 FullSourceLoc(Loc,PP.getSourceManager()),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000807 CharWidth, &PP.getDiagnostics());
Nico Weber59705ae2010-10-09 00:27:47 +0000808 }
809 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000810
811 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
812 // implementation defined (C99 6.4.4.4p10).
Chris Lattnere3ad8812009-04-28 21:51:46 +0000813 if (NumCharsSoFar) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000814 if (!isAscii()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000816 LitVal = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 } else {
818 // Narrow character literals act as though their value is concatenated
Chris Lattnere3ad8812009-04-28 21:51:46 +0000819 // in this implementation, but warn on overflow.
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000820 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000822 Warned = true;
823 }
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000824 LitVal <<= 8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 }
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000828 LitVal = LitVal + ResultChar;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000829 ++NumCharsSoFar;
830 }
831
832 // If this is the second character being processed, do special handling.
833 if (NumCharsSoFar > 1) {
834 // Warn about discarding the top bits for multi-char wide-character
835 // constants (L'abcd').
Douglas Gregor5cee1192011-07-27 05:40:30 +0000836 if (!isAscii())
837 PP.Diag(Loc, diag::warn_extraneous_char_constant);
Chris Lattnere3ad8812009-04-28 21:51:46 +0000838 else if (NumCharsSoFar != 4)
839 PP.Diag(Loc, diag::ext_multichar_character_literal);
840 else
841 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +0000842 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +0000843 } else
844 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000845
846 // Transfer the value from APInt to uint64_t
847 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
850 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
851 // character constants are not sign extended in the this implementation:
852 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Douglas Gregor5cee1192011-07-27 05:40:30 +0000853 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman15b91762009-06-05 07:05:05 +0000854 PP.getLangOptions().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 Value = (signed char)Value;
856}
857
858
Craig Topper2fa4e862011-08-11 04:06:15 +0000859/// string-literal: [C++0x lex.string]
860/// encoding-prefix " [s-char-sequence] "
861/// encoding-prefix R raw-string
862/// encoding-prefix:
863/// u8
864/// u
865/// U
866/// L
Reid Spencer5f016e22007-07-11 17:01:13 +0000867/// s-char-sequence:
868/// s-char
869/// s-char-sequence s-char
870/// s-char:
Craig Topper2fa4e862011-08-11 04:06:15 +0000871/// any member of the source character set except the double-quote ",
872/// backslash \, or new-line character
873/// escape-sequence
Reid Spencer5f016e22007-07-11 17:01:13 +0000874/// universal-character-name
Craig Topper2fa4e862011-08-11 04:06:15 +0000875/// raw-string:
876/// " d-char-sequence ( r-char-sequence ) d-char-sequence "
877/// r-char-sequence:
878/// r-char
879/// r-char-sequence r-char
880/// r-char:
881/// any member of the source character set, except a right parenthesis )
882/// followed by the initial d-char-sequence (which may be empty)
883/// followed by a double quote ".
884/// d-char-sequence:
885/// d-char
886/// d-char-sequence d-char
887/// d-char:
888/// any member of the basic source character set except:
889/// space, the left parenthesis (, the right parenthesis ),
890/// the backslash \, and the control characters representing horizontal
891/// tab, vertical tab, form feed, and newline.
892/// escape-sequence: [C++0x lex.ccon]
893/// simple-escape-sequence
894/// octal-escape-sequence
895/// hexadecimal-escape-sequence
896/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000897/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000898/// octal-escape-sequence:
899/// \ octal-digit
900/// \ octal-digit octal-digit
901/// \ octal-digit octal-digit octal-digit
902/// hexadecimal-escape-sequence:
903/// \x hexadecimal-digit
904/// hexadecimal-escape-sequence hexadecimal-digit
Reid Spencer5f016e22007-07-11 17:01:13 +0000905/// universal-character-name:
906/// \u hex-quad
907/// \U hex-quad hex-quad
908/// hex-quad:
909/// hex-digit hex-digit hex-digit hex-digit
910///
911StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000912StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner0833dd02010-11-17 07:21:13 +0000913 Preprocessor &PP, bool Complain)
914 : SM(PP.getSourceManager()), Features(PP.getLangOptions()),
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000915 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000916 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
917 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
Chris Lattner0833dd02010-11-17 07:21:13 +0000918 init(StringToks, NumStringToks);
919}
920
921void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000922 // The literal token may have come from an invalid source location (e.g. due
923 // to a PCH error), in which case the token length will be 0.
924 if (NumStringToks == 0 || StringToks[0].getLength() < 2) {
925 hadError = true;
926 return;
927 }
928
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 // Scan all of the string portions, remember the max individual token length,
930 // computing a bound on the concatenated string length, and see whether any
931 // piece is a wide-string. If any of the string portions is a wide-string
932 // literal, the result is a wide-string literal [C99 6.4.5p4].
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000933 assert(NumStringToks && "expected at least one token");
Sean Hunt6cf75022010-08-30 17:47:05 +0000934 MaxTokenLength = StringToks[0].getLength();
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000935 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +0000936 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Douglas Gregor5cee1192011-07-27 05:40:30 +0000937 Kind = StringToks[0].getKind();
Sean Hunt6cf75022010-08-30 17:47:05 +0000938
939 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000940
941 // Implement Translation Phase #6: concatenation of string literals
942 /// (C99 5.1.1.2p1). The common case is only one string fragment.
943 for (unsigned i = 1; i != NumStringToks; ++i) {
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000944 if (StringToks[i].getLength() < 2) {
945 hadError = true;
946 return;
947 }
948
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 // The string could be shorter than this if it needs cleaning, but this is a
950 // reasonable bound, which is all we need.
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +0000951 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +0000952 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +0000955 if (StringToks[i].getLength() > MaxTokenLength)
956 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor5cee1192011-07-27 05:40:30 +0000958 // Remember if we see any wide or utf-8/16/32 strings.
959 // Also check for illegal concatenations.
960 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
961 if (isAscii()) {
962 Kind = StringToks[i].getKind();
963 } else {
964 if (Diags)
965 Diags->Report(FullSourceLoc(StringToks[i].getLocation(), SM),
966 diag::err_unsupported_string_concat);
967 hadError = true;
968 }
969 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +0000971
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 // Include space for the null terminator.
973 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor5cee1192011-07-27 05:40:30 +0000977 // Get the width in bytes of char/wchar_t/char16_t/char32_t
978 CharByteWidth = getCharWidth(Kind, Target);
979 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
980 CharByteWidth /= 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 // The output buffer size needs to be large enough to hold wide characters.
983 // This is a worst-case assumption which basically corresponds to L"" "long".
Douglas Gregor5cee1192011-07-27 05:40:30 +0000984 SizeBound *= CharByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 // Size the temporary buffer to hold the result string data.
987 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 // Likewise, but for each string piece.
990 llvm::SmallString<512> TokenBuf;
991 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Loop over all the strings, getting their spelling, and expanding them to
994 // wide strings as appropriate.
995 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Anders Carlssonee98ac52007-10-15 02:50:23 +0000997 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
1000 const char *ThisTokBuf = &TokenBuf[0];
1001 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1002 // that ThisTokBuf points to a buffer that is big enough for the whole token
1003 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001004 bool StringInvalid = false;
Chris Lattner0833dd02010-11-17 07:21:13 +00001005 unsigned ThisTokLen =
Chris Lattnerb0607272010-11-17 07:26:20 +00001006 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1007 &StringInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +00001008 if (StringInvalid) {
Douglas Gregor5cee1192011-07-27 05:40:30 +00001009 hadError = true;
Douglas Gregor50f6af72010-03-16 05:20:39 +00001010 continue;
1011 }
1012
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Craig Topper1661d712011-08-08 06:10:39 +00001016 // Skip marker for wide or unicode strings.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001017 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 ++ThisTokBuf;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001019 // Skip 8 of u8 marker for utf8 strings.
1020 if (ThisTokBuf[0] == '8')
1021 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +00001022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Craig Topper2fa4e862011-08-11 04:06:15 +00001024 // Check for raw string
1025 if (ThisTokBuf[0] == 'R') {
1026 ThisTokBuf += 2; // skip R"
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Craig Topper2fa4e862011-08-11 04:06:15 +00001028 const char *Prefix = ThisTokBuf;
1029 while (ThisTokBuf[0] != '(')
Anders Carlssonee98ac52007-10-15 02:50:23 +00001030 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001031 ++ThisTokBuf; // skip '('
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Craig Topper2fa4e862011-08-11 04:06:15 +00001033 // remove same number of characters from the end
1034 if (ThisTokEnd >= ThisTokBuf + (ThisTokBuf - Prefix))
1035 ThisTokEnd -= (ThisTokBuf - Prefix);
1036
1037 // Copy the string over
1038 CopyStringFragment(StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf));
1039 } else {
1040 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
1041 ++ThisTokBuf; // skip "
1042
1043 // Check if this is a pascal string
1044 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1045 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1046
1047 // If the \p sequence is found in the first token, we have a pascal string
1048 // Otherwise, if we already have a pascal string, ignore the first \p
1049 if (i == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001051 Pascal = true;
1052 } else if (Pascal)
1053 ThisTokBuf += 2;
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Craig Topper2fa4e862011-08-11 04:06:15 +00001056 while (ThisTokBuf != ThisTokEnd) {
1057 // Is this a span of non-escape characters?
1058 if (ThisTokBuf[0] != '\\') {
1059 const char *InStart = ThisTokBuf;
1060 do {
1061 ++ThisTokBuf;
1062 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1063
1064 // Copy the character span over.
1065 CopyStringFragment(StringRef(InStart, ThisTokBuf - InStart));
1066 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001068 // Is this a Universal Character Name escape?
1069 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
1070 EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
1071 hadError, FullSourceLoc(StringToks[i].getLocation(),SM),
1072 CharByteWidth, Diags, Features);
1073 continue;
1074 }
1075 // Otherwise, this is a non-UCN escape character. Process it.
1076 unsigned ResultChar =
1077 ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
1078 FullSourceLoc(StringToks[i].getLocation(), SM),
1079 CharByteWidth*8, Diags);
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Craig Topper2fa4e862011-08-11 04:06:15 +00001081 // Note: our internal rep of wide char tokens is always little-endian.
1082 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Craig Topper2fa4e862011-08-11 04:06:15 +00001084 for (unsigned i = 1, e = CharByteWidth; i != e; ++i)
1085 *ResultPtr++ = ResultChar >> i*8;
1086 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 }
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001090 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +00001091 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001092 ResultBuf[0] /= CharByteWidth;
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001093
1094 // Verify that pascal strings aren't too large.
Chris Lattner0833dd02010-11-17 07:21:13 +00001095 if (GetStringLength() > 256) {
1096 if (Diags)
1097 Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1098 diag::err_pascal_string_too_long)
1099 << SourceRange(StringToks[0].getLocation(),
1100 StringToks[NumStringToks-1].getLocation());
Douglas Gregor5cee1192011-07-27 05:40:30 +00001101 hadError = true;
Eli Friedman57d7dde2009-04-01 03:17:08 +00001102 return;
1103 }
Chris Lattner0833dd02010-11-17 07:21:13 +00001104 } else if (Diags) {
Douglas Gregor427c4922010-07-20 14:33:20 +00001105 // Complain if this string literal has too many characters.
Chris Lattnera95880d2010-11-17 07:12:42 +00001106 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Douglas Gregor427c4922010-07-20 14:33:20 +00001107
1108 if (GetNumStringChars() > MaxChars)
Chris Lattner0833dd02010-11-17 07:21:13 +00001109 Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1110 diag::ext_string_too_long)
Douglas Gregor427c4922010-07-20 14:33:20 +00001111 << GetNumStringChars() << MaxChars
Chris Lattnera95880d2010-11-17 07:12:42 +00001112 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregor427c4922010-07-20 14:33:20 +00001113 << SourceRange(StringToks[0].getLocation(),
1114 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001115 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001116}
Chris Lattner719e6152009-02-18 19:21:10 +00001117
1118
Craig Topper2fa4e862011-08-11 04:06:15 +00001119/// copyStringFragment - This function copies from Start to End into ResultPtr.
1120/// Performs widening for multi-byte characters.
Craig Topper03720fc2011-08-11 05:10:55 +00001121void StringLiteralParser::CopyStringFragment(StringRef Fragment) {
Craig Topper2fa4e862011-08-11 04:06:15 +00001122 // Copy the character span over.
1123 if (CharByteWidth == 1) {
1124 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1125 ResultPtr += Fragment.size();
1126 } else {
1127 // Note: our internal rep of wide char tokens is always little-endian.
1128 for (StringRef::iterator I=Fragment.begin(), E=Fragment.end(); I!=E; ++I) {
1129 *ResultPtr++ = *I;
1130 // Add zeros at the end.
1131 for (unsigned i = 1, e = CharByteWidth; i != e; ++i)
1132 *ResultPtr++ = 0;
1133 }
1134 }
1135}
1136
1137
Chris Lattner719e6152009-02-18 19:21:10 +00001138/// getOffsetOfStringByte - This function returns the offset of the
1139/// specified byte of the string data represented by Token. This handles
1140/// advancing over escape sequences in the string.
1141unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattner6c66f072010-11-17 06:46:14 +00001142 unsigned ByteNo) const {
Chris Lattner719e6152009-02-18 19:21:10 +00001143 // Get the spelling of the token.
Chris Lattnerca1475e2010-11-17 06:35:43 +00001144 llvm::SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001145 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregor50f6af72010-03-16 05:20:39 +00001147 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001148 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerb0607272010-11-17 07:26:20 +00001149 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1150 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001151 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001152 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001153
Douglas Gregor5cee1192011-07-27 05:40:30 +00001154 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1155 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
Chris Lattner719e6152009-02-18 19:21:10 +00001156
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner719e6152009-02-18 19:21:10 +00001158 const char *SpellingStart = SpellingPtr;
1159 const char *SpellingEnd = SpellingPtr+TokLen;
1160
1161 // Skip over the leading quote.
1162 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1163 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Chris Lattner719e6152009-02-18 19:21:10 +00001165 // Skip over bytes until we find the offset we're looking for.
1166 while (ByteNo) {
1167 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Chris Lattner719e6152009-02-18 19:21:10 +00001169 // Step over non-escapes simply.
1170 if (*SpellingPtr != '\\') {
1171 ++SpellingPtr;
1172 --ByteNo;
1173 continue;
1174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Chris Lattner719e6152009-02-18 19:21:10 +00001176 // Otherwise, this is an escape character. Advance over it.
1177 bool HadError = false;
1178 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Chris Lattnerca1475e2010-11-17 06:35:43 +00001179 FullSourceLoc(Tok.getLocation(), SM),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001180 CharByteWidth*8, Diags);
Chris Lattner719e6152009-02-18 19:21:10 +00001181 assert(!HadError && "This method isn't valid on erroneous strings");
1182 --ByteNo;
1183 }
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattner719e6152009-02-18 19:21:10 +00001185 return SpellingPtr-SpellingStart;
1186}