blob: 09f4a682f05db00761f1f95166e47bd5afa6d835 [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"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.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"
Dmitri Gribenkocb5620c2013-01-30 12:06:08 +000021#include "llvm/Support/ConvertUTF.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000022#include "llvm/Support/ErrorHandling.h"
Dmitri Gribenkocb5620c2013-01-30 12:06:08 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Douglas Gregor5cee1192011-07-27 05:40:30 +000026static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
27 switch (kind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000028 default: llvm_unreachable("Unknown token type!");
Douglas Gregor5cee1192011-07-27 05:40:30 +000029 case tok::char_constant:
30 case tok::string_literal:
31 case tok::utf8_string_literal:
32 return Target.getCharWidth();
33 case tok::wide_char_constant:
34 case tok::wide_string_literal:
35 return Target.getWCharWidth();
36 case tok::utf16_char_constant:
37 case tok::utf16_string_literal:
38 return Target.getChar16Width();
39 case tok::utf32_char_constant:
40 case tok::utf32_string_literal:
41 return Target.getChar32Width();
42 }
43}
44
Seth Cantrell5bffbe52012-10-28 18:24:46 +000045static CharSourceRange MakeCharSourceRange(const LangOptions &Features,
46 FullSourceLoc TokLoc,
47 const char *TokBegin,
48 const char *TokRangeBegin,
49 const char *TokRangeEnd) {
50 SourceLocation Begin =
51 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
52 TokLoc.getManager(), Features);
53 SourceLocation End =
54 Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin,
55 TokLoc.getManager(), Features);
56 return CharSourceRange::getCharRange(Begin, End);
57}
58
Richard Smithe5f05882012-09-08 07:16:20 +000059/// \brief Produce a diagnostic highlighting some portion of a literal.
60///
61/// Emits the diagnostic \p DiagID, highlighting the range of characters from
62/// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
63/// a substring of a spelling buffer for the token beginning at \p TokBegin.
64static DiagnosticBuilder Diag(DiagnosticsEngine *Diags,
65 const LangOptions &Features, FullSourceLoc TokLoc,
66 const char *TokBegin, const char *TokRangeBegin,
67 const char *TokRangeEnd, unsigned DiagID) {
68 SourceLocation Begin =
69 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
70 TokLoc.getManager(), Features);
Seth Cantrell5bffbe52012-10-28 18:24:46 +000071 return Diags->Report(Begin, DiagID) <<
72 MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd);
Richard Smithe5f05882012-09-08 07:16:20 +000073}
74
Reid Spencer5f016e22007-07-11 17:01:13 +000075/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
76/// either a character or a string literal.
Richard Smithe5f05882012-09-08 07:16:20 +000077static unsigned ProcessCharEscape(const char *ThisTokBegin,
78 const char *&ThisTokBuf,
Reid Spencer5f016e22007-07-11 17:01:13 +000079 const char *ThisTokEnd, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +000080 FullSourceLoc Loc, unsigned CharWidth,
Richard Smithe5f05882012-09-08 07:16:20 +000081 DiagnosticsEngine *Diags,
82 const LangOptions &Features) {
83 const char *EscapeBegin = ThisTokBuf;
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // Skip the '\' char.
86 ++ThisTokBuf;
87
88 // We know that this character can't be off the end of the buffer, because
89 // that would have been \", which would not have been the end of string.
90 unsigned ResultChar = *ThisTokBuf++;
91 switch (ResultChar) {
92 // These map to themselves.
93 case '\\': case '\'': case '"': case '?': break;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 // These have fixed mappings.
96 case 'a':
97 // TODO: K&R: the meaning of '\\a' is different in traditional C
98 ResultChar = 7;
99 break;
100 case 'b':
101 ResultChar = 8;
102 break;
103 case 'e':
Chris Lattner91f54ce2010-11-17 06:26:08 +0000104 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000105 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
106 diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 ResultChar = 27;
108 break;
Eli Friedman3c548012009-06-10 01:32:39 +0000109 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +0000110 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000111 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
112 diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +0000113 ResultChar = 27;
114 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 case 'f':
116 ResultChar = 12;
117 break;
118 case 'n':
119 ResultChar = 10;
120 break;
121 case 'r':
122 ResultChar = 13;
123 break;
124 case 't':
125 ResultChar = 9;
126 break;
127 case 'v':
128 ResultChar = 11;
129 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 case 'x': { // Hex escape.
131 ResultChar = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000132 if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000133 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000134 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
Jordan Rose5209e2b2013-01-24 20:50:13 +0000135 diag::err_hex_escape_no_digits) << "x";
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 HadError = 1;
137 break;
138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 // Hex escapes are a maximal series of hex digits.
141 bool Overflow = false;
142 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
Jordan Rose728bb4c2013-01-18 22:33:58 +0000143 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000145 // About to shift out a digit?
146 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ResultChar <<= 4;
148 ResultChar |= CharVal;
149 }
150
151 // See if any bits will be truncated when evaluated as a character.
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
153 Overflow = true;
154 ResultChar &= ~0U >> (32-CharWidth);
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // Check for overflow.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000158 if (Overflow && Diags) // Too many digits to fit in
Richard Smithe5f05882012-09-08 07:16:20 +0000159 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
160 diag::warn_hex_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 break;
162 }
163 case '0': case '1': case '2': case '3':
164 case '4': case '5': case '6': case '7': {
165 // Octal escapes.
166 --ThisTokBuf;
167 ResultChar = 0;
168
169 // Octal escapes are a series of octal digits with maximum length 3.
170 // "\0123" is a two digit sequence equal to "\012" "3".
171 unsigned NumDigits = 0;
172 do {
173 ResultChar <<= 3;
174 ResultChar |= *ThisTokBuf++ - '0';
175 ++NumDigits;
176 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
177 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // Check for overflow. Reject '\777', but not L'\777'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000181 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000182 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
183 diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 ResultChar &= ~0U >> (32-CharWidth);
185 }
186 break;
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 // Otherwise, these are not valid escapes.
190 case '(': case '{': case '[': case '%':
191 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000192 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000193 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
194 diag::ext_nonstandard_escape)
195 << std::string(1, ResultChar);
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000196 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000198 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000199 break;
Richard Smithe5f05882012-09-08 07:16:20 +0000200
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000201 if (isPrintable(ResultChar))
Richard Smithe5f05882012-09-08 07:16:20 +0000202 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
203 diag::ext_unknown_escape)
204 << std::string(1, ResultChar);
Chris Lattnerac92d822008-11-22 07:23:31 +0000205 else
Richard Smithe5f05882012-09-08 07:16:20 +0000206 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
207 diag::ext_unknown_escape)
208 << "x" + llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 break;
210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 return ResultChar;
213}
214
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000215/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-10-09 00:27:47 +0000216/// return the UTF32.
Richard Smith26b75c02012-03-09 22:27:51 +0000217static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
218 const char *ThisTokEnd,
Nico Weber59705ae2010-10-09 00:27:47 +0000219 uint32_t &UcnVal, unsigned short &UcnLen,
David Blaikied6471f72011-09-25 23:23:43 +0000220 FullSourceLoc Loc, DiagnosticsEngine *Diags,
Seth Cantrellbe773522012-01-18 12:27:04 +0000221 const LangOptions &Features,
222 bool in_char_string_literal = false) {
Richard Smith26b75c02012-03-09 22:27:51 +0000223 const char *UcnBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000225 // Skip the '\u' char's.
226 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000227
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000228 if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
Chris Lattner6c66f072010-11-17 06:46:14 +0000229 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000230 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
Jordan Rose5209e2b2013-01-24 20:50:13 +0000231 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
Nico Weber59705ae2010-10-09 00:27:47 +0000232 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000233 }
Nico Weber59705ae2010-10-09 00:27:47 +0000234 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000235 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000236 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Jordan Rose728bb4c2013-01-18 22:33:58 +0000237 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000238 if (CharVal == -1) break;
239 UcnVal <<= 4;
240 UcnVal |= CharVal;
241 }
242 // If we didn't consume the proper number of digits, there is a problem.
Nico Weber59705ae2010-10-09 00:27:47 +0000243 if (UcnLenSave) {
Richard Smithe5f05882012-09-08 07:16:20 +0000244 if (Diags)
245 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
246 diag::err_ucn_escape_incomplete);
Nico Weber59705ae2010-10-09 00:27:47 +0000247 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000248 }
Richard Smith26b75c02012-03-09 22:27:51 +0000249
Seth Cantrellbe773522012-01-18 12:27:04 +0000250 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
Richard Smith26b75c02012-03-09 22:27:51 +0000251 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints
252 UcnVal > 0x10FFFF) { // maximum legal UTF32 value
Chris Lattner6c66f072010-11-17 06:46:14 +0000253 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +0000254 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
255 diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000256 return false;
257 }
Richard Smith26b75c02012-03-09 22:27:51 +0000258
259 // C++11 allows UCNs that refer to control characters and basic source
260 // characters inside character and string literals
261 if (UcnVal < 0xa0 &&
262 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, `
Richard Smith80ad52f2013-01-02 11:42:31 +0000263 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
Richard Smith26b75c02012-03-09 22:27:51 +0000264 if (Diags) {
Richard Smith26b75c02012-03-09 22:27:51 +0000265 char BasicSCSChar = UcnVal;
266 if (UcnVal >= 0x20 && UcnVal < 0x7f)
Richard Smithe5f05882012-09-08 07:16:20 +0000267 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
268 IsError ? diag::err_ucn_escape_basic_scs :
269 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
270 << StringRef(&BasicSCSChar, 1);
Richard Smith26b75c02012-03-09 22:27:51 +0000271 else
Richard Smithe5f05882012-09-08 07:16:20 +0000272 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
273 IsError ? diag::err_ucn_control_character :
274 diag::warn_cxx98_compat_literal_ucn_control_character);
Richard Smith26b75c02012-03-09 22:27:51 +0000275 }
276 if (IsError)
277 return false;
278 }
279
Richard Smithe5f05882012-09-08 07:16:20 +0000280 if (!Features.CPlusPlus && !Features.C99 && Diags)
281 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
Jordan Rosebfec9162013-01-27 20:12:04 +0000282 diag::warn_ucn_not_valid_in_c89_literal);
Richard Smithe5f05882012-09-08 07:16:20 +0000283
Nico Weber59705ae2010-10-09 00:27:47 +0000284 return true;
285}
286
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000287/// MeasureUCNEscape - Determine the number of bytes within the resulting string
288/// which this UCN will occupy.
289static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
290 const char *ThisTokEnd, unsigned CharByteWidth,
291 const LangOptions &Features, bool &HadError) {
292 // UTF-32: 4 bytes per escape.
293 if (CharByteWidth == 4)
294 return 4;
295
296 uint32_t UcnVal = 0;
297 unsigned short UcnLen = 0;
298 FullSourceLoc Loc;
299
300 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
301 UcnLen, Loc, 0, Features, true)) {
302 HadError = true;
303 return 0;
304 }
305
306 // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
307 if (CharByteWidth == 2)
308 return UcnVal <= 0xFFFF ? 2 : 4;
309
310 // UTF-8.
311 if (UcnVal < 0x80)
312 return 1;
313 if (UcnVal < 0x800)
314 return 2;
315 if (UcnVal < 0x10000)
316 return 3;
317 return 4;
318}
319
Nico Weber59705ae2010-10-09 00:27:47 +0000320/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
321/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
322/// StringLiteralParser. When we decide to implement UCN's for identifiers,
323/// we will likely rework our support for UCN's.
Richard Smith26b75c02012-03-09 22:27:51 +0000324static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
325 const char *ThisTokEnd,
Chris Lattnera95880d2010-11-17 07:12:42 +0000326 char *&ResultBuf, bool &HadError,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000327 FullSourceLoc Loc, unsigned CharByteWidth,
David Blaikied6471f72011-09-25 23:23:43 +0000328 DiagnosticsEngine *Diags,
329 const LangOptions &Features) {
Nico Weber59705ae2010-10-09 00:27:47 +0000330 typedef uint32_t UTF32;
331 UTF32 UcnVal = 0;
332 unsigned short UcnLen = 0;
Richard Smith26b75c02012-03-09 22:27:51 +0000333 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
334 Loc, Diags, Features, true)) {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000335 HadError = true;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000336 return;
337 }
Nico Weber59705ae2010-10-09 00:27:47 +0000338
Douglas Gregor5cee1192011-07-27 05:40:30 +0000339 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) &&
340 "only character widths of 1, 2, or 4 bytes supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000341
Douglas Gregor5cee1192011-07-27 05:40:30 +0000342 (void)UcnLen;
343 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000344
Douglas Gregor5cee1192011-07-27 05:40:30 +0000345 if (CharByteWidth == 4) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000346 // FIXME: Make the type of the result buffer correct instead of
347 // using reinterpret_cast.
348 UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf);
349 *ResultPtr = UcnVal;
350 ResultBuf += 4;
Douglas Gregor5cee1192011-07-27 05:40:30 +0000351 return;
352 }
353
354 if (CharByteWidth == 2) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000355 // FIXME: Make the type of the result buffer correct instead of
356 // using reinterpret_cast.
357 UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf);
358
Richard Smith59b26d82012-06-13 05:41:29 +0000359 if (UcnVal <= (UTF32)0xFFFF) {
Eli Friedmancaf1f262011-11-02 23:06:23 +0000360 *ResultPtr = UcnVal;
361 ResultBuf += 2;
Nico Webera0f15b02010-10-06 04:57:26 +0000362 return;
363 }
Nico Webera0f15b02010-10-06 04:57:26 +0000364
Eli Friedmancaf1f262011-11-02 23:06:23 +0000365 // Convert to UTF16.
Nico Webera0f15b02010-10-06 04:57:26 +0000366 UcnVal -= 0x10000;
Eli Friedmancaf1f262011-11-02 23:06:23 +0000367 *ResultPtr = 0xD800 + (UcnVal >> 10);
368 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
369 ResultBuf += 4;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000370 return;
371 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000372
373 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
374
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000375 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
376 // The conversion below was inspired by:
377 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000378 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000379 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000380
381 unsigned short bytesToWrite = 0;
382 if (UcnVal < (UTF32)0x80)
383 bytesToWrite = 1;
384 else if (UcnVal < (UTF32)0x800)
385 bytesToWrite = 2;
386 else if (UcnVal < (UTF32)0x10000)
387 bytesToWrite = 3;
388 else
389 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000391 const unsigned byteMask = 0xBF;
392 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000394 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000395 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000396 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000397 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000398 };
399 // Finally, we write the bytes into ResultBuf.
400 ResultBuf += bytesToWrite;
401 switch (bytesToWrite) { // note: everything falls through.
Benjamin Kramer5d704fe2012-11-08 19:22:26 +0000402 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
403 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
404 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
405 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000406 }
407 // Update the buffer.
408 ResultBuf += bytesToWrite;
409}
Reid Spencer5f016e22007-07-11 17:01:13 +0000410
411
412/// integer-constant: [C99 6.4.4.1]
413/// decimal-constant integer-suffix
414/// octal-constant integer-suffix
415/// hexadecimal-constant integer-suffix
Richard Smith49d51742012-03-08 21:59:28 +0000416/// user-defined-integer-literal: [C++11 lex.ext]
Richard Smithb453ad32012-03-08 08:45:32 +0000417/// decimal-literal ud-suffix
418/// octal-literal ud-suffix
419/// hexadecimal-literal ud-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000420/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000421/// nonzero-digit
422/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000423/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000424/// 0
425/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000426/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000427/// hexadecimal-prefix hexadecimal-digit
428/// hexadecimal-constant hexadecimal-digit
429/// hexadecimal-prefix: one of
430/// 0x 0X
431/// integer-suffix:
432/// unsigned-suffix [long-suffix]
433/// unsigned-suffix [long-long-suffix]
434/// long-suffix [unsigned-suffix]
435/// long-long-suffix [unsigned-sufix]
436/// nonzero-digit:
437/// 1 2 3 4 5 6 7 8 9
438/// octal-digit:
439/// 0 1 2 3 4 5 6 7
440/// hexadecimal-digit:
441/// 0 1 2 3 4 5 6 7 8 9
442/// a b c d e f
443/// A B C D E F
444/// unsigned-suffix: one of
445/// u U
446/// long-suffix: one of
447/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000448/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000449/// ll LL
450///
451/// floating-constant: [C99 6.4.4.2]
452/// TODO: add rules...
453///
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000454NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
455 SourceLocation TokLoc,
456 Preprocessor &PP)
457 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000459 // This routine assumes that the range begin/end matches the regex for integer
460 // and FP constants (specifically, the 'pp-number' regex), and assumes that
461 // the byte at "*end" is both valid and not part of the regex. Because of
462 // this, it doesn't have to check for 'overscan' in various places.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000463 assert(!isPreprocessingNumberBody(*ThisTokEnd) && "didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000465 s = DigitsBegin = ThisTokBegin;
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 saw_exponent = false;
467 saw_period = false;
Richard Smithb453ad32012-03-08 08:45:32 +0000468 saw_ud_suffix = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 isLong = false;
470 isUnsigned = false;
471 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000472 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000473 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000474 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000478 ParseNumberStartingWithZero(TokLoc);
479 if (hadError)
480 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 } else { // the first digit is non-zero
482 radix = 10;
483 s = SkipDigits(s);
484 if (s == ThisTokEnd) {
485 // Done.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000486 } else if (isHexDigit(*s) && !(*s == 'e' || *s == 'E')) {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000487 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000488 diag::err_invalid_decimal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000489 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 return;
491 } else if (*s == '.') {
492 s++;
493 saw_period = true;
494 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000495 }
Chris Lattner4411f462008-09-29 23:12:31 +0000496 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000497 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 s++;
499 saw_exponent = true;
500 if (*s == '+' || *s == '-') s++; // sign
501 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000502 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000504 } else {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000505 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000506 diag::err_exponent_has_no_digits);
507 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000508 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 }
510 }
511 }
512
513 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner506b8de2007-08-26 01:58:14 +0000515 // Parse the suffix. At this point we can classify whether we have an FP or
516 // integer constant.
517 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner506b8de2007-08-26 01:58:14 +0000519 // Loop over all of the characters of the suffix. If we see something bad,
520 // we break out of the loop.
521 for (; s != ThisTokEnd; ++s) {
522 switch (*s) {
523 case 'f': // FP Suffix for "float"
524 case 'F':
525 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000526 if (isFloat || isLong) break; // FF, LF invalid.
527 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000528 continue; // Success.
529 case 'u':
530 case 'U':
531 if (isFPConstant) break; // Error for floating constant.
532 if (isUnsigned) break; // Cannot be repeated.
533 isUnsigned = true;
534 continue; // Success.
535 case 'l':
536 case 'L':
537 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000538 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner506b8de2007-08-26 01:58:14 +0000540 // Check for long long. The L's need to be adjacent and the same case.
541 if (s+1 != ThisTokEnd && s[1] == s[0]) {
542 if (isFPConstant) break; // long long invalid for floats.
543 isLongLong = true;
544 ++s; // Eat both of them.
545 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000548 continue; // Success.
549 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000550 case 'I':
David Blaikie4e4d0842012-03-11 07:00:24 +0000551 if (PP.getLangOpts().MicrosoftExt) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000552 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000553
Steve Naroff0c29b222008-04-04 21:02:54 +0000554 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000555 if (s + 1 != ThisTokEnd) {
556 switch (s[1]) {
557 case '8':
558 s += 2; // i8 suffix
559 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000560 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000561 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000562 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000563 if (s[2] == '6') {
564 s += 3; // i16 suffix
565 isMicrosoftInteger = true;
566 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000567 else if (s[2] == '2') {
568 if (s + 3 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000569 if (s[3] == '8') {
570 s += 4; // i128 suffix
571 isMicrosoftInteger = true;
572 }
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000573 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000574 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000575 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000576 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000577 if (s[2] == '2') {
578 s += 3; // i32 suffix
579 isLong = true;
580 isMicrosoftInteger = true;
581 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000582 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000583 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000584 if (s + 2 == ThisTokEnd) break;
Francois Pichetd062b602011-01-11 11:57:53 +0000585 if (s[2] == '4') {
586 s += 3; // i64 suffix
587 isLongLong = true;
588 isMicrosoftInteger = true;
589 }
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000590 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000591 default:
592 break;
593 }
594 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000595 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000596 }
597 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000598 case 'j':
599 case 'J':
600 if (isImaginary) break; // Cannot be repeated.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000601 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Chris Lattner506b8de2007-08-26 01:58:14 +0000602 diag::ext_imaginary_constant);
603 isImaginary = true;
604 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 }
Richard Smithb453ad32012-03-08 08:45:32 +0000606 // If we reached here, there was an error or a ud-suffix.
Chris Lattner506b8de2007-08-26 01:58:14 +0000607 break;
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Chris Lattner506b8de2007-08-26 01:58:14 +0000610 if (s != ThisTokEnd) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000611 if (PP.getLangOpts().CPlusPlus11 && s == SuffixBegin && *s == '_') {
Richard Smithb453ad32012-03-08 08:45:32 +0000612 // We have a ud-suffix! By C++11 [lex.ext]p10, ud-suffixes not starting
613 // with an '_' are ill-formed.
614 saw_ud_suffix = true;
615 return;
616 }
617
618 // Report an error if there are any.
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000619 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000620 isFPConstant ? diag::err_invalid_suffix_float_constant :
621 diag::err_invalid_suffix_integer_constant)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000622 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000623 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000624 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 }
626}
627
Chris Lattner368328c2008-06-30 06:39:54 +0000628/// ParseNumberStartingWithZero - This method is called when the first character
629/// of the number is found to be a zero. This means it is either an octal
630/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000631/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000632/// radix etc.
633void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
634 assert(s[0] == '0' && "Invalid method call");
635 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Chris Lattner368328c2008-06-30 06:39:54 +0000637 // Handle a hex number like 0x1234.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000638 if ((*s == 'x' || *s == 'X') && (isHexDigit(s[1]) || s[1] == '.')) {
Chris Lattner368328c2008-06-30 06:39:54 +0000639 s++;
640 radix = 16;
641 DigitsBegin = s;
642 s = SkipHexDigits(s);
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000643 bool noSignificand = (s == DigitsBegin);
Chris Lattner368328c2008-06-30 06:39:54 +0000644 if (s == ThisTokEnd) {
645 // Done.
646 } else if (*s == '.') {
647 s++;
648 saw_period = true;
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000649 const char *floatDigitsBegin = s;
Chris Lattner368328c2008-06-30 06:39:54 +0000650 s = SkipHexDigits(s);
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000651 noSignificand &= (floatDigitsBegin == s);
Chris Lattner368328c2008-06-30 06:39:54 +0000652 }
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000653
654 if (noSignificand) {
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000655 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
Aaron Ballman66b0eba2012-02-08 13:36:33 +0000656 diag::err_hexconstant_requires_digits);
657 hadError = true;
658 return;
659 }
660
Chris Lattner368328c2008-06-30 06:39:54 +0000661 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000662 // binary exponent is required.
Douglas Gregor1155c422011-08-30 22:40:35 +0000663 if (*s == 'p' || *s == 'P') {
Chris Lattner368328c2008-06-30 06:39:54 +0000664 const char *Exponent = s;
665 s++;
666 saw_exponent = true;
667 if (*s == '+' || *s == '-') s++; // sign
668 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000669 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000670 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
671 diag::err_exponent_has_no_digits);
672 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000673 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000674 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000675 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000676
David Blaikie4e4d0842012-03-11 07:00:24 +0000677 if (!PP.getLangOpts().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000678 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000679 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000680 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
681 diag::err_hexconstant_requires_exponent);
682 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000683 }
684 return;
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Chris Lattner368328c2008-06-30 06:39:54 +0000687 // Handle simple binary numbers 0b01010
688 if (*s == 'b' || *s == 'B') {
Richard Smith2fcf0de2013-04-19 20:47:20 +0000689 // 0b101010 is a C++1y / GCC extension.
690 PP.Diag(TokLoc,
691 PP.getLangOpts().CPlusPlus1y
692 ? diag::warn_cxx11_compat_binary_literal
693 : PP.getLangOpts().CPlusPlus
694 ? diag::ext_binary_literal_cxx1y
695 : diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000696 ++s;
697 radix = 2;
698 DigitsBegin = s;
699 s = SkipBinaryDigits(s);
700 if (s == ThisTokEnd) {
701 // Done.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000702 } else if (isHexDigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000703 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000704 diag::err_invalid_binary_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000705 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000706 }
Chris Lattner413d3552008-06-30 06:44:49 +0000707 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000708 return;
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattner368328c2008-06-30 06:39:54 +0000711 // For now, the radix is set to 8. If we discover that we have a
712 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000713 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000714 radix = 8;
715 DigitsBegin = s;
716 s = SkipOctalDigits(s);
717 if (s == ThisTokEnd)
718 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattner413d3552008-06-30 06:44:49 +0000720 // If we have some other non-octal digit that *is* a decimal digit, see if
721 // this is part of a floating point number like 094.123 or 09e1.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000722 if (isDigit(*s)) {
Chris Lattner413d3552008-06-30 06:44:49 +0000723 const char *EndDecimal = SkipDigits(s);
724 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
725 s = EndDecimal;
726 radix = 10;
727 }
728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Chris Lattner413d3552008-06-30 06:44:49 +0000730 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
731 // the code is using an incorrect base.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000732 if (isHexDigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000733 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner5f9e2722011-07-23 10:55:15 +0000734 diag::err_invalid_octal_digit) << StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000735 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000736 return;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattner368328c2008-06-30 06:39:54 +0000739 if (*s == '.') {
740 s++;
741 radix = 10;
742 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000743 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000744 }
745 if (*s == 'e' || *s == 'E') { // exponent
746 const char *Exponent = s;
747 s++;
748 radix = 10;
749 saw_exponent = true;
750 if (*s == '+' || *s == '-') s++; // sign
751 const char *first_non_digit = SkipDigits(s);
752 if (first_non_digit != s) {
753 s = first_non_digit;
754 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000755 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000756 diag::err_exponent_has_no_digits);
757 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000758 return;
759 }
760 }
761}
762
Jordan Rose2fd69562012-09-25 22:32:51 +0000763static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) {
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000764 switch (Radix) {
765 case 2:
766 return NumDigits <= 64;
767 case 8:
768 return NumDigits <= 64 / 3; // Digits are groups of 3 bits.
769 case 10:
770 return NumDigits <= 19; // floor(log10(2^64))
771 case 16:
772 return NumDigits <= 64 / 4; // Digits are groups of 4 bits.
773 default:
774 llvm_unreachable("impossible Radix");
775 }
776}
Chris Lattner368328c2008-06-30 06:39:54 +0000777
Reid Spencer5f016e22007-07-11 17:01:13 +0000778/// GetIntegerValue - Convert this numeric literal value to an APInt that
779/// matches Val's input width. If there is an overflow, set Val to the low bits
780/// of the result and return true. Otherwise, return false.
781bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000782 // Fast path: Compute a conservative bound on the maximum number of
783 // bits per digit in this radix. If we can't possibly overflow a
784 // uint64 based on that bound then do the simple conversion to
785 // integer. This avoids the expensive overflow checking below, and
786 // handles the common cases that matter (small decimal integers and
787 // hex/octal values which don't overflow).
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000788 const unsigned NumDigits = SuffixBegin - DigitsBegin;
Jordan Rose2fd69562012-09-25 22:32:51 +0000789 if (alwaysFitsInto64Bits(radix, NumDigits)) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000790 uint64_t N = 0;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000791 for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
Jordan Rose728bb4c2013-01-18 22:33:58 +0000792 N = N * radix + llvm::hexDigitValue(*Ptr);
Daniel Dunbara179be32008-10-16 07:32:01 +0000793
794 // This will truncate the value to Val's input width. Simply check
795 // for overflow by comparing.
796 Val = N;
797 return Val.getZExtValue() != N;
798 }
799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 Val = 0;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000801 const char *Ptr = DigitsBegin;
Reid Spencer5f016e22007-07-11 17:01:13 +0000802
803 llvm::APInt RadixVal(Val.getBitWidth(), radix);
804 llvm::APInt CharVal(Val.getBitWidth(), 0);
805 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 bool OverflowOccurred = false;
Dmitri Gribenko191046d2012-09-25 19:09:15 +0000808 while (Ptr < SuffixBegin) {
Jordan Rose728bb4c2013-01-18 22:33:58 +0000809 unsigned C = llvm::hexDigitValue(*Ptr++);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // If this letter is out of bound for this radix, reject it.
812 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // Add the digit to the value in the appropriate radix. If adding in digits
817 // made the value smaller, then this overflowed.
818 OldVal = Val;
819
820 // Multiply by radix, did overflow occur on the multiply?
821 Val *= RadixVal;
822 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
823
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000825 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 OverflowOccurred |= Val.ult(CharVal);
828 }
829 return OverflowOccurred;
830}
831
John McCall94c939d2009-12-24 09:08:04 +0000832llvm::APFloat::opStatus
833NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000834 using llvm::APFloat;
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000836 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000837 return Result.convertFromString(StringRef(ThisTokBegin, n),
838 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000839}
840
Reid Spencer5f016e22007-07-11 17:01:13 +0000841
James Dennett58f9ce12012-06-17 03:34:42 +0000842/// \verbatim
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000843/// user-defined-character-literal: [C++11 lex.ext]
844/// character-literal ud-suffix
845/// ud-suffix:
846/// identifier
847/// character-literal: [C++11 lex.ccon]
Craig Topper2fa4e862011-08-11 04:06:15 +0000848/// ' c-char-sequence '
849/// u' c-char-sequence '
850/// U' c-char-sequence '
851/// L' c-char-sequence '
852/// c-char-sequence:
853/// c-char
854/// c-char-sequence c-char
855/// c-char:
856/// any member of the source character set except the single-quote ',
857/// backslash \, or new-line character
858/// escape-sequence
859/// universal-character-name
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000860/// escape-sequence:
Craig Topper2fa4e862011-08-11 04:06:15 +0000861/// simple-escape-sequence
862/// octal-escape-sequence
863/// hexadecimal-escape-sequence
864/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +0000865/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +0000866/// octal-escape-sequence:
867/// \ octal-digit
868/// \ octal-digit octal-digit
869/// \ octal-digit octal-digit octal-digit
870/// hexadecimal-escape-sequence:
871/// \x hexadecimal-digit
872/// hexadecimal-escape-sequence hexadecimal-digit
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000873/// universal-character-name: [C++11 lex.charset]
Craig Topper2fa4e862011-08-11 04:06:15 +0000874/// \u hex-quad
875/// \U hex-quad hex-quad
876/// hex-quad:
877/// hex-digit hex-digit hex-digit hex-digit
James Dennett58f9ce12012-06-17 03:34:42 +0000878/// \endverbatim
Craig Topper2fa4e862011-08-11 04:06:15 +0000879///
Reid Spencer5f016e22007-07-11 17:01:13 +0000880CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000881 SourceLocation Loc, Preprocessor &PP,
882 tok::TokenKind kind) {
Seth Cantrellbe773522012-01-18 12:27:04 +0000883 // At this point we know that the character matches the regex "(L|u|U)?'.*'".
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregor5cee1192011-07-27 05:40:30 +0000886 Kind = kind;
887
Richard Smith26b75c02012-03-09 22:27:51 +0000888 const char *TokBegin = begin;
889
Seth Cantrellbe773522012-01-18 12:27:04 +0000890 // Skip over wide character determinant.
891 if (Kind != tok::char_constant) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000892 ++begin;
893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 // Skip over the entry quote.
896 assert(begin[0] == '\'' && "Invalid token lexed");
897 ++begin;
898
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000899 // Remove an optional ud-suffix.
900 if (end[-1] != '\'') {
901 const char *UDSuffixEnd = end;
902 do {
903 --end;
904 } while (end[-1] != '\'');
905 UDSuffixBuf.assign(end, UDSuffixEnd);
Richard Smith26b75c02012-03-09 22:27:51 +0000906 UDSuffixOffset = end - TokBegin;
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000907 }
908
Seth Cantrellbe773522012-01-18 12:27:04 +0000909 // Trim the ending quote.
Richard Smith5cc2c6e2012-03-05 04:02:15 +0000910 assert(end != begin && "Invalid token lexed");
Seth Cantrellbe773522012-01-18 12:27:04 +0000911 --end;
912
Mike Stump1eb44332009-09-09 15:08:12 +0000913 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000914 // up to 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000916 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000918 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
919 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
920 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
921 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
922 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000923
Seth Cantrellbe773522012-01-18 12:27:04 +0000924 SmallVector<uint32_t,4> codepoint_buffer;
925 codepoint_buffer.resize(end-begin);
926 uint32_t *buffer_begin = &codepoint_buffer.front();
927 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Seth Cantrellbe773522012-01-18 12:27:04 +0000929 // Unicode escapes representing characters that cannot be correctly
930 // represented in a single code unit are disallowed in character literals
931 // by this implementation.
932 uint32_t largest_character_for_kind;
933 if (tok::wide_char_constant == Kind) {
934 largest_character_for_kind = 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
935 } else if (tok::utf16_char_constant == Kind) {
936 largest_character_for_kind = 0xFFFF;
937 } else if (tok::utf32_char_constant == Kind) {
938 largest_character_for_kind = 0x10FFFF;
939 } else {
940 largest_character_for_kind = 0x7Fu;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000941 }
942
Seth Cantrellbe773522012-01-18 12:27:04 +0000943 while (begin!=end) {
944 // Is this a span of non-escape characters?
945 if (begin[0] != '\\') {
946 char const *start = begin;
947 do {
948 ++begin;
949 } while (begin != end && *begin != '\\');
950
Eli Friedman91359302012-02-11 05:08:10 +0000951 char const *tmp_in_start = start;
952 uint32_t *tmp_out_start = buffer_begin;
Seth Cantrellbe773522012-01-18 12:27:04 +0000953 ConversionResult res =
954 ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
955 reinterpret_cast<UTF8 const *>(begin),
956 &buffer_begin,buffer_end,strictConversion);
957 if (res!=conversionOK) {
Eli Friedman91359302012-02-11 05:08:10 +0000958 // If we see bad encoding for unprefixed character literals, warn and
959 // simply copy the byte values, for compatibility with gcc and
960 // older versions of clang.
961 bool NoErrorOnBadEncoding = isAscii();
962 unsigned Msg = diag::err_bad_character_encoding;
963 if (NoErrorOnBadEncoding)
964 Msg = diag::warn_bad_character_encoding;
965 PP.Diag(Loc, Msg);
966 if (NoErrorOnBadEncoding) {
967 start = tmp_in_start;
968 buffer_begin = tmp_out_start;
969 for ( ; start != begin; ++start, ++buffer_begin)
970 *buffer_begin = static_cast<uint8_t>(*start);
971 } else {
972 HadError = true;
973 }
Seth Cantrellbe773522012-01-18 12:27:04 +0000974 } else {
Eli Friedman91359302012-02-11 05:08:10 +0000975 for (; tmp_out_start <buffer_begin; ++tmp_out_start) {
976 if (*tmp_out_start > largest_character_for_kind) {
Seth Cantrellbe773522012-01-18 12:27:04 +0000977 HadError = true;
978 PP.Diag(Loc, diag::err_character_too_large);
979 }
980 }
981 }
982
983 continue;
984 }
985 // Is this a Universal Character Name excape?
986 if (begin[1] == 'u' || begin[1] == 'U') {
987 unsigned short UcnLen = 0;
Richard Smith26b75c02012-03-09 22:27:51 +0000988 if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen,
Seth Cantrellbe773522012-01-18 12:27:04 +0000989 FullSourceLoc(Loc, PP.getSourceManager()),
David Blaikie4e4d0842012-03-11 07:00:24 +0000990 &PP.getDiagnostics(), PP.getLangOpts(),
Seth Cantrellbe773522012-01-18 12:27:04 +0000991 true))
992 {
993 HadError = true;
994 } else if (*buffer_begin > largest_character_for_kind) {
995 HadError = true;
Richard Smithe5f05882012-09-08 07:16:20 +0000996 PP.Diag(Loc, diag::err_character_too_large);
Seth Cantrellbe773522012-01-18 12:27:04 +0000997 }
998
999 ++buffer_begin;
1000 continue;
1001 }
1002 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
1003 uint64_t result =
Richard Smithe5f05882012-09-08 07:16:20 +00001004 ProcessCharEscape(TokBegin, begin, end, HadError,
1005 FullSourceLoc(Loc,PP.getSourceManager()),
1006 CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
Seth Cantrellbe773522012-01-18 12:27:04 +00001007 *buffer_begin++ = result;
1008 }
1009
1010 unsigned NumCharsSoFar = buffer_begin-&codepoint_buffer.front();
1011
Chris Lattnere3ad8812009-04-28 21:51:46 +00001012 if (NumCharsSoFar > 1) {
Seth Cantrellbe773522012-01-18 12:27:04 +00001013 if (isWide())
Douglas Gregor5cee1192011-07-27 05:40:30 +00001014 PP.Diag(Loc, diag::warn_extraneous_char_constant);
Seth Cantrellbe773522012-01-18 12:27:04 +00001015 else if (isAscii() && NumCharsSoFar == 4)
1016 PP.Diag(Loc, diag::ext_four_char_character_literal);
1017 else if (isAscii())
Chris Lattnere3ad8812009-04-28 21:51:46 +00001018 PP.Diag(Loc, diag::ext_multichar_character_literal);
1019 else
Seth Cantrellbe773522012-01-18 12:27:04 +00001020 PP.Diag(Loc, diag::err_multichar_utf_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +00001021 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +00001022 } else
1023 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +00001024
Seth Cantrellbe773522012-01-18 12:27:04 +00001025 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
1026
1027 // Narrow character literals act as though their value is concatenated
1028 // in this implementation, but warn on overflow.
1029 bool multi_char_too_long = false;
1030 if (isAscii() && isMultiChar()) {
1031 LitVal = 0;
1032 for (size_t i=0;i<NumCharsSoFar;++i) {
1033 // check for enough leading zeros to shift into
1034 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1035 LitVal <<= 8;
1036 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1037 }
1038 } else if (NumCharsSoFar > 0) {
1039 // otherwise just take the last character
1040 LitVal = buffer_begin[-1];
1041 }
1042
1043 if (!HadError && multi_char_too_long) {
1044 PP.Diag(Loc,diag::warn_char_constant_too_large);
1045 }
1046
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +00001047 // Transfer the value from APInt to uint64_t
1048 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1051 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
1052 // character constants are not sign extended in the this implementation:
1053 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001054 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001055 PP.getLangOpts().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 Value = (signed char)Value;
1057}
1058
James Dennetta1263cf2012-06-19 21:04:25 +00001059/// \verbatim
Craig Topper2fa4e862011-08-11 04:06:15 +00001060/// string-literal: [C++0x lex.string]
1061/// encoding-prefix " [s-char-sequence] "
1062/// encoding-prefix R raw-string
1063/// encoding-prefix:
1064/// u8
1065/// u
1066/// U
1067/// L
Reid Spencer5f016e22007-07-11 17:01:13 +00001068/// s-char-sequence:
1069/// s-char
1070/// s-char-sequence s-char
1071/// s-char:
Craig Topper2fa4e862011-08-11 04:06:15 +00001072/// any member of the source character set except the double-quote ",
1073/// backslash \, or new-line character
1074/// escape-sequence
Reid Spencer5f016e22007-07-11 17:01:13 +00001075/// universal-character-name
Craig Topper2fa4e862011-08-11 04:06:15 +00001076/// raw-string:
1077/// " d-char-sequence ( r-char-sequence ) d-char-sequence "
1078/// r-char-sequence:
1079/// r-char
1080/// r-char-sequence r-char
1081/// r-char:
1082/// any member of the source character set, except a right parenthesis )
1083/// followed by the initial d-char-sequence (which may be empty)
1084/// followed by a double quote ".
1085/// d-char-sequence:
1086/// d-char
1087/// d-char-sequence d-char
1088/// d-char:
1089/// any member of the basic source character set except:
1090/// space, the left parenthesis (, the right parenthesis ),
1091/// the backslash \, and the control characters representing horizontal
1092/// tab, vertical tab, form feed, and newline.
1093/// escape-sequence: [C++0x lex.ccon]
1094/// simple-escape-sequence
1095/// octal-escape-sequence
1096/// hexadecimal-escape-sequence
1097/// simple-escape-sequence:
NAKAMURA Takumiddddd482011-08-12 05:49:51 +00001098/// one of \' \" \? \\ \a \b \f \n \r \t \v
Craig Topper2fa4e862011-08-11 04:06:15 +00001099/// octal-escape-sequence:
1100/// \ octal-digit
1101/// \ octal-digit octal-digit
1102/// \ octal-digit octal-digit octal-digit
1103/// hexadecimal-escape-sequence:
1104/// \x hexadecimal-digit
1105/// hexadecimal-escape-sequence hexadecimal-digit
Reid Spencer5f016e22007-07-11 17:01:13 +00001106/// universal-character-name:
1107/// \u hex-quad
1108/// \U hex-quad hex-quad
1109/// hex-quad:
1110/// hex-digit hex-digit hex-digit hex-digit
James Dennetta1263cf2012-06-19 21:04:25 +00001111/// \endverbatim
Reid Spencer5f016e22007-07-11 17:01:13 +00001112///
1113StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +00001114StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner0833dd02010-11-17 07:21:13 +00001115 Preprocessor &PP, bool Complain)
David Blaikie4e4d0842012-03-11 07:00:24 +00001116 : SM(PP.getSourceManager()), Features(PP.getLangOpts()),
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001117 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001118 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
1119 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
Chris Lattner0833dd02010-11-17 07:21:13 +00001120 init(StringToks, NumStringToks);
1121}
1122
1123void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001124 // The literal token may have come from an invalid source location (e.g. due
1125 // to a PCH error), in which case the token length will be 0.
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001126 if (NumStringToks == 0 || StringToks[0].getLength() < 2)
1127 return DiagnoseLexingError(SourceLocation());
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 // Scan all of the string portions, remember the max individual token length,
1130 // computing a bound on the concatenated string length, and see whether any
1131 // piece is a wide-string. If any of the string portions is a wide-string
1132 // literal, the result is a wide-string literal [C99 6.4.5p4].
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001133 assert(NumStringToks && "expected at least one token");
Sean Hunt6cf75022010-08-30 17:47:05 +00001134 MaxTokenLength = StringToks[0].getLength();
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001135 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +00001136 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Douglas Gregor5cee1192011-07-27 05:40:30 +00001137 Kind = StringToks[0].getKind();
Sean Hunt6cf75022010-08-30 17:47:05 +00001138
1139 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001140
1141 // Implement Translation Phase #6: concatenation of string literals
1142 /// (C99 5.1.1.2p1). The common case is only one string fragment.
1143 for (unsigned i = 1; i != NumStringToks; ++i) {
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001144 if (StringToks[i].getLength() < 2)
1145 return DiagnoseLexingError(StringToks[i].getLocation());
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // The string could be shorter than this if it needs cleaning, but this is a
1148 // reasonable bound, which is all we need.
Argyrios Kyrtzidis403de3f2011-05-17 22:09:56 +00001149 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
Sean Hunt6cf75022010-08-30 17:47:05 +00001150 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +00001153 if (StringToks[i].getLength() > MaxTokenLength)
1154 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregor5cee1192011-07-27 05:40:30 +00001156 // Remember if we see any wide or utf-8/16/32 strings.
1157 // Also check for illegal concatenations.
1158 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
1159 if (isAscii()) {
1160 Kind = StringToks[i].getKind();
1161 } else {
1162 if (Diags)
Richard Smithe5f05882012-09-08 07:16:20 +00001163 Diags->Report(StringToks[i].getLocation(),
Douglas Gregor5cee1192011-07-27 05:40:30 +00001164 diag::err_unsupported_string_concat);
1165 hadError = true;
1166 }
1167 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +00001169
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 // Include space for the null terminator.
1171 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor5cee1192011-07-27 05:40:30 +00001175 // Get the width in bytes of char/wchar_t/char16_t/char32_t
1176 CharByteWidth = getCharWidth(Kind, Target);
1177 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
1178 CharByteWidth /= 8;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // The output buffer size needs to be large enough to hold wide characters.
1181 // This is a worst-case assumption which basically corresponds to L"" "long".
Douglas Gregor5cee1192011-07-27 05:40:30 +00001182 SizeBound *= CharByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 // Size the temporary buffer to hold the result string data.
1185 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 // Likewise, but for each string piece.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001188 SmallString<512> TokenBuf;
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // Loop over all the strings, getting their spelling, and expanding them to
1192 // wide strings as appropriate.
1193 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Anders Carlssonee98ac52007-10-15 02:50:23 +00001195 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001197 SourceLocation UDSuffixTokLoc;
1198
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
1200 const char *ThisTokBuf = &TokenBuf[0];
1201 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1202 // that ThisTokBuf points to a buffer that is big enough for the whole token
1203 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001204 bool StringInvalid = false;
Chris Lattner0833dd02010-11-17 07:21:13 +00001205 unsigned ThisTokLen =
Chris Lattnerb0607272010-11-17 07:26:20 +00001206 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1207 &StringInvalid);
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001208 if (StringInvalid)
1209 return DiagnoseLexingError(StringToks[i].getLocation());
Douglas Gregor50f6af72010-03-16 05:20:39 +00001210
Richard Smith26b75c02012-03-09 22:27:51 +00001211 const char *ThisTokBegin = ThisTokBuf;
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001212 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1213
1214 // Remove an optional ud-suffix.
1215 if (ThisTokEnd[-1] != '"') {
1216 const char *UDSuffixEnd = ThisTokEnd;
1217 do {
1218 --ThisTokEnd;
1219 } while (ThisTokEnd[-1] != '"');
1220
1221 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1222
1223 if (UDSuffixBuf.empty()) {
1224 UDSuffixBuf.assign(UDSuffix);
Richard Smithdd66be72012-03-08 01:34:56 +00001225 UDSuffixToken = i;
1226 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
Richard Smith5cc2c6e2012-03-05 04:02:15 +00001227 UDSuffixTokLoc = StringToks[i].getLocation();
1228 } else if (!UDSuffixBuf.equals(UDSuffix)) {
1229 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1230 // result of a concatenation involving at least one user-defined-string-
1231 // literal, all the participating user-defined-string-literals shall
1232 // have the same ud-suffix.
1233 if (Diags) {
1234 SourceLocation TokLoc = StringToks[i].getLocation();
1235 Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix)
1236 << UDSuffixBuf << UDSuffix
1237 << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc)
1238 << SourceRange(TokLoc, TokLoc);
1239 }
1240 hadError = true;
1241 }
1242 }
1243
1244 // Strip the end quote.
1245 --ThisTokEnd;
1246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Craig Topper1661d712011-08-08 06:10:39 +00001249 // Skip marker for wide or unicode strings.
Douglas Gregor5cee1192011-07-27 05:40:30 +00001250 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 ++ThisTokBuf;
Douglas Gregor5cee1192011-07-27 05:40:30 +00001252 // Skip 8 of u8 marker for utf8 strings.
1253 if (ThisTokBuf[0] == '8')
1254 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +00001255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Craig Topper2fa4e862011-08-11 04:06:15 +00001257 // Check for raw string
1258 if (ThisTokBuf[0] == 'R') {
1259 ThisTokBuf += 2; // skip R"
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Craig Topper2fa4e862011-08-11 04:06:15 +00001261 const char *Prefix = ThisTokBuf;
1262 while (ThisTokBuf[0] != '(')
Anders Carlssonee98ac52007-10-15 02:50:23 +00001263 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001264 ++ThisTokBuf; // skip '('
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Richard Smith49d51742012-03-08 21:59:28 +00001266 // Remove same number of characters from the end
1267 ThisTokEnd -= ThisTokBuf - Prefix;
1268 assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal");
Craig Topper2fa4e862011-08-11 04:06:15 +00001269
1270 // Copy the string over
Richard Smithe5f05882012-09-08 07:16:20 +00001271 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1272 StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf)))
1273 hadError = true;
Craig Topper2fa4e862011-08-11 04:06:15 +00001274 } else {
Argyrios Kyrtzidis07a07582012-05-03 01:01:56 +00001275 if (ThisTokBuf[0] != '"') {
1276 // The file may have come from PCH and then changed after loading the
1277 // PCH; Fail gracefully.
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001278 return DiagnoseLexingError(StringToks[i].getLocation());
Argyrios Kyrtzidis07a07582012-05-03 01:01:56 +00001279 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001280 ++ThisTokBuf; // skip "
1281
1282 // Check if this is a pascal string
1283 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1284 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1285
1286 // If the \p sequence is found in the first token, we have a pascal string
1287 // Otherwise, if we already have a pascal string, ignore the first \p
1288 if (i == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 ++ThisTokBuf;
Craig Topper2fa4e862011-08-11 04:06:15 +00001290 Pascal = true;
1291 } else if (Pascal)
1292 ThisTokBuf += 2;
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Craig Topper2fa4e862011-08-11 04:06:15 +00001295 while (ThisTokBuf != ThisTokEnd) {
1296 // Is this a span of non-escape characters?
1297 if (ThisTokBuf[0] != '\\') {
1298 const char *InStart = ThisTokBuf;
1299 do {
1300 ++ThisTokBuf;
1301 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1302
1303 // Copy the character span over.
Richard Smithe5f05882012-09-08 07:16:20 +00001304 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1305 StringRef(InStart, ThisTokBuf - InStart)))
1306 hadError = true;
Craig Topper2fa4e862011-08-11 04:06:15 +00001307 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001309 // Is this a Universal Character Name escape?
1310 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Richard Smith26b75c02012-03-09 22:27:51 +00001311 EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
1312 ResultPtr, hadError,
1313 FullSourceLoc(StringToks[i].getLocation(), SM),
Craig Topper2fa4e862011-08-11 04:06:15 +00001314 CharByteWidth, Diags, Features);
1315 continue;
1316 }
1317 // Otherwise, this is a non-UCN escape character. Process it.
1318 unsigned ResultChar =
Richard Smithe5f05882012-09-08 07:16:20 +00001319 ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError,
Craig Topper2fa4e862011-08-11 04:06:15 +00001320 FullSourceLoc(StringToks[i].getLocation(), SM),
Richard Smithe5f05882012-09-08 07:16:20 +00001321 CharByteWidth*8, Diags, Features);
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Eli Friedmancaf1f262011-11-02 23:06:23 +00001323 if (CharByteWidth == 4) {
1324 // FIXME: Make the type of the result buffer correct instead of
1325 // using reinterpret_cast.
1326 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr);
Nico Weber9b483df2011-11-14 05:17:37 +00001327 *ResultWidePtr = ResultChar;
Eli Friedmancaf1f262011-11-02 23:06:23 +00001328 ResultPtr += 4;
1329 } else if (CharByteWidth == 2) {
1330 // FIXME: Make the type of the result buffer correct instead of
1331 // using reinterpret_cast.
1332 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr);
Nico Weber9b483df2011-11-14 05:17:37 +00001333 *ResultWidePtr = ResultChar & 0xFFFF;
Eli Friedmancaf1f262011-11-02 23:06:23 +00001334 ResultPtr += 2;
1335 } else {
1336 assert(CharByteWidth == 1 && "Unexpected char width");
1337 *ResultPtr++ = ResultChar & 0xFF;
1338 }
Craig Topper2fa4e862011-08-11 04:06:15 +00001339 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 }
1341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001343 if (Pascal) {
Eli Friedman22508f42011-11-05 00:41:04 +00001344 if (CharByteWidth == 4) {
1345 // FIXME: Make the type of the result buffer correct instead of
1346 // using reinterpret_cast.
1347 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data());
1348 ResultWidePtr[0] = GetNumStringChars() - 1;
1349 } else if (CharByteWidth == 2) {
1350 // FIXME: Make the type of the result buffer correct instead of
1351 // using reinterpret_cast.
1352 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data());
1353 ResultWidePtr[0] = GetNumStringChars() - 1;
1354 } else {
1355 assert(CharByteWidth == 1 && "Unexpected char width");
1356 ResultBuf[0] = GetNumStringChars() - 1;
1357 }
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001358
1359 // Verify that pascal strings aren't too large.
Chris Lattner0833dd02010-11-17 07:21:13 +00001360 if (GetStringLength() > 256) {
Richard Smithe5f05882012-09-08 07:16:20 +00001361 if (Diags)
1362 Diags->Report(StringToks[0].getLocation(),
Chris Lattner0833dd02010-11-17 07:21:13 +00001363 diag::err_pascal_string_too_long)
1364 << SourceRange(StringToks[0].getLocation(),
1365 StringToks[NumStringToks-1].getLocation());
Douglas Gregor5cee1192011-07-27 05:40:30 +00001366 hadError = true;
Eli Friedman57d7dde2009-04-01 03:17:08 +00001367 return;
1368 }
Chris Lattner0833dd02010-11-17 07:21:13 +00001369 } else if (Diags) {
Douglas Gregor427c4922010-07-20 14:33:20 +00001370 // Complain if this string literal has too many characters.
Chris Lattnera95880d2010-11-17 07:12:42 +00001371 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001372
Douglas Gregor427c4922010-07-20 14:33:20 +00001373 if (GetNumStringChars() > MaxChars)
Richard Smithe5f05882012-09-08 07:16:20 +00001374 Diags->Report(StringToks[0].getLocation(),
Chris Lattner0833dd02010-11-17 07:21:13 +00001375 diag::ext_string_too_long)
Douglas Gregor427c4922010-07-20 14:33:20 +00001376 << GetNumStringChars() << MaxChars
Chris Lattnera95880d2010-11-17 07:12:42 +00001377 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregor427c4922010-07-20 14:33:20 +00001378 << SourceRange(StringToks[0].getLocation(),
1379 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001380 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001381}
Chris Lattner719e6152009-02-18 19:21:10 +00001382
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001383static const char *resyncUTF8(const char *Err, const char *End) {
1384 if (Err == End)
1385 return End;
1386 End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err);
1387 while (++Err != End && (*Err & 0xC0) == 0x80)
1388 ;
1389 return Err;
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001390}
1391
Richard Smithe5f05882012-09-08 07:16:20 +00001392/// \brief This function copies from Fragment, which is a sequence of bytes
1393/// within Tok's contents (which begin at TokBegin) into ResultPtr.
Craig Topper2fa4e862011-08-11 04:06:15 +00001394/// Performs widening for multi-byte characters.
Richard Smithe5f05882012-09-08 07:16:20 +00001395bool StringLiteralParser::CopyStringFragment(const Token &Tok,
1396 const char *TokBegin,
1397 StringRef Fragment) {
1398 const UTF8 *ErrorPtrTmp;
1399 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1400 return false;
Craig Topper2fa4e862011-08-11 04:06:15 +00001401
Eli Friedman91359302012-02-11 05:08:10 +00001402 // If we see bad encoding for unprefixed string literals, warn and
1403 // simply copy the byte values, for compatibility with gcc and older
1404 // versions of clang.
1405 bool NoErrorOnBadEncoding = isAscii();
Richard Smithe5f05882012-09-08 07:16:20 +00001406 if (NoErrorOnBadEncoding) {
1407 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1408 ResultPtr += Fragment.size();
1409 }
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001410
Richard Smithe5f05882012-09-08 07:16:20 +00001411 if (Diags) {
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001412 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1413
1414 FullSourceLoc SourceLoc(Tok.getLocation(), SM);
1415 const DiagnosticBuilder &Builder =
1416 Diag(Diags, Features, SourceLoc, TokBegin,
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001417 ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()),
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001418 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1419 : diag::err_bad_string_encoding);
1420
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001421 const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end());
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001422 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1423
Benjamin Kramer4f056ac2012-11-08 19:22:31 +00001424 // Decode into a dummy buffer.
1425 SmallString<512> Dummy;
1426 Dummy.reserve(Fragment.size() * CharByteWidth);
1427 char *Ptr = Dummy.data();
1428
David Blaikie82c6dc72012-10-30 23:22:22 +00001429 while (!Builder.hasMaxRanges() &&
Benjamin Kramer4f056ac2012-11-08 19:22:31 +00001430 !ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001431 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
Benjamin Kramer5d704fe2012-11-08 19:22:26 +00001432 NextStart = resyncUTF8(ErrorPtr, Fragment.end());
Seth Cantrell5bffbe52012-10-28 18:24:46 +00001433 Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin,
1434 ErrorPtr, NextStart);
1435 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1436 }
Richard Smithe5f05882012-09-08 07:16:20 +00001437 }
Eli Friedman91359302012-02-11 05:08:10 +00001438 return !NoErrorOnBadEncoding;
1439}
Craig Topper2fa4e862011-08-11 04:06:15 +00001440
Argyrios Kyrtzidis31447492012-05-03 17:50:32 +00001441void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) {
1442 hadError = true;
1443 if (Diags)
1444 Diags->Report(Loc, diag::err_lexing_string);
1445}
1446
Chris Lattner719e6152009-02-18 19:21:10 +00001447/// getOffsetOfStringByte - This function returns the offset of the
1448/// specified byte of the string data represented by Token. This handles
1449/// advancing over escape sequences in the string.
1450unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattner6c66f072010-11-17 06:46:14 +00001451 unsigned ByteNo) const {
Chris Lattner719e6152009-02-18 19:21:10 +00001452 // Get the spelling of the token.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001453 SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001454 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor50f6af72010-03-16 05:20:39 +00001456 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001457 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerb0607272010-11-17 07:26:20 +00001458 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1459 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001460 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001461 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001462
Chris Lattner719e6152009-02-18 19:21:10 +00001463 const char *SpellingStart = SpellingPtr;
1464 const char *SpellingEnd = SpellingPtr+TokLen;
1465
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001466 // Handle UTF-8 strings just like narrow strings.
1467 if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8')
1468 SpellingPtr += 2;
1469
1470 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1471 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
1472
1473 // For raw string literals, this is easy.
1474 if (SpellingPtr[0] == 'R') {
1475 assert(SpellingPtr[1] == '"' && "Should be a raw string literal!");
1476 // Skip 'R"'.
1477 SpellingPtr += 2;
1478 while (*SpellingPtr != '(') {
1479 ++SpellingPtr;
1480 assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal");
1481 }
1482 // Skip '('.
1483 ++SpellingPtr;
1484 return SpellingPtr - SpellingStart + ByteNo;
1485 }
1486
1487 // Skip over the leading quote
Chris Lattner719e6152009-02-18 19:21:10 +00001488 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1489 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Chris Lattner719e6152009-02-18 19:21:10 +00001491 // Skip over bytes until we find the offset we're looking for.
1492 while (ByteNo) {
1493 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Chris Lattner719e6152009-02-18 19:21:10 +00001495 // Step over non-escapes simply.
1496 if (*SpellingPtr != '\\') {
1497 ++SpellingPtr;
1498 --ByteNo;
1499 continue;
1500 }
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Chris Lattner719e6152009-02-18 19:21:10 +00001502 // Otherwise, this is an escape character. Advance over it.
1503 bool HadError = false;
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001504 if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') {
1505 const char *EscapePtr = SpellingPtr;
1506 unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd,
1507 1, Features, HadError);
1508 if (Len > ByteNo) {
1509 // ByteNo is somewhere within the escape sequence.
1510 SpellingPtr = EscapePtr;
1511 break;
1512 }
1513 ByteNo -= Len;
1514 } else {
Richard Smithe5f05882012-09-08 07:16:20 +00001515 ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError,
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001516 FullSourceLoc(Tok.getLocation(), SM),
Richard Smithe5f05882012-09-08 07:16:20 +00001517 CharByteWidth*8, Diags, Features);
Richard Smithdf9ef1b2012-06-13 05:37:23 +00001518 --ByteNo;
1519 }
Chris Lattner719e6152009-02-18 19:21:10 +00001520 assert(!HadError && "This method isn't valid on erroneous strings");
Chris Lattner719e6152009-02-18 19:21:10 +00001521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Chris Lattner719e6152009-02-18 19:21:10 +00001523 return SpellingPtr-SpellingStart;
1524}