blob: 91edf4f4a0494f09c47f1855336eb1e91abccebb [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
Seth Cantrell5bffbe52012-10-28 18:24:46 +000052static CharSourceRange MakeCharSourceRange(const LangOptions &Features,
53 FullSourceLoc TokLoc,
54 const char *TokBegin,
55 const char *TokRangeBegin,
56 const char *TokRangeEnd) {
57 SourceLocation Begin =
58 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
59 TokLoc.getManager(), Features);
60 SourceLocation End =
61 Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin,
62 TokLoc.getManager(), Features);
63 return CharSourceRange::getCharRange(Begin, End);
64}
65
Richard Smithe5f05882012-09-08 07:16:20 +000066/// \brief Produce a diagnostic highlighting some portion of a literal.
67///
68/// Emits the diagnostic \p DiagID, highlighting the range of characters from
69/// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
70/// a substring of a spelling buffer for the token beginning at \p TokBegin.
71static DiagnosticBuilder Diag(DiagnosticsEngine *Diags,
72 const LangOptions &Features, FullSourceLoc TokLoc,
73 const char *TokBegin, const char *TokRangeBegin,
74 const char *TokRangeEnd, unsigned DiagID) {
75 SourceLocation Begin =
76 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
77 TokLoc.getManager(), Features);
Seth Cantrell5bffbe52012-10-28 18:24:46 +000078 return Diags->Report(Begin, DiagID) <<
79 MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd);
Richard Smithe5f05882012-09-08 07:16:20 +000080}
81
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
83/// either a character or a string literal.
Richard Smithe5f05882012-09-08 07:16:20 +000084static unsigned ProcessCharEscape(const char *ThisTokBegin,
85 const char *&ThisTokBuf,
Reid Spencer5f016e22007-07-11 17:01:13 +000086 const char *ThisTokEnd, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +000087 FullSourceLoc Loc, unsigned CharWidth,
Richard Smithe5f05882012-09-08 07:16:20 +000088 DiagnosticsEngine *Diags,
89 const LangOptions &Features) {
90 const char *EscapeBegin = ThisTokBuf;
91
Reid Spencer5f016e22007-07-11 17:01:13 +000092 // Skip the '\' char.
93 ++ThisTokBuf;
94
95 // We know that this character can't be off the end of the buffer, because
96 // that would have been \", which would not have been the end of string.
97 unsigned ResultChar = *ThisTokBuf++;
98 switch (ResultChar) {
99 // These map to themselves.
100 case '\\': case '\'': case '"': case '?': break;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // These have fixed mappings.
103 case 'a':
104 // TODO: K&R: the meaning of '\\a' is different in traditional C
105 ResultChar = 7;
106 break;
107 case 'b':
108 ResultChar = 8;
109 break;
110 case 'e':
Chris Lattner91f54ce2010-11-17 06:26:08 +0000111 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000112 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
113 diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ResultChar = 27;
115 break;
Eli Friedman3c548012009-06-10 01:32:39 +0000116 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +0000117 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000118 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
119 diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +0000120 ResultChar = 27;
121 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 case 'f':
123 ResultChar = 12;
124 break;
125 case 'n':
126 ResultChar = 10;
127 break;
128 case 'r':
129 ResultChar = 13;
130 break;
131 case 't':
132 ResultChar = 9;
133 break;
134 case 'v':
135 ResultChar = 11;
136 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 case 'x': { // Hex escape.
138 ResultChar = 0;
139 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000140 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000141 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
142 diag::err_hex_escape_no_digits);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 HadError = 1;
144 break;
145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // Hex escapes are a maximal series of hex digits.
148 bool Overflow = false;
149 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
150 int CharVal = HexDigitValue(ThisTokBuf[0]);
151 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000152 // About to shift out a digit?
153 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 ResultChar <<= 4;
155 ResultChar |= CharVal;
156 }
157
158 // See if any bits will be truncated when evaluated as a character.
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
160 Overflow = true;
161 ResultChar &= ~0U >> (32-CharWidth);
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // Check for overflow.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000165 if (Overflow && Diags) // Too many digits to fit in
Richard Smithe5f05882012-09-08 07:16:20 +0000166 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
167 diag::warn_hex_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 break;
169 }
170 case '0': case '1': case '2': case '3':
171 case '4': case '5': case '6': case '7': {
172 // Octal escapes.
173 --ThisTokBuf;
174 ResultChar = 0;
175
176 // Octal escapes are a series of octal digits with maximum length 3.
177 // "\0123" is a two digit sequence equal to "\012" "3".
178 unsigned NumDigits = 0;
179 do {
180 ResultChar <<= 3;
181 ResultChar |= *ThisTokBuf++ - '0';
182 ++NumDigits;
183 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
184 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // Check for overflow. Reject '\777', but not L'\777'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000188 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000189 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
190 diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 ResultChar &= ~0U >> (32-CharWidth);
192 }
193 break;
194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 // Otherwise, these are not valid escapes.
197 case '(': case '{': case '[': case '%':
198 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000199 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000200 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
201 diag::ext_nonstandard_escape)
202 << std::string(1, ResultChar);
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000203 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000205 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000206 break;
Richard Smithe5f05882012-09-08 07:16:20 +0000207
Ted Kremenek23ef69d2010-12-03 00:09:56 +0000208 if (isgraph(ResultChar))
Richard Smithe5f05882012-09-08 07:16:20 +0000209 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
210 diag::ext_unknown_escape)
211 << std::string(1, ResultChar);
Chris Lattnerac92d822008-11-22 07:23:31 +0000212 else
Richard Smithe5f05882012-09-08 07:16:20 +0000213 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
214 diag::ext_unknown_escape)
215 << "x" + llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 break;
217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 return ResultChar;
220}
221
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000222/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-10-09 00:27:47 +0000223/// return the UTF32.
Richard Smith26b75c02012-03-09 22:27:51 +0000224static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
225 const char *ThisTokEnd,
Nico Weber59705ae2010-10-09 00:27:47 +0000226 uint32_t &UcnVal, unsigned short &UcnLen,
David Blaikied6471f72011-09-25 23:23:43 +0000227 FullSourceLoc Loc, DiagnosticsEngine *Diags,
Seth Cantrellbe773522012-01-18 12:27:04 +0000228 const LangOptions &Features,
229 bool in_char_string_literal = false) {
Richard Smith26b75c02012-03-09 22:27:51 +0000230 const char *UcnBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000232 // Skip the '\u' char's.
233 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000235 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner6c66f072010-11-17 06:46:14 +0000236 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000237 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
238 diag::err_ucn_escape_no_digits);
Nico Weber59705ae2010-10-09 00:27:47 +0000239 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000240 }
Nico Weber59705ae2010-10-09 00:27:47 +0000241 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000242 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000243 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000244 int CharVal = HexDigitValue(ThisTokBuf[0]);
245 if (CharVal == -1) break;
246 UcnVal <<= 4;
247 UcnVal |= CharVal;
248 }
249 // If we didn't consume the proper number of digits, there is a problem.
Nico Weber59705ae2010-10-09 00:27:47 +0000250 if (UcnLenSave) {
Richard Smithe5f05882012-09-08 07:16:20 +0000251 if (Diags)
252 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
253 diag::err_ucn_escape_incomplete);
Nico Weber59705ae2010-10-09 00:27:47 +0000254 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000255 }
Richard Smith26b75c02012-03-09 22:27:51 +0000256
Seth Cantrellbe773522012-01-18 12:27:04 +0000257 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
Richard Smith26b75c02012-03-09 22:27:51 +0000258 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints
259 UcnVal > 0x10FFFF) { // maximum legal UTF32 value
Chris Lattner6c66f072010-11-17 06:46:14 +0000260 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000261 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
262 diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000263 return false;
264 }
Richard Smith26b75c02012-03-09 22:27:51 +0000265
266 // C++11 allows UCNs that refer to control characters and basic source
267 // characters inside character and string literals
268 if (UcnVal < 0xa0 &&
269 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, `
270 bool IsError = (!Features.CPlusPlus0x || !in_char_string_literal);
271 if (Diags) {
Richard Smith26b75c02012-03-09 22:27:51 +0000272 char BasicSCSChar = UcnVal;
273 if (UcnVal >= 0x20 && UcnVal < 0x7f)
Richard Smithe5f05882012-09-08 07:16:20 +0000274 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
275 IsError ? diag::err_ucn_escape_basic_scs :
276 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
277 << StringRef(&BasicSCSChar, 1);
Richard Smith26b75c02012-03-09 22:27:51 +0000278 else
Richard Smithe5f05882012-09-08 07:16:20 +0000279 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
280 IsError ? diag::err_ucn_control_character :
281 diag::warn_cxx98_compat_literal_ucn_control_character);
Richard Smith26b75c02012-03-09 22:27:51 +0000282 }
283 if (IsError)
284 return false;
285 }
286
Richard Smithe5f05882012-09-08 07:16:20 +0000287 if (!Features.CPlusPlus && !Features.C99 && Diags)
288 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
289 diag::warn_ucn_not_valid_in_c89);
290
Nico Weber59705ae2010-10-09 00:27:47 +0000291 return true;
292}
293
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000294/// MeasureUCNEscape - Determine the number of bytes within the resulting string
295/// which this UCN will occupy.
296static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
297 const char *ThisTokEnd, unsigned CharByteWidth,
298 const LangOptions &Features, bool &HadError) {
299 // UTF-32: 4 bytes per escape.
300 if (CharByteWidth == 4)
301 return 4;
302
303 uint32_t UcnVal = 0;
304 unsigned short UcnLen = 0;
305 FullSourceLoc Loc;
306
307 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
308 UcnLen, Loc, 0, Features, true)) {
309 HadError = true;
310 return 0;
311 }
312
313 // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
314 if (CharByteWidth == 2)
315 return UcnVal <= 0xFFFF ? 2 : 4;
316
317 // UTF-8.
318 if (UcnVal < 0x80)
319 return 1;
320 if (UcnVal < 0x800)
321 return 2;
322 if (UcnVal < 0x10000)
323 return 3;
324 return 4;
325}
326
Nico Weber59705ae2010-10-09 00:27:47 +0000327/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
328/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
329/// StringLiteralParser. When we decide to implement UCN's for identifiers,
330/// we will likely rework our support for UCN's.
Richard Smith26b75c02012-03-09 22:27:51 +0000331static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
332 const char *ThisTokEnd,
Chris Lattnera95880d2010-11-17 07:12:42 +0000333 char *&ResultBuf, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000334 FullSourceLoc Loc, unsigned CharByteWidth,
David Blaikied6471f72011-09-25 23:23:43 +0000335 DiagnosticsEngine *Diags,
336 const LangOptions &Features) {
Nico Weber59705ae2010-10-09 00:27:47 +0000337 typedef uint32_t UTF32;
338 UTF32 UcnVal = 0;
339 unsigned short UcnLen = 0;
Richard Smith26b75c02012-03-09 22:27:51 +0000340 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
341 Loc, Diags, Features, true)) {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000342 HadError = true;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000343 return;
344 }
Nico Weber59705ae2010-10-09 00:27:47 +0000345
Douglas Gregor5cee1192011-07-27 05:40:30 +0000346 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) &&
347 "only character widths of 1, 2, or 4 bytes supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000348
Douglas Gregor5cee1192011-07-27 05:40:30 +0000349 (void)UcnLen;
350 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000351
Douglas Gregor5cee1192011-07-27 05:40:30 +0000352 if (CharByteWidth == 4) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000353 // FIXME: Make the type of the result buffer correct instead of
354 // using reinterpret_cast.
355 UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf);
356 *ResultPtr = UcnVal;
357 ResultBuf += 4;
Douglas Gregor5cee1192011-07-27 05:40:30 +0000358 return;
359 }
360
361 if (CharByteWidth == 2) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000362 // FIXME: Make the type of the result buffer correct instead of
363 // using reinterpret_cast.
364 UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf);
365
Richard Smith59b26d82012-06-13 05:41:29 +0000366 if (UcnVal <= (UTF32)0xFFFF) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000367 *ResultPtr = UcnVal;
368 ResultBuf += 2;
Nico Webera0f15b02010-10-06 04:57:26 +0000369 return;
370 }
Nico Webera0f15b02010-10-06 04:57:26 +0000371
Eli Friedmancaf1f262011-11-02 23:06:23 +0000372 // Convert to UTF16.
Nico Webera0f15b02010-10-06 04:57:26 +0000373 UcnVal -= 0x10000;
Eli Friedmancaf1f262011-11-02 23:06:23 +0000374 *ResultPtr = 0xD800 + (UcnVal >> 10);
375 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
376 ResultBuf += 4;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000377 return;
378 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000379
380 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
381
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000382 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
383 // The conversion below was inspired by:
384 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000385 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000386 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000387
388 unsigned short bytesToWrite = 0;
389 if (UcnVal < (UTF32)0x80)
390 bytesToWrite = 1;
391 else if (UcnVal < (UTF32)0x800)
392 bytesToWrite = 2;
393 else if (UcnVal < (UTF32)0x10000)
394 bytesToWrite = 3;
395 else
396 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000398 const unsigned byteMask = 0xBF;
399 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000401 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000402 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000403 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000404 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000405 };
406 // Finally, we write the bytes into ResultBuf.
407 ResultBuf += bytesToWrite;
408 switch (bytesToWrite) { // note: everything falls through.
409 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
410 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
411 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
412 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
413 }
414 // Update the buffer.
415 ResultBuf += bytesToWrite;
416}
Reid Spencer5f016e22007-07-11 17:01:13 +0000417
418
419/// integer-constant: [C99 6.4.4.1]
420/// decimal-constant integer-suffix
421/// octal-constant integer-suffix
422/// hexadecimal-constant integer-suffix
Richard Smith49d51742012-03-08 21:59:28 +0000423/// user-defined-integer-literal: [C++11 lex.ext]
Richard Smithb453ad32012-03-08 08:45:32 +0000424/// decimal-literal ud-suffix
425/// octal-literal ud-suffix
426/// hexadecimal-literal ud-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000427/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000428/// nonzero-digit
429/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000430/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000431/// 0
432/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000433/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000434/// hexadecimal-prefix hexadecimal-digit
435/// hexadecimal-constant hexadecimal-digit
436/// hexadecimal-prefix: one of
437/// 0x 0X
438/// integer-suffix:
439/// unsigned-suffix [long-suffix]
440/// unsigned-suffix [long-long-suffix]
441/// long-suffix [unsigned-suffix]
442/// long-long-suffix [unsigned-sufix]
443/// nonzero-digit:
444/// 1 2 3 4 5 6 7 8 9
445/// octal-digit:
446/// 0 1 2 3 4 5 6 7
447/// hexadecimal-digit:
448/// 0 1 2 3 4 5 6 7 8 9
449/// a b c d e f
450/// A B C D E F
451/// unsigned-suffix: one of
452/// u U
453/// long-suffix: one of
454/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000455/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000456/// ll LL
457///
458/// floating-constant: [C99 6.4.4.2]
459/// TODO: add rules...
460///
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000461NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
462 SourceLocation TokLoc,
463 Preprocessor &PP)
464 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000466 // This routine assumes that the range begin/end matches the regex for integer
467 // and FP constants (specifically, the 'pp-number' regex), and assumes that
468 // the byte at "*end" is both valid and not part of the regex. Because of
469 // this, it doesn't have to check for 'overscan' in various places.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000470 assert(!isalnum(*ThisTokEnd) && *ThisTokEnd != '.' && *ThisTokEnd != '_' &&
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000471 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000473 s = DigitsBegin = ThisTokBegin;
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 saw_exponent = false;
475 saw_period = false;
Richard Smithb453ad32012-03-08 08:45:32 +0000476 saw_ud_suffix = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 isLong = false;
478 isUnsigned = false;
479 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000480 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000481 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000482 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000486 ParseNumberStartingWithZero(TokLoc);
487 if (hadError)
488 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 } else { // the first digit is non-zero
490 radix = 10;
491 s = SkipDigits(s);
492 if (s == ThisTokEnd) {
493 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000494 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000495 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000496 diag::err_invalid_decimal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000497 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 return;
499 } else if (*s == '.') {
500 s++;
501 saw_period = true;
502 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000503 }
Chris Lattner4411f462008-09-29 23:12:31 +0000504 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000505 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 s++;
507 saw_exponent = true;
508 if (*s == '+' || *s == '-') s++; // sign
509 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000510 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000512 } else {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000513 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000514 diag::err_exponent_has_no_digits);
515 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000516 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 }
518 }
519 }
520
521 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattner506b8de2007-08-26 01:58:14 +0000523 // Parse the suffix. At this point we can classify whether we have an FP or
524 // integer constant.
525 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattner506b8de2007-08-26 01:58:14 +0000527 // Loop over all of the characters of the suffix. If we see something bad,
528 // we break out of the loop.
529 for (; s != ThisTokEnd; ++s) {
530 switch (*s) {
531 case 'f': // FP Suffix for "float"
532 case 'F':
533 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000534 if (isFloat || isLong) break; // FF, LF invalid.
535 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000536 continue; // Success.
537 case 'u':
538 case 'U':
539 if (isFPConstant) break; // Error for floating constant.
540 if (isUnsigned) break; // Cannot be repeated.
541 isUnsigned = true;
542 continue; // Success.
543 case 'l':
544 case 'L':
545 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000546 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner506b8de2007-08-26 01:58:14 +0000548 // Check for long long. The L's need to be adjacent and the same case.
549 if (s+1 != ThisTokEnd && s[1] == s[0]) {
550 if (isFPConstant) break; // long long invalid for floats.
551 isLongLong = true;
552 ++s; // Eat both of them.
553 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000556 continue; // Success.
557 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000558 case 'I':
David Blaikie4e4d0842012-03-11 07:00:24 +0000559 if (PP.getLangOpts().MicrosoftExt) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000560 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000561
Steve Naroff0c29b222008-04-04 21:02:54 +0000562 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000563 if (s + 1 != ThisTokEnd) {
564 switch (s[1]) {
565 case '8':
566 s += 2; // i8 suffix
567 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000568 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000569 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000570 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000571 if (s[2] == '6') {
572 s += 3; // i16 suffix
573 isMicrosoftInteger = true;
574 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000575 else if (s[2] == '2') {
576 if (s + 3 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000577 if (s[3] == '8') {
578 s += 4; // i128 suffix
579 isMicrosoftInteger = true;
580 }
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000581 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000582 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000583 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000584 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000585 if (s[2] == '2') {
586 s += 3; // i32 suffix
587 isLong = true;
588 isMicrosoftInteger = true;
589 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000590 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000591 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000592 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000593 if (s[2] == '4') {
594 s += 3; // i64 suffix
595 isLongLong = true;
596 isMicrosoftInteger = true;
597 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000598 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000599 default:
600 break;
601 }
602 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000603 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000604 }
605 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000606 case 'j':
607 case 'J':
608 if (isImaginary) break; // Cannot be repeated.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000609 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Chris Lattner506b8de2007-08-26 01:58:14 +0000610 diag::ext_imaginary_constant);
611 isImaginary = true;
612 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 }
Richard Smithb453ad32012-03-08 08:45:32 +0000614 // If we reached here, there was an error or a ud-suffix.
Chris Lattner506b8de2007-08-26 01:58:14 +0000615 break;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner506b8de2007-08-26 01:58:14 +0000618 if (s != ThisTokEnd) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000619 if (PP.getLangOpts().CPlusPlus0x && s == SuffixBegin && *s == '_') {
Richard Smithb453ad32012-03-08 08:45:32 +0000620 // We have a ud-suffix! By C++11 [lex.ext]p10, ud-suffixes not starting
621 // with an '_' are ill-formed.
622 saw_ud_suffix = true;
623 return;
624 }
625
626 // Report an error if there are any.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000627 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000628 isFPConstant ? diag::err_invalid_suffix_float_constant :
629 diag::err_invalid_suffix_integer_constant)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000630 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000631 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000632 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 }
634}
635
Chris Lattner368328c2008-06-30 06:39:54 +0000636/// ParseNumberStartingWithZero - This method is called when the first character
637/// of the number is found to be a zero. This means it is either an octal
638/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000639/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000640/// radix etc.
641void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
642 assert(s[0] == '0' && "Invalid method call");
643 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Chris Lattner368328c2008-06-30 06:39:54 +0000645 // Handle a hex number like 0x1234.
646 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
647 s++;
648 radix = 16;
649 DigitsBegin = s;
650 s = SkipHexDigits(s);
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000651 bool noSignificand = (s == DigitsBegin);
Chris Lattner368328c2008-06-30 06:39:54 +0000652 if (s == ThisTokEnd) {
653 // Done.
654 } else if (*s == '.') {
655 s++;
656 saw_period = true;
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000657 const char *floatDigitsBegin = s;
Chris Lattner368328c2008-06-30 06:39:54 +0000658 s = SkipHexDigits(s);
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000659 noSignificand &= (floatDigitsBegin == s);
Chris Lattner368328c2008-06-30 06:39:54 +0000660 }
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000661
662 if (noSignificand) {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000663 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000664 diag::err_hexconstant_requires_digits);
665 hadError = true;
666 return;
667 }
668
Chris Lattner368328c2008-06-30 06:39:54 +0000669 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000670 // binary exponent is required.
Douglas Gregor1155c422011-08-30 22:40:35 +0000671 if (*s == 'p' || *s == 'P') {
Chris Lattner368328c2008-06-30 06:39:54 +0000672 const char *Exponent = s;
673 s++;
674 saw_exponent = true;
675 if (*s == '+' || *s == '-') s++; // sign
676 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000677 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000678 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
679 diag::err_exponent_has_no_digits);
680 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000681 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000682 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000683 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
David Blaikie4e4d0842012-03-11 07:00:24 +0000685 if (!PP.getLangOpts().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000686 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000687 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000688 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
689 diag::err_hexconstant_requires_exponent);
690 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000691 }
692 return;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattner368328c2008-06-30 06:39:54 +0000695 // Handle simple binary numbers 0b01010
696 if (*s == 'b' || *s == 'B') {
697 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000698 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000699 ++s;
700 radix = 2;
701 DigitsBegin = s;
702 s = SkipBinaryDigits(s);
703 if (s == ThisTokEnd) {
704 // Done.
705 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000706 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000707 diag::err_invalid_binary_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000708 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000709 }
Chris Lattner413d3552008-06-30 06:44:49 +0000710 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000711 return;
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattner368328c2008-06-30 06:39:54 +0000714 // For now, the radix is set to 8. If we discover that we have a
715 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000716 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000717 radix = 8;
718 DigitsBegin = s;
719 s = SkipOctalDigits(s);
720 if (s == ThisTokEnd)
721 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattner413d3552008-06-30 06:44:49 +0000723 // If we have some other non-octal digit that *is* a decimal digit, see if
724 // this is part of a floating point number like 094.123 or 09e1.
725 if (isdigit(*s)) {
726 const char *EndDecimal = SkipDigits(s);
727 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
728 s = EndDecimal;
729 radix = 10;
730 }
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattner413d3552008-06-30 06:44:49 +0000733 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
734 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000735 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000736 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000737 diag::err_invalid_octal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000738 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000739 return;
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Chris Lattner368328c2008-06-30 06:39:54 +0000742 if (*s == '.') {
743 s++;
744 radix = 10;
745 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000746 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000747 }
748 if (*s == 'e' || *s == 'E') { // exponent
749 const char *Exponent = s;
750 s++;
751 radix = 10;
752 saw_exponent = true;
753 if (*s == '+' || *s == '-') s++; // sign
754 const char *first_non_digit = SkipDigits(s);
755 if (first_non_digit != s) {
756 s = first_non_digit;
757 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000758 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000759 diag::err_exponent_has_no_digits);
760 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000761 return;
762 }
763 }
764}
765
Jordan Rose2fd69562012-09-25 22:32:51 +0000766static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) {
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000767 switch (Radix) {
768 case 2:
769 return NumDigits <= 64;
770 case 8:
771 return NumDigits <= 64 / 3; // Digits are groups of 3 bits.
772 case 10:
773 return NumDigits <= 19; // floor(log10(2^64))
774 case 16:
775 return NumDigits <= 64 / 4; // Digits are groups of 4 bits.
776 default:
777 llvm_unreachable("impossible Radix");
778 }
779}
Chris Lattner368328c2008-06-30 06:39:54 +0000780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781/// GetIntegerValue - Convert this numeric literal value to an APInt that
782/// matches Val's input width. If there is an overflow, set Val to the low bits
783/// of the result and return true. Otherwise, return false.
784bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000785 // Fast path: Compute a conservative bound on the maximum number of
786 // bits per digit in this radix. If we can't possibly overflow a
787 // uint64 based on that bound then do the simple conversion to
788 // integer. This avoids the expensive overflow checking below, and
789 // handles the common cases that matter (small decimal integers and
790 // hex/octal values which don't overflow).
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000791 const unsigned NumDigits = SuffixBegin - DigitsBegin;
Jordan Rose2fd69562012-09-25 22:32:51 +0000792 if (alwaysFitsInto64Bits(radix, NumDigits)) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000793 uint64_t N = 0;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000794 for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
795 N = N * radix + HexDigitValue(*Ptr);
Daniel Dunbara179be32008-10-16 07:32:01 +0000796
797 // This will truncate the value to Val's input width. Simply check
798 // for overflow by comparing.
799 Val = N;
800 return Val.getZExtValue() != N;
801 }
802
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 Val = 0;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000804 const char *Ptr = DigitsBegin;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805
806 llvm::APInt RadixVal(Val.getBitWidth(), radix);
807 llvm::APInt CharVal(Val.getBitWidth(), 0);
808 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 bool OverflowOccurred = false;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000811 while (Ptr < SuffixBegin) {
812 unsigned C = HexDigitValue(*Ptr++);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 // If this letter is out of bound for this radix, reject it.
815 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 // Add the digit to the value in the appropriate radix. If adding in digits
820 // made the value smaller, then this overflowed.
821 OldVal = Val;
822
823 // Multiply by radix, did overflow occur on the multiply?
824 Val *= RadixVal;
825 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
826
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000828 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 OverflowOccurred |= Val.ult(CharVal);
831 }
832 return OverflowOccurred;
833}
834
John McCall94c939d2009-12-24 09:08:04 +0000835llvm::APFloat::opStatus
836NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000837 using llvm::APFloat;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000839 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000840 return Result.convertFromString(StringRef(ThisTokBegin, n),
841 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000842}
843
Reid Spencer5f016e22007-07-11 17:01:13 +0000844
James Dennett58f9ce12012-06-17 03:34:42 +0000845/// \verbatim
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000846/// user-defined-character-literal: [C++11 lex.ext]
847/// character-literal ud-suffix
848/// ud-suffix:
849/// identifier
850/// character-literal: [C++11 lex.ccon]
Craig Topper2fa4e862011-08-11 04:06:15 +0000851/// ' c-char-sequence '
852/// u' c-char-sequence '
853/// U' c-char-sequence '
854/// L' c-char-sequence '
855/// c-char-sequence:
856/// c-char
857/// c-char-sequence c-char
858/// c-char:
859/// any member of the source character set except the single-quote ',
860/// backslash \, or new-line character
861/// escape-sequence
862/// universal-character-name
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000863/// escape-sequence:
Craig Topper2fa4e862011-08-11 04:06:15 +0000864/// simple-escape-sequence
865/// octal-escape-sequence
866/// hexadecimal-escape-sequence
867/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000868/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000869/// octal-escape-sequence:
870/// \ octal-digit
871/// \ octal-digit octal-digit
872/// \ octal-digit octal-digit octal-digit
873/// hexadecimal-escape-sequence:
874/// \x hexadecimal-digit
875/// hexadecimal-escape-sequence hexadecimal-digit
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000876/// universal-character-name: [C++11 lex.charset]
Craig Topper2fa4e862011-08-11 04:06:15 +0000877/// \u hex-quad
878/// \U hex-quad hex-quad
879/// hex-quad:
880/// hex-digit hex-digit hex-digit hex-digit
James Dennett58f9ce12012-06-17 03:34:42 +0000881/// \endverbatim
Craig Topper2fa4e862011-08-11 04:06:15 +0000882///
Reid Spencer5f016e22007-07-11 17:01:13 +0000883CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000884 SourceLocation Loc, Preprocessor &PP,
885 tok::TokenKind kind) {
Seth Cantrellbe773522012-01-18 12:27:04 +0000886 // At this point we know that the character matches the regex "(L|u|U)?'.*'".
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Douglas Gregor5cee1192011-07-27 05:40:30 +0000889 Kind = kind;
890
Richard Smith26b75c02012-03-09 22:27:51 +0000891 const char *TokBegin = begin;
892
Seth Cantrellbe773522012-01-18 12:27:04 +0000893 // Skip over wide character determinant.
894 if (Kind != tok::char_constant) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000895 ++begin;
896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // Skip over the entry quote.
899 assert(begin[0] == '\'' && "Invalid token lexed");
900 ++begin;
901
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000902 // Remove an optional ud-suffix.
903 if (end[-1] != '\'') {
904 const char *UDSuffixEnd = end;
905 do {
906 --end;
907 } while (end[-1] != '\'');
908 UDSuffixBuf.assign(end, UDSuffixEnd);
Richard Smith26b75c02012-03-09 22:27:51 +0000909 UDSuffixOffset = end - TokBegin;
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000910 }
911
Seth Cantrellbe773522012-01-18 12:27:04 +0000912 // Trim the ending quote.
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000913 assert(end != begin && "Invalid token lexed");
Seth Cantrellbe773522012-01-18 12:27:04 +0000914 --end;
915
Mike Stump1eb44332009-09-09 15:08:12 +0000916 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000917 // up to 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000919 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000921 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
922 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
923 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
924 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
925 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000926
Seth Cantrellbe773522012-01-18 12:27:04 +0000927 SmallVector<uint32_t,4> codepoint_buffer;
928 codepoint_buffer.resize(end-begin);
929 uint32_t *buffer_begin = &codepoint_buffer.front();
930 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Seth Cantrellbe773522012-01-18 12:27:04 +0000932 // Unicode escapes representing characters that cannot be correctly
933 // represented in a single code unit are disallowed in character literals
934 // by this implementation.
935 uint32_t largest_character_for_kind;
936 if (tok::wide_char_constant == Kind) {
937 largest_character_for_kind = 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
938 } else if (tok::utf16_char_constant == Kind) {
939 largest_character_for_kind = 0xFFFF;
940 } else if (tok::utf32_char_constant == Kind) {
941 largest_character_for_kind = 0x10FFFF;
942 } else {
943 largest_character_for_kind = 0x7Fu;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000944 }
945
Seth Cantrellbe773522012-01-18 12:27:04 +0000946 while (begin!=end) {
947 // Is this a span of non-escape characters?
948 if (begin[0] != '\\') {
949 char const *start = begin;
950 do {
951 ++begin;
952 } while (begin != end && *begin != '\\');
953
Eli Friedman91359302012-02-11 05:08:10 +0000954 char const *tmp_in_start = start;
955 uint32_t *tmp_out_start = buffer_begin;
Seth Cantrellbe773522012-01-18 12:27:04 +0000956 ConversionResult res =
957 ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
958 reinterpret_cast<UTF8 const *>(begin),
959 &buffer_begin,buffer_end,strictConversion);
960 if (res!=conversionOK) {
Eli Friedman91359302012-02-11 05:08:10 +0000961 // If we see bad encoding for unprefixed character literals, warn and
962 // simply copy the byte values, for compatibility with gcc and
963 // older versions of clang.
964 bool NoErrorOnBadEncoding = isAscii();
965 unsigned Msg = diag::err_bad_character_encoding;
966 if (NoErrorOnBadEncoding)
967 Msg = diag::warn_bad_character_encoding;
968 PP.Diag(Loc, Msg);
969 if (NoErrorOnBadEncoding) {
970 start = tmp_in_start;
971 buffer_begin = tmp_out_start;
972 for ( ; start != begin; ++start, ++buffer_begin)
973 *buffer_begin = static_cast<uint8_t>(*start);
974 } else {
975 HadError = true;
976 }
Seth Cantrellbe773522012-01-18 12:27:04 +0000977 } else {
Eli Friedman91359302012-02-11 05:08:10 +0000978 for (; tmp_out_start <buffer_begin; ++tmp_out_start) {
979 if (*tmp_out_start > largest_character_for_kind) {
Seth Cantrellbe773522012-01-18 12:27:04 +0000980 HadError = true;
981 PP.Diag(Loc, diag::err_character_too_large);
982 }
983 }
984 }
985
986 continue;
987 }
988 // Is this a Universal Character Name excape?
989 if (begin[1] == 'u' || begin[1] == 'U') {
990 unsigned short UcnLen = 0;
Richard Smith26b75c02012-03-09 22:27:51 +0000991 if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen,
Seth Cantrellbe773522012-01-18 12:27:04 +0000992 FullSourceLoc(Loc, PP.getSourceManager()),
David Blaikie4e4d0842012-03-11 07:00:24 +0000993 &PP.getDiagnostics(), PP.getLangOpts(),
Seth Cantrellbe773522012-01-18 12:27:04 +0000994 true))
995 {
996 HadError = true;
997 } else if (*buffer_begin > largest_character_for_kind) {
998 HadError = true;
Richard Smithe5f05882012-09-08 07:16:20 +0000999 PP.Diag(Loc, diag::err_character_too_large);
Seth Cantrellbe773522012-01-18 12:27:04 +00001000 }
1001
1002 ++buffer_begin;
1003 continue;
1004 }
1005 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
1006 uint64_t result =
Richard Smithe5f05882012-09-08 07:16:20 +00001007 ProcessCharEscape(TokBegin, begin, end, HadError,
1008 FullSourceLoc(Loc,PP.getSourceManager()),
1009 CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
Seth Cantrellbe773522012-01-18 12:27:04 +00001010 *buffer_begin++ = result;
1011 }
1012
1013 unsigned NumCharsSoFar = buffer_begin-&codepoint_buffer.front();
1014
Chris Lattnere3ad8812009-04-28 21:51:46 +00001015 if (NumCharsSoFar > 1) {
Seth Cantrellbe773522012-01-18 12:27:04 +00001016 if (isWide())
Douglas Gregor5cee1192011-07-27 05:40:30 +00001017 PP.Diag(Loc, diag::warn_extraneous_char_constant);
Seth Cantrellbe773522012-01-18 12:27:04 +00001018 else if (isAscii() && NumCharsSoFar == 4)
1019 PP.Diag(Loc, diag::ext_four_char_character_literal);
1020 else if (isAscii())
Chris Lattnere3ad8812009-04-28 21:51:46 +00001021 PP.Diag(Loc, diag::ext_multichar_character_literal);
1022 else
Seth Cantrellbe773522012-01-18 12:27:04 +00001023 PP.Diag(Loc, diag::err_multichar_utf_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +00001024 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +00001025 } else
1026 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +00001027
Seth Cantrellbe773522012-01-18 12:27:04 +00001028 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
1029
1030 // Narrow character literals act as though their value is concatenated
1031 // in this implementation, but warn on overflow.
1032 bool multi_char_too_long = false;
1033 if (isAscii() && isMultiChar()) {
1034 LitVal = 0;
1035 for (size_t i=0;i<NumCharsSoFar;++i) {
1036 // check for enough leading zeros to shift into
1037 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1038 LitVal <<= 8;
1039 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1040 }
1041 } else if (NumCharsSoFar > 0) {
1042 // otherwise just take the last character
1043 LitVal = buffer_begin[-1];
1044 }
1045
1046 if (!HadError && multi_char_too_long) {
1047 PP.Diag(Loc,diag::warn_char_constant_too_large);
1048 }
1049
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +00001050 // Transfer the value from APInt to uint64_t
1051 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1054 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
1055 // character constants are not sign extended in the this implementation:
1056 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001057 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001058 PP.getLangOpts().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 Value = (signed char)Value;
1060}
1061
James Dennetta1263cf2012-06-19 21:04:25 +00001062/// \verbatim
Craig Topper2fa4e862011-08-11 04:06:15 +00001063/// string-literal: [C++0x lex.string]
1064/// encoding-prefix " [s-char-sequence] "
1065/// encoding-prefix R raw-string
1066/// encoding-prefix:
1067/// u8
1068/// u
1069/// U
1070/// L
Reid Spencer5f016e22007-07-11 17:01:13 +00001071/// s-char-sequence:
1072/// s-char
1073/// s-char-sequence s-char
1074/// s-char:
Craig Topper2fa4e862011-08-11 04:06:15 +00001075/// any member of the source character set except the double-quote ",
1076/// backslash \, or new-line character
1077/// escape-sequence
Reid Spencer5f016e22007-07-11 17:01:13 +00001078/// universal-character-name
Craig Topper2fa4e862011-08-11 04:06:15 +00001079/// raw-string:
1080/// " d-char-sequence ( r-char-sequence ) d-char-sequence "
1081/// r-char-sequence:
1082/// r-char
1083/// r-char-sequence r-char
1084/// r-char:
1085/// any member of the source character set, except a right parenthesis )
1086/// followed by the initial d-char-sequence (which may be empty)
1087/// followed by a double quote ".
1088/// d-char-sequence:
1089/// d-char
1090/// d-char-sequence d-char
1091/// d-char:
1092/// any member of the basic source character set except:
1093/// space, the left parenthesis (, the right parenthesis ),
1094/// the backslash \, and the control characters representing horizontal
1095/// tab, vertical tab, form feed, and newline.
1096/// escape-sequence: [C++0x lex.ccon]
1097/// simple-escape-sequence
1098/// octal-escape-sequence
1099/// hexadecimal-escape-sequence
1100/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +00001101/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +00001102/// octal-escape-sequence:
1103/// \ octal-digit
1104/// \ octal-digit octal-digit
1105/// \ octal-digit octal-digit octal-digit
1106/// hexadecimal-escape-sequence:
1107/// \x hexadecimal-digit
1108/// hexadecimal-escape-sequence hexadecimal-digit
Reid Spencer5f016e22007-07-11 17:01:13 +00001109/// universal-character-name:
1110/// \u hex-quad
1111/// \U hex-quad hex-quad
1112/// hex-quad:
1113/// hex-digit hex-digit hex-digit hex-digit
James Dennetta1263cf2012-06-19 21:04:25 +00001114/// \endverbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001115///
1116StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +00001117StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner0833dd02010-11-17 07:21:13 +00001118 Preprocessor &PP, bool Complain)
David Blaikie4e4d0842012-03-11 07:00:24 +00001119 : SM(PP.getSourceManager()), Features(PP.getLangOpts()),
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001120 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001121 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
1122 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
Chris Lattner0833dd02010-11-17 07:21:13 +00001123 init(StringToks, NumStringToks);
1124}
1125
1126void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001127 // The literal token may have come from an invalid source location (e.g. due
1128 // to a PCH error), in which case the token length will be 0.
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001129 if (NumStringToks == 0 || StringToks[0].getLength() < 2)
1130 return DiagnoseLexingError(SourceLocation());
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001131
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 // Scan all of the string portions, remember the max individual token length,
1133 // computing a bound on the concatenated string length, and see whether any
1134 // piece is a wide-string. If any of the string portions is a wide-string
1135 // literal, the result is a wide-string literal [C99 6.4.5p4].
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001136 assert(NumStringToks && "expected at least one token");
Sean Hunt6cf75022010-08-30 17:47:05 +00001137 MaxTokenLength = StringToks[0].getLength();
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001138 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +00001139 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Douglas Gregor5cee1192011-07-27 05:40:30 +00001140 Kind = StringToks[0].getKind();
Sean Hunt6cf75022010-08-30 17:47:05 +00001141
1142 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001143
1144 // Implement Translation Phase #6: concatenation of string literals
1145 /// (C99 5.1.1.2p1). The common case is only one string fragment.
1146 for (unsigned i = 1; i != NumStringToks; ++i) {
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001147 if (StringToks[i].getLength() < 2)
1148 return DiagnoseLexingError(StringToks[i].getLocation());
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001149
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 // The string could be shorter than this if it needs cleaning, but this is a
1151 // reasonable bound, which is all we need.
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001152 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +00001153 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +00001156 if (StringToks[i].getLength() > MaxTokenLength)
1157 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor5cee1192011-07-27 05:40:30 +00001159 // Remember if we see any wide or utf-8/16/32 strings.
1160 // Also check for illegal concatenations.
1161 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
1162 if (isAscii()) {
1163 Kind = StringToks[i].getKind();
1164 } else {
1165 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +00001166 Diags->Report(StringToks[i].getLocation(),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001167 diag::err_unsupported_string_concat);
1168 hadError = true;
1169 }
1170 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +00001172
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 // Include space for the null terminator.
1174 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor5cee1192011-07-27 05:40:30 +00001178 // Get the width in bytes of char/wchar_t/char16_t/char32_t
1179 CharByteWidth = getCharWidth(Kind, Target);
1180 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
1181 CharByteWidth /= 8;
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 // The output buffer size needs to be large enough to hold wide characters.
1184 // This is a worst-case assumption which basically corresponds to L"" "long".
Douglas Gregor5cee1192011-07-27 05:40:30 +00001185 SizeBound *= CharByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 // Size the temporary buffer to hold the result string data.
1188 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 // Likewise, but for each string piece.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001191 SmallString<512> TokenBuf;
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 // Loop over all the strings, getting their spelling, and expanding them to
1195 // wide strings as appropriate.
1196 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Anders Carlssonee98ac52007-10-15 02:50:23 +00001198 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001200 SourceLocation UDSuffixTokLoc;
1201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
1203 const char *ThisTokBuf = &TokenBuf[0];
1204 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1205 // that ThisTokBuf points to a buffer that is big enough for the whole token
1206 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001207 bool StringInvalid = false;
Chris Lattner0833dd02010-11-17 07:21:13 +00001208 unsigned ThisTokLen =
Chris Lattnerb0607272010-11-17 07:26:20 +00001209 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1210 &StringInvalid);
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001211 if (StringInvalid)
1212 return DiagnoseLexingError(StringToks[i].getLocation());
Douglas Gregor50f6af72010-03-16 05:20:39 +00001213
Richard Smith26b75c02012-03-09 22:27:51 +00001214 const char *ThisTokBegin = ThisTokBuf;
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001215 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1216
1217 // Remove an optional ud-suffix.
1218 if (ThisTokEnd[-1] != '"') {
1219 const char *UDSuffixEnd = ThisTokEnd;
1220 do {
1221 --ThisTokEnd;
1222 } while (ThisTokEnd[-1] != '"');
1223
1224 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1225
1226 if (UDSuffixBuf.empty()) {
1227 UDSuffixBuf.assign(UDSuffix);
Richard Smithdd66be72012-03-08 01:34:56 +00001228 UDSuffixToken = i;
1229 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001230 UDSuffixTokLoc = StringToks[i].getLocation();
1231 } else if (!UDSuffixBuf.equals(UDSuffix)) {
1232 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1233 // result of a concatenation involving at least one user-defined-string-
1234 // literal, all the participating user-defined-string-literals shall
1235 // have the same ud-suffix.
1236 if (Diags) {
1237 SourceLocation TokLoc = StringToks[i].getLocation();
1238 Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix)
1239 << UDSuffixBuf << UDSuffix
1240 << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc)
1241 << SourceRange(TokLoc, TokLoc);
1242 }
1243 hadError = true;
1244 }
1245 }
1246
1247 // Strip the end quote.
1248 --ThisTokEnd;
1249
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Craig Topper1661d712011-08-08 06:10:39 +00001252 // Skip marker for wide or unicode strings.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001253 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 ++ThisTokBuf;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001255 // Skip 8 of u8 marker for utf8 strings.
1256 if (ThisTokBuf[0] == '8')
1257 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +00001258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Craig Topper2fa4e862011-08-11 04:06:15 +00001260 // Check for raw string
1261 if (ThisTokBuf[0] == 'R') {
1262 ThisTokBuf += 2; // skip R"
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Craig Topper2fa4e862011-08-11 04:06:15 +00001264 const char *Prefix = ThisTokBuf;
1265 while (ThisTokBuf[0] != '(')
Anders Carlssonee98ac52007-10-15 02:50:23 +00001266 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001267 ++ThisTokBuf; // skip '('
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Richard Smith49d51742012-03-08 21:59:28 +00001269 // Remove same number of characters from the end
1270 ThisTokEnd -= ThisTokBuf - Prefix;
1271 assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal");
Craig Topper2fa4e862011-08-11 04:06:15 +00001272
1273 // Copy the string over
Richard Smithe5f05882012-09-08 07:16:20 +00001274 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1275 StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf)))
1276 hadError = true;
Craig Topper2fa4e862011-08-11 04:06:15 +00001277 } else {
Argyrios Kyrtzidis07a07582012-05-03 01:01:56 +00001278 if (ThisTokBuf[0] != '"') {
1279 // The file may have come from PCH and then changed after loading the
1280 // PCH; Fail gracefully.
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001281 return DiagnoseLexingError(StringToks[i].getLocation());
Argyrios Kyrtzidis07a07582012-05-03 01:01:56 +00001282 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001283 ++ThisTokBuf; // skip "
1284
1285 // Check if this is a pascal string
1286 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1287 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1288
1289 // If the \p sequence is found in the first token, we have a pascal string
1290 // Otherwise, if we already have a pascal string, ignore the first \p
1291 if (i == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001293 Pascal = true;
1294 } else if (Pascal)
1295 ThisTokBuf += 2;
1296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Craig Topper2fa4e862011-08-11 04:06:15 +00001298 while (ThisTokBuf != ThisTokEnd) {
1299 // Is this a span of non-escape characters?
1300 if (ThisTokBuf[0] != '\\') {
1301 const char *InStart = ThisTokBuf;
1302 do {
1303 ++ThisTokBuf;
1304 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1305
1306 // Copy the character span over.
Richard Smithe5f05882012-09-08 07:16:20 +00001307 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1308 StringRef(InStart, ThisTokBuf - InStart)))
1309 hadError = true;
Craig Topper2fa4e862011-08-11 04:06:15 +00001310 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001312 // Is this a Universal Character Name escape?
1313 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Richard Smith26b75c02012-03-09 22:27:51 +00001314 EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
1315 ResultPtr, hadError,
1316 FullSourceLoc(StringToks[i].getLocation(), SM),
Craig Topper2fa4e862011-08-11 04:06:15 +00001317 CharByteWidth, Diags, Features);
1318 continue;
1319 }
1320 // Otherwise, this is a non-UCN escape character. Process it.
1321 unsigned ResultChar =
Richard Smithe5f05882012-09-08 07:16:20 +00001322 ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError,
Craig Topper2fa4e862011-08-11 04:06:15 +00001323 FullSourceLoc(StringToks[i].getLocation(), SM),
Richard Smithe5f05882012-09-08 07:16:20 +00001324 CharByteWidth*8, Diags, Features);
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Eli Friedmancaf1f262011-11-02 23:06:23 +00001326 if (CharByteWidth == 4) {
1327 // FIXME: Make the type of the result buffer correct instead of
1328 // using reinterpret_cast.
1329 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr);
Nico Weber9b483df2011-11-14 05:17:37 +00001330 *ResultWidePtr = ResultChar;
Eli Friedmancaf1f262011-11-02 23:06:23 +00001331 ResultPtr += 4;
1332 } else if (CharByteWidth == 2) {
1333 // FIXME: Make the type of the result buffer correct instead of
1334 // using reinterpret_cast.
1335 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr);
Nico Weber9b483df2011-11-14 05:17:37 +00001336 *ResultWidePtr = ResultChar & 0xFFFF;
Eli Friedmancaf1f262011-11-02 23:06:23 +00001337 ResultPtr += 2;
1338 } else {
1339 assert(CharByteWidth == 1 && "Unexpected char width");
1340 *ResultPtr++ = ResultChar & 0xFF;
1341 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001342 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 }
1344 }
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001346 if (Pascal) {
Eli Friedman22508f42011-11-05 00:41:04 +00001347 if (CharByteWidth == 4) {
1348 // FIXME: Make the type of the result buffer correct instead of
1349 // using reinterpret_cast.
1350 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data());
1351 ResultWidePtr[0] = GetNumStringChars() - 1;
1352 } else if (CharByteWidth == 2) {
1353 // FIXME: Make the type of the result buffer correct instead of
1354 // using reinterpret_cast.
1355 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data());
1356 ResultWidePtr[0] = GetNumStringChars() - 1;
1357 } else {
1358 assert(CharByteWidth == 1 && "Unexpected char width");
1359 ResultBuf[0] = GetNumStringChars() - 1;
1360 }
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001361
1362 // Verify that pascal strings aren't too large.
Chris Lattner0833dd02010-11-17 07:21:13 +00001363 if (GetStringLength() > 256) {
Richard Smithe5f05882012-09-08 07:16:20 +00001364 if (Diags)
1365 Diags->Report(StringToks[0].getLocation(),
Chris Lattner0833dd02010-11-17 07:21:13 +00001366 diag::err_pascal_string_too_long)
1367 << SourceRange(StringToks[0].getLocation(),
1368 StringToks[NumStringToks-1].getLocation());
Douglas Gregor5cee1192011-07-27 05:40:30 +00001369 hadError = true;
Eli Friedman57d7dde2009-04-01 03:17:08 +00001370 return;
1371 }
Chris Lattner0833dd02010-11-17 07:21:13 +00001372 } else if (Diags) {
Douglas Gregor427c4922010-07-20 14:33:20 +00001373 // Complain if this string literal has too many characters.
Chris Lattnera95880d2010-11-17 07:12:42 +00001374 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Douglas Gregor427c4922010-07-20 14:33:20 +00001375
1376 if (GetNumStringChars() > MaxChars)
Richard Smithe5f05882012-09-08 07:16:20 +00001377 Diags->Report(StringToks[0].getLocation(),
Chris Lattner0833dd02010-11-17 07:21:13 +00001378 diag::ext_string_too_long)
Douglas Gregor427c4922010-07-20 14:33:20 +00001379 << GetNumStringChars() << MaxChars
Chris Lattnera95880d2010-11-17 07:12:42 +00001380 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregor427c4922010-07-20 14:33:20 +00001381 << SourceRange(StringToks[0].getLocation(),
1382 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001383 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001384}
Chris Lattner719e6152009-02-18 19:21:10 +00001385
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001386static const char *resync_utf8(const char *err, const char *end) {
1387 if (err==end)
1388 return end;
1389 end = err + std::min<unsigned>(getNumBytesForUTF8(*err), end-err);
1390 while (++err!=end && (*err&0xC0)==0x80)
1391 ;
1392 return err;
1393}
1394
Richard Smithe5f05882012-09-08 07:16:20 +00001395/// \brief This function copies from Fragment, which is a sequence of bytes
1396/// within Tok's contents (which begin at TokBegin) into ResultPtr.
Craig Topper2fa4e862011-08-11 04:06:15 +00001397/// Performs widening for multi-byte characters.
Richard Smithe5f05882012-09-08 07:16:20 +00001398bool StringLiteralParser::CopyStringFragment(const Token &Tok,
1399 const char *TokBegin,
1400 StringRef Fragment) {
1401 const UTF8 *ErrorPtrTmp;
1402 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1403 return false;
Craig Topper2fa4e862011-08-11 04:06:15 +00001404
Eli Friedman91359302012-02-11 05:08:10 +00001405 // If we see bad encoding for unprefixed string literals, warn and
1406 // simply copy the byte values, for compatibility with gcc and older
1407 // versions of clang.
1408 bool NoErrorOnBadEncoding = isAscii();
Richard Smithe5f05882012-09-08 07:16:20 +00001409 if (NoErrorOnBadEncoding) {
1410 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1411 ResultPtr += Fragment.size();
1412 }
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001413
Richard Smithe5f05882012-09-08 07:16:20 +00001414 if (Diags) {
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001415 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1416
1417 FullSourceLoc SourceLoc(Tok.getLocation(), SM);
1418 const DiagnosticBuilder &Builder =
1419 Diag(Diags, Features, SourceLoc, TokBegin,
1420 ErrorPtr, resync_utf8(ErrorPtr, Fragment.end()),
1421 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1422 : diag::err_bad_string_encoding);
1423
1424 char *SavedResultPtr = ResultPtr;
1425 const char *NextStart = resync_utf8(ErrorPtr, Fragment.end());
1426 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1427
David Blaikie82c6dc72012-10-30 23:22:22 +00001428 while (!Builder.hasMaxRanges() &&
1429 !ConvertUTF8toWide(CharByteWidth, NextFragment, ResultPtr,
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001430 ErrorPtrTmp)) {
1431 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1432 NextStart = resync_utf8(ErrorPtr, Fragment.end());
1433 Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin,
1434 ErrorPtr, NextStart);
1435 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1436 }
1437
1438 ResultPtr = SavedResultPtr;
Richard Smithe5f05882012-09-08 07:16:20 +00001439 }
Eli Friedman91359302012-02-11 05:08:10 +00001440 return !NoErrorOnBadEncoding;
1441}
Craig Topper2fa4e862011-08-11 04:06:15 +00001442
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001443void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) {
1444 hadError = true;
1445 if (Diags)
1446 Diags->Report(Loc, diag::err_lexing_string);
1447}
1448
Chris Lattner719e6152009-02-18 19:21:10 +00001449/// getOffsetOfStringByte - This function returns the offset of the
1450/// specified byte of the string data represented by Token. This handles
1451/// advancing over escape sequences in the string.
1452unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattner6c66f072010-11-17 06:46:14 +00001453 unsigned ByteNo) const {
Chris Lattner719e6152009-02-18 19:21:10 +00001454 // Get the spelling of the token.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001455 SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001456 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Douglas Gregor50f6af72010-03-16 05:20:39 +00001458 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001459 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerb0607272010-11-17 07:26:20 +00001460 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1461 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001462 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001463 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001464
Chris Lattner719e6152009-02-18 19:21:10 +00001465 const char *SpellingStart = SpellingPtr;
1466 const char *SpellingEnd = SpellingPtr+TokLen;
1467
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001468 // Handle UTF-8 strings just like narrow strings.
1469 if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8')
1470 SpellingPtr += 2;
1471
1472 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1473 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
1474
1475 // For raw string literals, this is easy.
1476 if (SpellingPtr[0] == 'R') {
1477 assert(SpellingPtr[1] == '"' && "Should be a raw string literal!");
1478 // Skip 'R"'.
1479 SpellingPtr += 2;
1480 while (*SpellingPtr != '(') {
1481 ++SpellingPtr;
1482 assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal");
1483 }
1484 // Skip '('.
1485 ++SpellingPtr;
1486 return SpellingPtr - SpellingStart + ByteNo;
1487 }
1488
1489 // Skip over the leading quote
Chris Lattner719e6152009-02-18 19:21:10 +00001490 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1491 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Chris Lattner719e6152009-02-18 19:21:10 +00001493 // Skip over bytes until we find the offset we're looking for.
1494 while (ByteNo) {
1495 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattner719e6152009-02-18 19:21:10 +00001497 // Step over non-escapes simply.
1498 if (*SpellingPtr != '\\') {
1499 ++SpellingPtr;
1500 --ByteNo;
1501 continue;
1502 }
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Chris Lattner719e6152009-02-18 19:21:10 +00001504 // Otherwise, this is an escape character. Advance over it.
1505 bool HadError = false;
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001506 if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') {
1507 const char *EscapePtr = SpellingPtr;
1508 unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd,
1509 1, Features, HadError);
1510 if (Len > ByteNo) {
1511 // ByteNo is somewhere within the escape sequence.
1512 SpellingPtr = EscapePtr;
1513 break;
1514 }
1515 ByteNo -= Len;
1516 } else {
Richard Smithe5f05882012-09-08 07:16:20 +00001517 ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError,
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001518 FullSourceLoc(Tok.getLocation(), SM),
Richard Smithe5f05882012-09-08 07:16:20 +00001519 CharByteWidth*8, Diags, Features);
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001520 --ByteNo;
1521 }
Chris Lattner719e6152009-02-18 19:21:10 +00001522 assert(!HadError && "This method isn't valid on erroneous strings");
Chris Lattner719e6152009-02-18 19:21:10 +00001523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Chris Lattner719e6152009-02-18 19:21:10 +00001525 return SpellingPtr-SpellingStart;
1526}