blob: 1647aa538191e02f35dafdace2f38d908b75c92b [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"
Eli Friedmanf74a4582011-11-01 02:14:50 +000016#include "clang/Basic/ConvertUTF.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/Lex/LexDiagnostic.h"
19#include "clang/Lex/Preprocessor.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
Douglas Gregor5cee1192011-07-27 05:40:30 +000024static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
25 switch (kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000026 default: llvm_unreachable("Unknown token type!");
Douglas Gregor5cee1192011-07-27 05:40:30 +000027 case tok::char_constant:
28 case tok::string_literal:
29 case tok::utf8_string_literal:
30 return Target.getCharWidth();
31 case tok::wide_char_constant:
32 case tok::wide_string_literal:
33 return Target.getWCharWidth();
34 case tok::utf16_char_constant:
35 case tok::utf16_string_literal:
36 return Target.getChar16Width();
37 case tok::utf32_char_constant:
38 case tok::utf32_string_literal:
39 return Target.getChar32Width();
40 }
41}
42
Seth Cantrell5bffbe52012-10-28 18:24:46 +000043static CharSourceRange MakeCharSourceRange(const LangOptions &Features,
44 FullSourceLoc TokLoc,
45 const char *TokBegin,
46 const char *TokRangeBegin,
47 const char *TokRangeEnd) {
48 SourceLocation Begin =
49 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
50 TokLoc.getManager(), Features);
51 SourceLocation End =
52 Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin,
53 TokLoc.getManager(), Features);
54 return CharSourceRange::getCharRange(Begin, End);
55}
56
Richard Smithe5f05882012-09-08 07:16:20 +000057/// \brief Produce a diagnostic highlighting some portion of a literal.
58///
59/// Emits the diagnostic \p DiagID, highlighting the range of characters from
60/// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
61/// a substring of a spelling buffer for the token beginning at \p TokBegin.
62static DiagnosticBuilder Diag(DiagnosticsEngine *Diags,
63 const LangOptions &Features, FullSourceLoc TokLoc,
64 const char *TokBegin, const char *TokRangeBegin,
65 const char *TokRangeEnd, unsigned DiagID) {
66 SourceLocation Begin =
67 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
68 TokLoc.getManager(), Features);
Seth Cantrell5bffbe52012-10-28 18:24:46 +000069 return Diags->Report(Begin, DiagID) <<
70 MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd);
Richard Smithe5f05882012-09-08 07:16:20 +000071}
72
Reid Spencer5f016e22007-07-11 17:01:13 +000073/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
74/// either a character or a string literal.
Richard Smithe5f05882012-09-08 07:16:20 +000075static unsigned ProcessCharEscape(const char *ThisTokBegin,
76 const char *&ThisTokBuf,
Reid Spencer5f016e22007-07-11 17:01:13 +000077 const char *ThisTokEnd, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +000078 FullSourceLoc Loc, unsigned CharWidth,
Richard Smithe5f05882012-09-08 07:16:20 +000079 DiagnosticsEngine *Diags,
80 const LangOptions &Features) {
81 const char *EscapeBegin = ThisTokBuf;
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083 // Skip the '\' char.
84 ++ThisTokBuf;
85
86 // We know that this character can't be off the end of the buffer, because
87 // that would have been \", which would not have been the end of string.
88 unsigned ResultChar = *ThisTokBuf++;
89 switch (ResultChar) {
90 // These map to themselves.
91 case '\\': case '\'': case '"': case '?': break;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Reid Spencer5f016e22007-07-11 17:01:13 +000093 // These have fixed mappings.
94 case 'a':
95 // TODO: K&R: the meaning of '\\a' is different in traditional C
96 ResultChar = 7;
97 break;
98 case 'b':
99 ResultChar = 8;
100 break;
101 case 'e':
Chris Lattner91f54ce2010-11-17 06:26:08 +0000102 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000103 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
104 diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 ResultChar = 27;
106 break;
Eli Friedman3c548012009-06-10 01:32:39 +0000107 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +0000108 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000109 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
110 diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +0000111 ResultChar = 27;
112 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 case 'f':
114 ResultChar = 12;
115 break;
116 case 'n':
117 ResultChar = 10;
118 break;
119 case 'r':
120 ResultChar = 13;
121 break;
122 case 't':
123 ResultChar = 9;
124 break;
125 case 'v':
126 ResultChar = 11;
127 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 case 'x': { // Hex escape.
129 ResultChar = 0;
130 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000131 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000132 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
Jordan Rose5209e2b2013-01-24 20:50:13 +0000133 diag::err_hex_escape_no_digits) << "x";
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 HadError = 1;
135 break;
136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // Hex escapes are a maximal series of hex digits.
139 bool Overflow = false;
140 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
Jordan Rose728bb4c2013-01-18 22:33:58 +0000141 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000143 // About to shift out a digit?
144 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 ResultChar <<= 4;
146 ResultChar |= CharVal;
147 }
148
149 // See if any bits will be truncated when evaluated as a character.
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
151 Overflow = true;
152 ResultChar &= ~0U >> (32-CharWidth);
153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 // Check for overflow.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000156 if (Overflow && Diags) // Too many digits to fit in
Richard Smithe5f05882012-09-08 07:16:20 +0000157 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
158 diag::warn_hex_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 break;
160 }
161 case '0': case '1': case '2': case '3':
162 case '4': case '5': case '6': case '7': {
163 // Octal escapes.
164 --ThisTokBuf;
165 ResultChar = 0;
166
167 // Octal escapes are a series of octal digits with maximum length 3.
168 // "\0123" is a two digit sequence equal to "\012" "3".
169 unsigned NumDigits = 0;
170 do {
171 ResultChar <<= 3;
172 ResultChar |= *ThisTokBuf++ - '0';
173 ++NumDigits;
174 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
175 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 // Check for overflow. Reject '\777', but not L'\777'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000179 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000180 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
181 diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 ResultChar &= ~0U >> (32-CharWidth);
183 }
184 break;
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // Otherwise, these are not valid escapes.
188 case '(': case '{': case '[': case '%':
189 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000190 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000191 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
192 diag::ext_nonstandard_escape)
193 << std::string(1, ResultChar);
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000194 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000196 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000197 break;
Richard Smithe5f05882012-09-08 07:16:20 +0000198
Ted Kremenek23ef69d2010-12-03 00:09:56 +0000199 if (isgraph(ResultChar))
Richard Smithe5f05882012-09-08 07:16:20 +0000200 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
201 diag::ext_unknown_escape)
202 << std::string(1, ResultChar);
Chris Lattnerac92d822008-11-22 07:23:31 +0000203 else
Richard Smithe5f05882012-09-08 07:16:20 +0000204 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
205 diag::ext_unknown_escape)
206 << "x" + llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 break;
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 return ResultChar;
211}
212
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000213/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-10-09 00:27:47 +0000214/// return the UTF32.
Richard Smith26b75c02012-03-09 22:27:51 +0000215static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
216 const char *ThisTokEnd,
Nico Weber59705ae2010-10-09 00:27:47 +0000217 uint32_t &UcnVal, unsigned short &UcnLen,
David Blaikied6471f72011-09-25 23:23:43 +0000218 FullSourceLoc Loc, DiagnosticsEngine *Diags,
Seth Cantrellbe773522012-01-18 12:27:04 +0000219 const LangOptions &Features,
220 bool in_char_string_literal = false) {
Richard Smith26b75c02012-03-09 22:27:51 +0000221 const char *UcnBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000223 // Skip the '\u' char's.
224 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000226 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner6c66f072010-11-17 06:46:14 +0000227 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000228 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
Jordan Rose5209e2b2013-01-24 20:50:13 +0000229 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
Nico Weber59705ae2010-10-09 00:27:47 +0000230 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000231 }
Nico Weber59705ae2010-10-09 00:27:47 +0000232 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000233 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000234 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Jordan Rose728bb4c2013-01-18 22:33:58 +0000235 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000236 if (CharVal == -1) break;
237 UcnVal <<= 4;
238 UcnVal |= CharVal;
239 }
240 // If we didn't consume the proper number of digits, there is a problem.
Nico Weber59705ae2010-10-09 00:27:47 +0000241 if (UcnLenSave) {
Richard Smithe5f05882012-09-08 07:16:20 +0000242 if (Diags)
243 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
244 diag::err_ucn_escape_incomplete);
Nico Weber59705ae2010-10-09 00:27:47 +0000245 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000246 }
Richard Smith26b75c02012-03-09 22:27:51 +0000247
Seth Cantrellbe773522012-01-18 12:27:04 +0000248 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
Richard Smith26b75c02012-03-09 22:27:51 +0000249 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints
250 UcnVal > 0x10FFFF) { // maximum legal UTF32 value
Chris Lattner6c66f072010-11-17 06:46:14 +0000251 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000252 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
253 diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000254 return false;
255 }
Richard Smith26b75c02012-03-09 22:27:51 +0000256
257 // C++11 allows UCNs that refer to control characters and basic source
258 // characters inside character and string literals
259 if (UcnVal < 0xa0 &&
260 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, `
Richard Smith80ad52f2013-01-02 11:42:31 +0000261 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
Richard Smith26b75c02012-03-09 22:27:51 +0000262 if (Diags) {
Richard Smith26b75c02012-03-09 22:27:51 +0000263 char BasicSCSChar = UcnVal;
264 if (UcnVal >= 0x20 && UcnVal < 0x7f)
Richard Smithe5f05882012-09-08 07:16:20 +0000265 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
266 IsError ? diag::err_ucn_escape_basic_scs :
267 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
268 << StringRef(&BasicSCSChar, 1);
Richard Smith26b75c02012-03-09 22:27:51 +0000269 else
Richard Smithe5f05882012-09-08 07:16:20 +0000270 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
271 IsError ? diag::err_ucn_control_character :
272 diag::warn_cxx98_compat_literal_ucn_control_character);
Richard Smith26b75c02012-03-09 22:27:51 +0000273 }
274 if (IsError)
275 return false;
276 }
277
Richard Smithe5f05882012-09-08 07:16:20 +0000278 if (!Features.CPlusPlus && !Features.C99 && Diags)
279 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
280 diag::warn_ucn_not_valid_in_c89);
281
Nico Weber59705ae2010-10-09 00:27:47 +0000282 return true;
283}
284
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000285/// MeasureUCNEscape - Determine the number of bytes within the resulting string
286/// which this UCN will occupy.
287static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
288 const char *ThisTokEnd, unsigned CharByteWidth,
289 const LangOptions &Features, bool &HadError) {
290 // UTF-32: 4 bytes per escape.
291 if (CharByteWidth == 4)
292 return 4;
293
294 uint32_t UcnVal = 0;
295 unsigned short UcnLen = 0;
296 FullSourceLoc Loc;
297
298 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
299 UcnLen, Loc, 0, Features, true)) {
300 HadError = true;
301 return 0;
302 }
303
304 // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
305 if (CharByteWidth == 2)
306 return UcnVal <= 0xFFFF ? 2 : 4;
307
308 // UTF-8.
309 if (UcnVal < 0x80)
310 return 1;
311 if (UcnVal < 0x800)
312 return 2;
313 if (UcnVal < 0x10000)
314 return 3;
315 return 4;
316}
317
Nico Weber59705ae2010-10-09 00:27:47 +0000318/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
319/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
320/// StringLiteralParser. When we decide to implement UCN's for identifiers,
321/// we will likely rework our support for UCN's.
Richard Smith26b75c02012-03-09 22:27:51 +0000322static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
323 const char *ThisTokEnd,
Chris Lattnera95880d2010-11-17 07:12:42 +0000324 char *&ResultBuf, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000325 FullSourceLoc Loc, unsigned CharByteWidth,
David Blaikied6471f72011-09-25 23:23:43 +0000326 DiagnosticsEngine *Diags,
327 const LangOptions &Features) {
Nico Weber59705ae2010-10-09 00:27:47 +0000328 typedef uint32_t UTF32;
329 UTF32 UcnVal = 0;
330 unsigned short UcnLen = 0;
Richard Smith26b75c02012-03-09 22:27:51 +0000331 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
332 Loc, Diags, Features, true)) {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000333 HadError = true;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000334 return;
335 }
Nico Weber59705ae2010-10-09 00:27:47 +0000336
Douglas Gregor5cee1192011-07-27 05:40:30 +0000337 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) &&
338 "only character widths of 1, 2, or 4 bytes supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000339
Douglas Gregor5cee1192011-07-27 05:40:30 +0000340 (void)UcnLen;
341 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000342
Douglas Gregor5cee1192011-07-27 05:40:30 +0000343 if (CharByteWidth == 4) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000344 // FIXME: Make the type of the result buffer correct instead of
345 // using reinterpret_cast.
346 UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf);
347 *ResultPtr = UcnVal;
348 ResultBuf += 4;
Douglas Gregor5cee1192011-07-27 05:40:30 +0000349 return;
350 }
351
352 if (CharByteWidth == 2) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000353 // FIXME: Make the type of the result buffer correct instead of
354 // using reinterpret_cast.
355 UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf);
356
Richard Smith59b26d82012-06-13 05:41:29 +0000357 if (UcnVal <= (UTF32)0xFFFF) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000358 *ResultPtr = UcnVal;
359 ResultBuf += 2;
Nico Webera0f15b02010-10-06 04:57:26 +0000360 return;
361 }
Nico Webera0f15b02010-10-06 04:57:26 +0000362
Eli Friedmancaf1f262011-11-02 23:06:23 +0000363 // Convert to UTF16.
Nico Webera0f15b02010-10-06 04:57:26 +0000364 UcnVal -= 0x10000;
Eli Friedmancaf1f262011-11-02 23:06:23 +0000365 *ResultPtr = 0xD800 + (UcnVal >> 10);
366 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
367 ResultBuf += 4;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000368 return;
369 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000370
371 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
372
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000373 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
374 // The conversion below was inspired by:
375 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000376 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000377 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000378
379 unsigned short bytesToWrite = 0;
380 if (UcnVal < (UTF32)0x80)
381 bytesToWrite = 1;
382 else if (UcnVal < (UTF32)0x800)
383 bytesToWrite = 2;
384 else if (UcnVal < (UTF32)0x10000)
385 bytesToWrite = 3;
386 else
387 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000389 const unsigned byteMask = 0xBF;
390 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000392 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000393 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000394 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000395 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000396 };
397 // Finally, we write the bytes into ResultBuf.
398 ResultBuf += bytesToWrite;
399 switch (bytesToWrite) { // note: everything falls through.
Benjamin Kramer5d704fe2012-11-08 19:22:26 +0000400 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
401 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
402 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
403 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000404 }
405 // Update the buffer.
406 ResultBuf += bytesToWrite;
407}
Reid Spencer5f016e22007-07-11 17:01:13 +0000408
409
410/// integer-constant: [C99 6.4.4.1]
411/// decimal-constant integer-suffix
412/// octal-constant integer-suffix
413/// hexadecimal-constant integer-suffix
Richard Smith49d51742012-03-08 21:59:28 +0000414/// user-defined-integer-literal: [C++11 lex.ext]
Richard Smithb453ad32012-03-08 08:45:32 +0000415/// decimal-literal ud-suffix
416/// octal-literal ud-suffix
417/// hexadecimal-literal ud-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000418/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000419/// nonzero-digit
420/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000421/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000422/// 0
423/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000424/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000425/// hexadecimal-prefix hexadecimal-digit
426/// hexadecimal-constant hexadecimal-digit
427/// hexadecimal-prefix: one of
428/// 0x 0X
429/// integer-suffix:
430/// unsigned-suffix [long-suffix]
431/// unsigned-suffix [long-long-suffix]
432/// long-suffix [unsigned-suffix]
433/// long-long-suffix [unsigned-sufix]
434/// nonzero-digit:
435/// 1 2 3 4 5 6 7 8 9
436/// octal-digit:
437/// 0 1 2 3 4 5 6 7
438/// hexadecimal-digit:
439/// 0 1 2 3 4 5 6 7 8 9
440/// a b c d e f
441/// A B C D E F
442/// unsigned-suffix: one of
443/// u U
444/// long-suffix: one of
445/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000446/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000447/// ll LL
448///
449/// floating-constant: [C99 6.4.4.2]
450/// TODO: add rules...
451///
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000452NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
453 SourceLocation TokLoc,
454 Preprocessor &PP)
455 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000457 // This routine assumes that the range begin/end matches the regex for integer
458 // and FP constants (specifically, the 'pp-number' regex), and assumes that
459 // the byte at "*end" is both valid and not part of the regex. Because of
460 // this, it doesn't have to check for 'overscan' in various places.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000461 assert(!isalnum(*ThisTokEnd) && *ThisTokEnd != '.' && *ThisTokEnd != '_' &&
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000462 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000464 s = DigitsBegin = ThisTokBegin;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 saw_exponent = false;
466 saw_period = false;
Richard Smithb453ad32012-03-08 08:45:32 +0000467 saw_ud_suffix = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 isLong = false;
469 isUnsigned = false;
470 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000471 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000472 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000473 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000477 ParseNumberStartingWithZero(TokLoc);
478 if (hadError)
479 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 } else { // the first digit is non-zero
481 radix = 10;
482 s = SkipDigits(s);
483 if (s == ThisTokEnd) {
484 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000485 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000486 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487 diag::err_invalid_decimal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000488 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 return;
490 } else if (*s == '.') {
491 s++;
492 saw_period = true;
493 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000494 }
Chris Lattner4411f462008-09-29 23:12:31 +0000495 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000496 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 s++;
498 saw_exponent = true;
499 if (*s == '+' || *s == '-') s++; // sign
500 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000501 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000503 } else {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000504 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000505 diag::err_exponent_has_no_digits);
506 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000507 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 }
509 }
510 }
511
512 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner506b8de2007-08-26 01:58:14 +0000514 // Parse the suffix. At this point we can classify whether we have an FP or
515 // integer constant.
516 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner506b8de2007-08-26 01:58:14 +0000518 // Loop over all of the characters of the suffix. If we see something bad,
519 // we break out of the loop.
520 for (; s != ThisTokEnd; ++s) {
521 switch (*s) {
522 case 'f': // FP Suffix for "float"
523 case 'F':
524 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000525 if (isFloat || isLong) break; // FF, LF invalid.
526 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000527 continue; // Success.
528 case 'u':
529 case 'U':
530 if (isFPConstant) break; // Error for floating constant.
531 if (isUnsigned) break; // Cannot be repeated.
532 isUnsigned = true;
533 continue; // Success.
534 case 'l':
535 case 'L':
536 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000537 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner506b8de2007-08-26 01:58:14 +0000539 // Check for long long. The L's need to be adjacent and the same case.
540 if (s+1 != ThisTokEnd && s[1] == s[0]) {
541 if (isFPConstant) break; // long long invalid for floats.
542 isLongLong = true;
543 ++s; // Eat both of them.
544 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000547 continue; // Success.
548 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000549 case 'I':
David Blaikie4e4d0842012-03-11 07:00:24 +0000550 if (PP.getLangOpts().MicrosoftExt) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000551 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000552
Steve Naroff0c29b222008-04-04 21:02:54 +0000553 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000554 if (s + 1 != ThisTokEnd) {
555 switch (s[1]) {
556 case '8':
557 s += 2; // i8 suffix
558 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000559 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000560 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000561 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000562 if (s[2] == '6') {
563 s += 3; // i16 suffix
564 isMicrosoftInteger = true;
565 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000566 else if (s[2] == '2') {
567 if (s + 3 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000568 if (s[3] == '8') {
569 s += 4; // i128 suffix
570 isMicrosoftInteger = true;
571 }
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000572 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000573 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000574 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000575 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000576 if (s[2] == '2') {
577 s += 3; // i32 suffix
578 isLong = true;
579 isMicrosoftInteger = true;
580 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000581 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000582 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000583 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000584 if (s[2] == '4') {
585 s += 3; // i64 suffix
586 isLongLong = true;
587 isMicrosoftInteger = true;
588 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000589 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000590 default:
591 break;
592 }
593 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000594 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000595 }
596 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000597 case 'j':
598 case 'J':
599 if (isImaginary) break; // Cannot be repeated.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000600 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Chris Lattner506b8de2007-08-26 01:58:14 +0000601 diag::ext_imaginary_constant);
602 isImaginary = true;
603 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 }
Richard Smithb453ad32012-03-08 08:45:32 +0000605 // If we reached here, there was an error or a ud-suffix.
Chris Lattner506b8de2007-08-26 01:58:14 +0000606 break;
607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Chris Lattner506b8de2007-08-26 01:58:14 +0000609 if (s != ThisTokEnd) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000610 if (PP.getLangOpts().CPlusPlus11 && s == SuffixBegin && *s == '_') {
Richard Smithb453ad32012-03-08 08:45:32 +0000611 // We have a ud-suffix! By C++11 [lex.ext]p10, ud-suffixes not starting
612 // with an '_' are ill-formed.
613 saw_ud_suffix = true;
614 return;
615 }
616
617 // Report an error if there are any.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000618 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000619 isFPConstant ? diag::err_invalid_suffix_float_constant :
620 diag::err_invalid_suffix_integer_constant)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000622 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000623 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 }
625}
626
Chris Lattner368328c2008-06-30 06:39:54 +0000627/// ParseNumberStartingWithZero - This method is called when the first character
628/// of the number is found to be a zero. This means it is either an octal
629/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000630/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000631/// radix etc.
632void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
633 assert(s[0] == '0' && "Invalid method call");
634 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Chris Lattner368328c2008-06-30 06:39:54 +0000636 // Handle a hex number like 0x1234.
637 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
638 s++;
639 radix = 16;
640 DigitsBegin = s;
641 s = SkipHexDigits(s);
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000642 bool noSignificand = (s == DigitsBegin);
Chris Lattner368328c2008-06-30 06:39:54 +0000643 if (s == ThisTokEnd) {
644 // Done.
645 } else if (*s == '.') {
646 s++;
647 saw_period = true;
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000648 const char *floatDigitsBegin = s;
Chris Lattner368328c2008-06-30 06:39:54 +0000649 s = SkipHexDigits(s);
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000650 noSignificand &= (floatDigitsBegin == s);
Chris Lattner368328c2008-06-30 06:39:54 +0000651 }
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000652
653 if (noSignificand) {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000654 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000655 diag::err_hexconstant_requires_digits);
656 hadError = true;
657 return;
658 }
659
Chris Lattner368328c2008-06-30 06:39:54 +0000660 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000661 // binary exponent is required.
Douglas Gregor1155c422011-08-30 22:40:35 +0000662 if (*s == 'p' || *s == 'P') {
Chris Lattner368328c2008-06-30 06:39:54 +0000663 const char *Exponent = s;
664 s++;
665 saw_exponent = true;
666 if (*s == '+' || *s == '-') s++; // sign
667 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000668 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000669 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
670 diag::err_exponent_has_no_digits);
671 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000672 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000673 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000674 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
David Blaikie4e4d0842012-03-11 07:00:24 +0000676 if (!PP.getLangOpts().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000677 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000678 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000679 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
680 diag::err_hexconstant_requires_exponent);
681 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000682 }
683 return;
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Chris Lattner368328c2008-06-30 06:39:54 +0000686 // Handle simple binary numbers 0b01010
687 if (*s == 'b' || *s == 'B') {
688 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000689 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000690 ++s;
691 radix = 2;
692 DigitsBegin = s;
693 s = SkipBinaryDigits(s);
694 if (s == ThisTokEnd) {
695 // Done.
696 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000697 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000698 diag::err_invalid_binary_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000699 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000700 }
Chris Lattner413d3552008-06-30 06:44:49 +0000701 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000702 return;
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattner368328c2008-06-30 06:39:54 +0000705 // For now, the radix is set to 8. If we discover that we have a
706 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000707 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000708 radix = 8;
709 DigitsBegin = s;
710 s = SkipOctalDigits(s);
711 if (s == ThisTokEnd)
712 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattner413d3552008-06-30 06:44:49 +0000714 // If we have some other non-octal digit that *is* a decimal digit, see if
715 // this is part of a floating point number like 094.123 or 09e1.
716 if (isdigit(*s)) {
717 const char *EndDecimal = SkipDigits(s);
718 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
719 s = EndDecimal;
720 radix = 10;
721 }
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Chris Lattner413d3552008-06-30 06:44:49 +0000724 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
725 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000726 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000727 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000728 diag::err_invalid_octal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000729 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000730 return;
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattner368328c2008-06-30 06:39:54 +0000733 if (*s == '.') {
734 s++;
735 radix = 10;
736 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000737 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000738 }
739 if (*s == 'e' || *s == 'E') { // exponent
740 const char *Exponent = s;
741 s++;
742 radix = 10;
743 saw_exponent = true;
744 if (*s == '+' || *s == '-') s++; // sign
745 const char *first_non_digit = SkipDigits(s);
746 if (first_non_digit != s) {
747 s = first_non_digit;
748 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000749 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000750 diag::err_exponent_has_no_digits);
751 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000752 return;
753 }
754 }
755}
756
Jordan Rose2fd69562012-09-25 22:32:51 +0000757static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) {
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000758 switch (Radix) {
759 case 2:
760 return NumDigits <= 64;
761 case 8:
762 return NumDigits <= 64 / 3; // Digits are groups of 3 bits.
763 case 10:
764 return NumDigits <= 19; // floor(log10(2^64))
765 case 16:
766 return NumDigits <= 64 / 4; // Digits are groups of 4 bits.
767 default:
768 llvm_unreachable("impossible Radix");
769 }
770}
Chris Lattner368328c2008-06-30 06:39:54 +0000771
Reid Spencer5f016e22007-07-11 17:01:13 +0000772/// GetIntegerValue - Convert this numeric literal value to an APInt that
773/// matches Val's input width. If there is an overflow, set Val to the low bits
774/// of the result and return true. Otherwise, return false.
775bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000776 // Fast path: Compute a conservative bound on the maximum number of
777 // bits per digit in this radix. If we can't possibly overflow a
778 // uint64 based on that bound then do the simple conversion to
779 // integer. This avoids the expensive overflow checking below, and
780 // handles the common cases that matter (small decimal integers and
781 // hex/octal values which don't overflow).
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000782 const unsigned NumDigits = SuffixBegin - DigitsBegin;
Jordan Rose2fd69562012-09-25 22:32:51 +0000783 if (alwaysFitsInto64Bits(radix, NumDigits)) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000784 uint64_t N = 0;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000785 for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
Jordan Rose728bb4c2013-01-18 22:33:58 +0000786 N = N * radix + llvm::hexDigitValue(*Ptr);
Daniel Dunbara179be32008-10-16 07:32:01 +0000787
788 // This will truncate the value to Val's input width. Simply check
789 // for overflow by comparing.
790 Val = N;
791 return Val.getZExtValue() != N;
792 }
793
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 Val = 0;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000795 const char *Ptr = DigitsBegin;
Reid Spencer5f016e22007-07-11 17:01:13 +0000796
797 llvm::APInt RadixVal(Val.getBitWidth(), radix);
798 llvm::APInt CharVal(Val.getBitWidth(), 0);
799 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 bool OverflowOccurred = false;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000802 while (Ptr < SuffixBegin) {
Jordan Rose728bb4c2013-01-18 22:33:58 +0000803 unsigned C = llvm::hexDigitValue(*Ptr++);
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 // If this letter is out of bound for this radix, reject it.
806 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 // Add the digit to the value in the appropriate radix. If adding in digits
811 // made the value smaller, then this overflowed.
812 OldVal = Val;
813
814 // Multiply by radix, did overflow occur on the multiply?
815 Val *= RadixVal;
816 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000819 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 OverflowOccurred |= Val.ult(CharVal);
822 }
823 return OverflowOccurred;
824}
825
John McCall94c939d2009-12-24 09:08:04 +0000826llvm::APFloat::opStatus
827NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000828 using llvm::APFloat;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000830 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000831 return Result.convertFromString(StringRef(ThisTokBegin, n),
832 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833}
834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835
James Dennett58f9ce12012-06-17 03:34:42 +0000836/// \verbatim
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000837/// user-defined-character-literal: [C++11 lex.ext]
838/// character-literal ud-suffix
839/// ud-suffix:
840/// identifier
841/// character-literal: [C++11 lex.ccon]
Craig Topper2fa4e862011-08-11 04:06:15 +0000842/// ' c-char-sequence '
843/// u' c-char-sequence '
844/// U' c-char-sequence '
845/// L' c-char-sequence '
846/// c-char-sequence:
847/// c-char
848/// c-char-sequence c-char
849/// c-char:
850/// any member of the source character set except the single-quote ',
851/// backslash \, or new-line character
852/// escape-sequence
853/// universal-character-name
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000854/// escape-sequence:
Craig Topper2fa4e862011-08-11 04:06:15 +0000855/// simple-escape-sequence
856/// octal-escape-sequence
857/// hexadecimal-escape-sequence
858/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000859/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000860/// octal-escape-sequence:
861/// \ octal-digit
862/// \ octal-digit octal-digit
863/// \ octal-digit octal-digit octal-digit
864/// hexadecimal-escape-sequence:
865/// \x hexadecimal-digit
866/// hexadecimal-escape-sequence hexadecimal-digit
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000867/// universal-character-name: [C++11 lex.charset]
Craig Topper2fa4e862011-08-11 04:06:15 +0000868/// \u hex-quad
869/// \U hex-quad hex-quad
870/// hex-quad:
871/// hex-digit hex-digit hex-digit hex-digit
James Dennett58f9ce12012-06-17 03:34:42 +0000872/// \endverbatim
Craig Topper2fa4e862011-08-11 04:06:15 +0000873///
Reid Spencer5f016e22007-07-11 17:01:13 +0000874CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000875 SourceLocation Loc, Preprocessor &PP,
876 tok::TokenKind kind) {
Seth Cantrellbe773522012-01-18 12:27:04 +0000877 // At this point we know that the character matches the regex "(L|u|U)?'.*'".
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregor5cee1192011-07-27 05:40:30 +0000880 Kind = kind;
881
Richard Smith26b75c02012-03-09 22:27:51 +0000882 const char *TokBegin = begin;
883
Seth Cantrellbe773522012-01-18 12:27:04 +0000884 // Skip over wide character determinant.
885 if (Kind != tok::char_constant) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000886 ++begin;
887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 // Skip over the entry quote.
890 assert(begin[0] == '\'' && "Invalid token lexed");
891 ++begin;
892
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000893 // Remove an optional ud-suffix.
894 if (end[-1] != '\'') {
895 const char *UDSuffixEnd = end;
896 do {
897 --end;
898 } while (end[-1] != '\'');
899 UDSuffixBuf.assign(end, UDSuffixEnd);
Richard Smith26b75c02012-03-09 22:27:51 +0000900 UDSuffixOffset = end - TokBegin;
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000901 }
902
Seth Cantrellbe773522012-01-18 12:27:04 +0000903 // Trim the ending quote.
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000904 assert(end != begin && "Invalid token lexed");
Seth Cantrellbe773522012-01-18 12:27:04 +0000905 --end;
906
Mike Stump1eb44332009-09-09 15:08:12 +0000907 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000908 // up to 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000910 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000912 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
913 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
914 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
915 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
916 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000917
Seth Cantrellbe773522012-01-18 12:27:04 +0000918 SmallVector<uint32_t,4> codepoint_buffer;
919 codepoint_buffer.resize(end-begin);
920 uint32_t *buffer_begin = &codepoint_buffer.front();
921 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Seth Cantrellbe773522012-01-18 12:27:04 +0000923 // Unicode escapes representing characters that cannot be correctly
924 // represented in a single code unit are disallowed in character literals
925 // by this implementation.
926 uint32_t largest_character_for_kind;
927 if (tok::wide_char_constant == Kind) {
928 largest_character_for_kind = 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
929 } else if (tok::utf16_char_constant == Kind) {
930 largest_character_for_kind = 0xFFFF;
931 } else if (tok::utf32_char_constant == Kind) {
932 largest_character_for_kind = 0x10FFFF;
933 } else {
934 largest_character_for_kind = 0x7Fu;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000935 }
936
Seth Cantrellbe773522012-01-18 12:27:04 +0000937 while (begin!=end) {
938 // Is this a span of non-escape characters?
939 if (begin[0] != '\\') {
940 char const *start = begin;
941 do {
942 ++begin;
943 } while (begin != end && *begin != '\\');
944
Eli Friedman91359302012-02-11 05:08:10 +0000945 char const *tmp_in_start = start;
946 uint32_t *tmp_out_start = buffer_begin;
Seth Cantrellbe773522012-01-18 12:27:04 +0000947 ConversionResult res =
948 ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
949 reinterpret_cast<UTF8 const *>(begin),
950 &buffer_begin,buffer_end,strictConversion);
951 if (res!=conversionOK) {
Eli Friedman91359302012-02-11 05:08:10 +0000952 // If we see bad encoding for unprefixed character literals, warn and
953 // simply copy the byte values, for compatibility with gcc and
954 // older versions of clang.
955 bool NoErrorOnBadEncoding = isAscii();
956 unsigned Msg = diag::err_bad_character_encoding;
957 if (NoErrorOnBadEncoding)
958 Msg = diag::warn_bad_character_encoding;
959 PP.Diag(Loc, Msg);
960 if (NoErrorOnBadEncoding) {
961 start = tmp_in_start;
962 buffer_begin = tmp_out_start;
963 for ( ; start != begin; ++start, ++buffer_begin)
964 *buffer_begin = static_cast<uint8_t>(*start);
965 } else {
966 HadError = true;
967 }
Seth Cantrellbe773522012-01-18 12:27:04 +0000968 } else {
Eli Friedman91359302012-02-11 05:08:10 +0000969 for (; tmp_out_start <buffer_begin; ++tmp_out_start) {
970 if (*tmp_out_start > largest_character_for_kind) {
Seth Cantrellbe773522012-01-18 12:27:04 +0000971 HadError = true;
972 PP.Diag(Loc, diag::err_character_too_large);
973 }
974 }
975 }
976
977 continue;
978 }
979 // Is this a Universal Character Name excape?
980 if (begin[1] == 'u' || begin[1] == 'U') {
981 unsigned short UcnLen = 0;
Richard Smith26b75c02012-03-09 22:27:51 +0000982 if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen,
Seth Cantrellbe773522012-01-18 12:27:04 +0000983 FullSourceLoc(Loc, PP.getSourceManager()),
David Blaikie4e4d0842012-03-11 07:00:24 +0000984 &PP.getDiagnostics(), PP.getLangOpts(),
Seth Cantrellbe773522012-01-18 12:27:04 +0000985 true))
986 {
987 HadError = true;
988 } else if (*buffer_begin > largest_character_for_kind) {
989 HadError = true;
Richard Smithe5f05882012-09-08 07:16:20 +0000990 PP.Diag(Loc, diag::err_character_too_large);
Seth Cantrellbe773522012-01-18 12:27:04 +0000991 }
992
993 ++buffer_begin;
994 continue;
995 }
996 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
997 uint64_t result =
Richard Smithe5f05882012-09-08 07:16:20 +0000998 ProcessCharEscape(TokBegin, begin, end, HadError,
999 FullSourceLoc(Loc,PP.getSourceManager()),
1000 CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
Seth Cantrellbe773522012-01-18 12:27:04 +00001001 *buffer_begin++ = result;
1002 }
1003
1004 unsigned NumCharsSoFar = buffer_begin-&codepoint_buffer.front();
1005
Chris Lattnere3ad8812009-04-28 21:51:46 +00001006 if (NumCharsSoFar > 1) {
Seth Cantrellbe773522012-01-18 12:27:04 +00001007 if (isWide())
Douglas Gregor5cee1192011-07-27 05:40:30 +00001008 PP.Diag(Loc, diag::warn_extraneous_char_constant);
Seth Cantrellbe773522012-01-18 12:27:04 +00001009 else if (isAscii() && NumCharsSoFar == 4)
1010 PP.Diag(Loc, diag::ext_four_char_character_literal);
1011 else if (isAscii())
Chris Lattnere3ad8812009-04-28 21:51:46 +00001012 PP.Diag(Loc, diag::ext_multichar_character_literal);
1013 else
Seth Cantrellbe773522012-01-18 12:27:04 +00001014 PP.Diag(Loc, diag::err_multichar_utf_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +00001015 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +00001016 } else
1017 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +00001018
Seth Cantrellbe773522012-01-18 12:27:04 +00001019 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
1020
1021 // Narrow character literals act as though their value is concatenated
1022 // in this implementation, but warn on overflow.
1023 bool multi_char_too_long = false;
1024 if (isAscii() && isMultiChar()) {
1025 LitVal = 0;
1026 for (size_t i=0;i<NumCharsSoFar;++i) {
1027 // check for enough leading zeros to shift into
1028 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1029 LitVal <<= 8;
1030 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1031 }
1032 } else if (NumCharsSoFar > 0) {
1033 // otherwise just take the last character
1034 LitVal = buffer_begin[-1];
1035 }
1036
1037 if (!HadError && multi_char_too_long) {
1038 PP.Diag(Loc,diag::warn_char_constant_too_large);
1039 }
1040
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +00001041 // Transfer the value from APInt to uint64_t
1042 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1045 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
1046 // character constants are not sign extended in the this implementation:
1047 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001048 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001049 PP.getLangOpts().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 Value = (signed char)Value;
1051}
1052
James Dennetta1263cf2012-06-19 21:04:25 +00001053/// \verbatim
Craig Topper2fa4e862011-08-11 04:06:15 +00001054/// string-literal: [C++0x lex.string]
1055/// encoding-prefix " [s-char-sequence] "
1056/// encoding-prefix R raw-string
1057/// encoding-prefix:
1058/// u8
1059/// u
1060/// U
1061/// L
Reid Spencer5f016e22007-07-11 17:01:13 +00001062/// s-char-sequence:
1063/// s-char
1064/// s-char-sequence s-char
1065/// s-char:
Craig Topper2fa4e862011-08-11 04:06:15 +00001066/// any member of the source character set except the double-quote ",
1067/// backslash \, or new-line character
1068/// escape-sequence
Reid Spencer5f016e22007-07-11 17:01:13 +00001069/// universal-character-name
Craig Topper2fa4e862011-08-11 04:06:15 +00001070/// raw-string:
1071/// " d-char-sequence ( r-char-sequence ) d-char-sequence "
1072/// r-char-sequence:
1073/// r-char
1074/// r-char-sequence r-char
1075/// r-char:
1076/// any member of the source character set, except a right parenthesis )
1077/// followed by the initial d-char-sequence (which may be empty)
1078/// followed by a double quote ".
1079/// d-char-sequence:
1080/// d-char
1081/// d-char-sequence d-char
1082/// d-char:
1083/// any member of the basic source character set except:
1084/// space, the left parenthesis (, the right parenthesis ),
1085/// the backslash \, and the control characters representing horizontal
1086/// tab, vertical tab, form feed, and newline.
1087/// escape-sequence: [C++0x lex.ccon]
1088/// simple-escape-sequence
1089/// octal-escape-sequence
1090/// hexadecimal-escape-sequence
1091/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +00001092/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +00001093/// octal-escape-sequence:
1094/// \ octal-digit
1095/// \ octal-digit octal-digit
1096/// \ octal-digit octal-digit octal-digit
1097/// hexadecimal-escape-sequence:
1098/// \x hexadecimal-digit
1099/// hexadecimal-escape-sequence hexadecimal-digit
Reid Spencer5f016e22007-07-11 17:01:13 +00001100/// universal-character-name:
1101/// \u hex-quad
1102/// \U hex-quad hex-quad
1103/// hex-quad:
1104/// hex-digit hex-digit hex-digit hex-digit
James Dennetta1263cf2012-06-19 21:04:25 +00001105/// \endverbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001106///
1107StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +00001108StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner0833dd02010-11-17 07:21:13 +00001109 Preprocessor &PP, bool Complain)
David Blaikie4e4d0842012-03-11 07:00:24 +00001110 : SM(PP.getSourceManager()), Features(PP.getLangOpts()),
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001111 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001112 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
1113 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
Chris Lattner0833dd02010-11-17 07:21:13 +00001114 init(StringToks, NumStringToks);
1115}
1116
1117void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001118 // The literal token may have come from an invalid source location (e.g. due
1119 // to a PCH error), in which case the token length will be 0.
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001120 if (NumStringToks == 0 || StringToks[0].getLength() < 2)
1121 return DiagnoseLexingError(SourceLocation());
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001122
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 // Scan all of the string portions, remember the max individual token length,
1124 // computing a bound on the concatenated string length, and see whether any
1125 // piece is a wide-string. If any of the string portions is a wide-string
1126 // literal, the result is a wide-string literal [C99 6.4.5p4].
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001127 assert(NumStringToks && "expected at least one token");
Sean Hunt6cf75022010-08-30 17:47:05 +00001128 MaxTokenLength = StringToks[0].getLength();
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001129 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +00001130 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Douglas Gregor5cee1192011-07-27 05:40:30 +00001131 Kind = StringToks[0].getKind();
Sean Hunt6cf75022010-08-30 17:47:05 +00001132
1133 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001134
1135 // Implement Translation Phase #6: concatenation of string literals
1136 /// (C99 5.1.1.2p1). The common case is only one string fragment.
1137 for (unsigned i = 1; i != NumStringToks; ++i) {
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001138 if (StringToks[i].getLength() < 2)
1139 return DiagnoseLexingError(StringToks[i].getLocation());
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 // The string could be shorter than this if it needs cleaning, but this is a
1142 // reasonable bound, which is all we need.
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001143 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +00001144 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +00001147 if (StringToks[i].getLength() > MaxTokenLength)
1148 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregor5cee1192011-07-27 05:40:30 +00001150 // Remember if we see any wide or utf-8/16/32 strings.
1151 // Also check for illegal concatenations.
1152 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
1153 if (isAscii()) {
1154 Kind = StringToks[i].getKind();
1155 } else {
1156 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +00001157 Diags->Report(StringToks[i].getLocation(),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001158 diag::err_unsupported_string_concat);
1159 hadError = true;
1160 }
1161 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +00001163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 // Include space for the null terminator.
1165 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor5cee1192011-07-27 05:40:30 +00001169 // Get the width in bytes of char/wchar_t/char16_t/char32_t
1170 CharByteWidth = getCharWidth(Kind, Target);
1171 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
1172 CharByteWidth /= 8;
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 // The output buffer size needs to be large enough to hold wide characters.
1175 // This is a worst-case assumption which basically corresponds to L"" "long".
Douglas Gregor5cee1192011-07-27 05:40:30 +00001176 SizeBound *= CharByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 // Size the temporary buffer to hold the result string data.
1179 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 // Likewise, but for each string piece.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001182 SmallString<512> TokenBuf;
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 // Loop over all the strings, getting their spelling, and expanding them to
1186 // wide strings as appropriate.
1187 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Anders Carlssonee98ac52007-10-15 02:50:23 +00001189 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001191 SourceLocation UDSuffixTokLoc;
1192
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
1194 const char *ThisTokBuf = &TokenBuf[0];
1195 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1196 // that ThisTokBuf points to a buffer that is big enough for the whole token
1197 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001198 bool StringInvalid = false;
Chris Lattner0833dd02010-11-17 07:21:13 +00001199 unsigned ThisTokLen =
Chris Lattnerb0607272010-11-17 07:26:20 +00001200 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1201 &StringInvalid);
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001202 if (StringInvalid)
1203 return DiagnoseLexingError(StringToks[i].getLocation());
Douglas Gregor50f6af72010-03-16 05:20:39 +00001204
Richard Smith26b75c02012-03-09 22:27:51 +00001205 const char *ThisTokBegin = ThisTokBuf;
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001206 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1207
1208 // Remove an optional ud-suffix.
1209 if (ThisTokEnd[-1] != '"') {
1210 const char *UDSuffixEnd = ThisTokEnd;
1211 do {
1212 --ThisTokEnd;
1213 } while (ThisTokEnd[-1] != '"');
1214
1215 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1216
1217 if (UDSuffixBuf.empty()) {
1218 UDSuffixBuf.assign(UDSuffix);
Richard Smithdd66be72012-03-08 01:34:56 +00001219 UDSuffixToken = i;
1220 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001221 UDSuffixTokLoc = StringToks[i].getLocation();
1222 } else if (!UDSuffixBuf.equals(UDSuffix)) {
1223 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1224 // result of a concatenation involving at least one user-defined-string-
1225 // literal, all the participating user-defined-string-literals shall
1226 // have the same ud-suffix.
1227 if (Diags) {
1228 SourceLocation TokLoc = StringToks[i].getLocation();
1229 Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix)
1230 << UDSuffixBuf << UDSuffix
1231 << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc)
1232 << SourceRange(TokLoc, TokLoc);
1233 }
1234 hadError = true;
1235 }
1236 }
1237
1238 // Strip the end quote.
1239 --ThisTokEnd;
1240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Craig Topper1661d712011-08-08 06:10:39 +00001243 // Skip marker for wide or unicode strings.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001244 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 ++ThisTokBuf;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001246 // Skip 8 of u8 marker for utf8 strings.
1247 if (ThisTokBuf[0] == '8')
1248 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +00001249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Craig Topper2fa4e862011-08-11 04:06:15 +00001251 // Check for raw string
1252 if (ThisTokBuf[0] == 'R') {
1253 ThisTokBuf += 2; // skip R"
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Craig Topper2fa4e862011-08-11 04:06:15 +00001255 const char *Prefix = ThisTokBuf;
1256 while (ThisTokBuf[0] != '(')
Anders Carlssonee98ac52007-10-15 02:50:23 +00001257 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001258 ++ThisTokBuf; // skip '('
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Richard Smith49d51742012-03-08 21:59:28 +00001260 // Remove same number of characters from the end
1261 ThisTokEnd -= ThisTokBuf - Prefix;
1262 assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal");
Craig Topper2fa4e862011-08-11 04:06:15 +00001263
1264 // Copy the string over
Richard Smithe5f05882012-09-08 07:16:20 +00001265 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1266 StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf)))
1267 hadError = true;
Craig Topper2fa4e862011-08-11 04:06:15 +00001268 } else {
Argyrios Kyrtzidis07a07582012-05-03 01:01:56 +00001269 if (ThisTokBuf[0] != '"') {
1270 // The file may have come from PCH and then changed after loading the
1271 // PCH; Fail gracefully.
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001272 return DiagnoseLexingError(StringToks[i].getLocation());
Argyrios Kyrtzidis07a07582012-05-03 01:01:56 +00001273 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001274 ++ThisTokBuf; // skip "
1275
1276 // Check if this is a pascal string
1277 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1278 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1279
1280 // If the \p sequence is found in the first token, we have a pascal string
1281 // Otherwise, if we already have a pascal string, ignore the first \p
1282 if (i == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001284 Pascal = true;
1285 } else if (Pascal)
1286 ThisTokBuf += 2;
1287 }
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Craig Topper2fa4e862011-08-11 04:06:15 +00001289 while (ThisTokBuf != ThisTokEnd) {
1290 // Is this a span of non-escape characters?
1291 if (ThisTokBuf[0] != '\\') {
1292 const char *InStart = ThisTokBuf;
1293 do {
1294 ++ThisTokBuf;
1295 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1296
1297 // Copy the character span over.
Richard Smithe5f05882012-09-08 07:16:20 +00001298 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1299 StringRef(InStart, ThisTokBuf - InStart)))
1300 hadError = true;
Craig Topper2fa4e862011-08-11 04:06:15 +00001301 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001303 // Is this a Universal Character Name escape?
1304 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Richard Smith26b75c02012-03-09 22:27:51 +00001305 EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
1306 ResultPtr, hadError,
1307 FullSourceLoc(StringToks[i].getLocation(), SM),
Craig Topper2fa4e862011-08-11 04:06:15 +00001308 CharByteWidth, Diags, Features);
1309 continue;
1310 }
1311 // Otherwise, this is a non-UCN escape character. Process it.
1312 unsigned ResultChar =
Richard Smithe5f05882012-09-08 07:16:20 +00001313 ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError,
Craig Topper2fa4e862011-08-11 04:06:15 +00001314 FullSourceLoc(StringToks[i].getLocation(), SM),
Richard Smithe5f05882012-09-08 07:16:20 +00001315 CharByteWidth*8, Diags, Features);
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Eli Friedmancaf1f262011-11-02 23:06:23 +00001317 if (CharByteWidth == 4) {
1318 // FIXME: Make the type of the result buffer correct instead of
1319 // using reinterpret_cast.
1320 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr);
Nico Weber9b483df2011-11-14 05:17:37 +00001321 *ResultWidePtr = ResultChar;
Eli Friedmancaf1f262011-11-02 23:06:23 +00001322 ResultPtr += 4;
1323 } else if (CharByteWidth == 2) {
1324 // FIXME: Make the type of the result buffer correct instead of
1325 // using reinterpret_cast.
1326 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr);
Nico Weber9b483df2011-11-14 05:17:37 +00001327 *ResultWidePtr = ResultChar & 0xFFFF;
Eli Friedmancaf1f262011-11-02 23:06:23 +00001328 ResultPtr += 2;
1329 } else {
1330 assert(CharByteWidth == 1 && "Unexpected char width");
1331 *ResultPtr++ = ResultChar & 0xFF;
1332 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001333 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 }
1335 }
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001337 if (Pascal) {
Eli Friedman22508f42011-11-05 00:41:04 +00001338 if (CharByteWidth == 4) {
1339 // FIXME: Make the type of the result buffer correct instead of
1340 // using reinterpret_cast.
1341 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data());
1342 ResultWidePtr[0] = GetNumStringChars() - 1;
1343 } else if (CharByteWidth == 2) {
1344 // FIXME: Make the type of the result buffer correct instead of
1345 // using reinterpret_cast.
1346 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data());
1347 ResultWidePtr[0] = GetNumStringChars() - 1;
1348 } else {
1349 assert(CharByteWidth == 1 && "Unexpected char width");
1350 ResultBuf[0] = GetNumStringChars() - 1;
1351 }
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001352
1353 // Verify that pascal strings aren't too large.
Chris Lattner0833dd02010-11-17 07:21:13 +00001354 if (GetStringLength() > 256) {
Richard Smithe5f05882012-09-08 07:16:20 +00001355 if (Diags)
1356 Diags->Report(StringToks[0].getLocation(),
Chris Lattner0833dd02010-11-17 07:21:13 +00001357 diag::err_pascal_string_too_long)
1358 << SourceRange(StringToks[0].getLocation(),
1359 StringToks[NumStringToks-1].getLocation());
Douglas Gregor5cee1192011-07-27 05:40:30 +00001360 hadError = true;
Eli Friedman57d7dde2009-04-01 03:17:08 +00001361 return;
1362 }
Chris Lattner0833dd02010-11-17 07:21:13 +00001363 } else if (Diags) {
Douglas Gregor427c4922010-07-20 14:33:20 +00001364 // Complain if this string literal has too many characters.
Chris Lattnera95880d2010-11-17 07:12:42 +00001365 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001366
Douglas Gregor427c4922010-07-20 14:33:20 +00001367 if (GetNumStringChars() > MaxChars)
Richard Smithe5f05882012-09-08 07:16:20 +00001368 Diags->Report(StringToks[0].getLocation(),
Chris Lattner0833dd02010-11-17 07:21:13 +00001369 diag::ext_string_too_long)
Douglas Gregor427c4922010-07-20 14:33:20 +00001370 << GetNumStringChars() << MaxChars
Chris Lattnera95880d2010-11-17 07:12:42 +00001371 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregor427c4922010-07-20 14:33:20 +00001372 << SourceRange(StringToks[0].getLocation(),
1373 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001374 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001375}
Chris Lattner719e6152009-02-18 19:21:10 +00001376
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001377static const char *resyncUTF8(const char *Err, const char *End) {
1378 if (Err == End)
1379 return End;
1380 End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err);
1381 while (++Err != End && (*Err & 0xC0) == 0x80)
1382 ;
1383 return Err;
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001384}
1385
Richard Smithe5f05882012-09-08 07:16:20 +00001386/// \brief This function copies from Fragment, which is a sequence of bytes
1387/// within Tok's contents (which begin at TokBegin) into ResultPtr.
Craig Topper2fa4e862011-08-11 04:06:15 +00001388/// Performs widening for multi-byte characters.
Richard Smithe5f05882012-09-08 07:16:20 +00001389bool StringLiteralParser::CopyStringFragment(const Token &Tok,
1390 const char *TokBegin,
1391 StringRef Fragment) {
1392 const UTF8 *ErrorPtrTmp;
1393 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1394 return false;
Craig Topper2fa4e862011-08-11 04:06:15 +00001395
Eli Friedman91359302012-02-11 05:08:10 +00001396 // If we see bad encoding for unprefixed string literals, warn and
1397 // simply copy the byte values, for compatibility with gcc and older
1398 // versions of clang.
1399 bool NoErrorOnBadEncoding = isAscii();
Richard Smithe5f05882012-09-08 07:16:20 +00001400 if (NoErrorOnBadEncoding) {
1401 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1402 ResultPtr += Fragment.size();
1403 }
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001404
Richard Smithe5f05882012-09-08 07:16:20 +00001405 if (Diags) {
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001406 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1407
1408 FullSourceLoc SourceLoc(Tok.getLocation(), SM);
1409 const DiagnosticBuilder &Builder =
1410 Diag(Diags, Features, SourceLoc, TokBegin,
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001411 ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()),
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001412 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1413 : diag::err_bad_string_encoding);
1414
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001415 const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end());
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001416 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1417
Benjamin Kramer4f056ac2012-11-08 19:22:31 +00001418 // Decode into a dummy buffer.
1419 SmallString<512> Dummy;
1420 Dummy.reserve(Fragment.size() * CharByteWidth);
1421 char *Ptr = Dummy.data();
1422
David Blaikie82c6dc72012-10-30 23:22:22 +00001423 while (!Builder.hasMaxRanges() &&
Benjamin Kramer4f056ac2012-11-08 19:22:31 +00001424 !ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001425 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001426 NextStart = resyncUTF8(ErrorPtr, Fragment.end());
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001427 Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin,
1428 ErrorPtr, NextStart);
1429 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1430 }
Richard Smithe5f05882012-09-08 07:16:20 +00001431 }
Eli Friedman91359302012-02-11 05:08:10 +00001432 return !NoErrorOnBadEncoding;
1433}
Craig Topper2fa4e862011-08-11 04:06:15 +00001434
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001435void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) {
1436 hadError = true;
1437 if (Diags)
1438 Diags->Report(Loc, diag::err_lexing_string);
1439}
1440
Chris Lattner719e6152009-02-18 19:21:10 +00001441/// getOffsetOfStringByte - This function returns the offset of the
1442/// specified byte of the string data represented by Token. This handles
1443/// advancing over escape sequences in the string.
1444unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattner6c66f072010-11-17 06:46:14 +00001445 unsigned ByteNo) const {
Chris Lattner719e6152009-02-18 19:21:10 +00001446 // Get the spelling of the token.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001447 SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001448 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregor50f6af72010-03-16 05:20:39 +00001450 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001451 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerb0607272010-11-17 07:26:20 +00001452 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1453 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001454 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001455 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001456
Chris Lattner719e6152009-02-18 19:21:10 +00001457 const char *SpellingStart = SpellingPtr;
1458 const char *SpellingEnd = SpellingPtr+TokLen;
1459
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001460 // Handle UTF-8 strings just like narrow strings.
1461 if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8')
1462 SpellingPtr += 2;
1463
1464 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1465 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
1466
1467 // For raw string literals, this is easy.
1468 if (SpellingPtr[0] == 'R') {
1469 assert(SpellingPtr[1] == '"' && "Should be a raw string literal!");
1470 // Skip 'R"'.
1471 SpellingPtr += 2;
1472 while (*SpellingPtr != '(') {
1473 ++SpellingPtr;
1474 assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal");
1475 }
1476 // Skip '('.
1477 ++SpellingPtr;
1478 return SpellingPtr - SpellingStart + ByteNo;
1479 }
1480
1481 // Skip over the leading quote
Chris Lattner719e6152009-02-18 19:21:10 +00001482 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1483 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Chris Lattner719e6152009-02-18 19:21:10 +00001485 // Skip over bytes until we find the offset we're looking for.
1486 while (ByteNo) {
1487 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Chris Lattner719e6152009-02-18 19:21:10 +00001489 // Step over non-escapes simply.
1490 if (*SpellingPtr != '\\') {
1491 ++SpellingPtr;
1492 --ByteNo;
1493 continue;
1494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Chris Lattner719e6152009-02-18 19:21:10 +00001496 // Otherwise, this is an escape character. Advance over it.
1497 bool HadError = false;
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001498 if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') {
1499 const char *EscapePtr = SpellingPtr;
1500 unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd,
1501 1, Features, HadError);
1502 if (Len > ByteNo) {
1503 // ByteNo is somewhere within the escape sequence.
1504 SpellingPtr = EscapePtr;
1505 break;
1506 }
1507 ByteNo -= Len;
1508 } else {
Richard Smithe5f05882012-09-08 07:16:20 +00001509 ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError,
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001510 FullSourceLoc(Tok.getLocation(), SM),
Richard Smithe5f05882012-09-08 07:16:20 +00001511 CharByteWidth*8, Diags, Features);
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001512 --ByteNo;
1513 }
Chris Lattner719e6152009-02-18 19:21:10 +00001514 assert(!HadError && "This method isn't valid on erroneous strings");
Chris Lattner719e6152009-02-18 19:21:10 +00001515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattner719e6152009-02-18 19:21:10 +00001517 return SpellingPtr-SpellingStart;
1518}