blob: f8a2a55117cf626cbda2b4232efff34883009582 [file] [log] [blame]
Chris Lattner2f5add62007-04-05 06:57:15 +00001//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
Steve Naroff09ef4742007-03-09 23:16:33 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Steve Naroff09ef4742007-03-09 23:16:33 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner2f5add62007-04-05 06:57:15 +000010// This file implements the NumericLiteralParser, CharLiteralParser, and
11// StringLiteralParser interfaces.
Steve Naroff09ef4742007-03-09 23:16:33 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/LiteralSupport.h"
16#include "clang/Lex/Preprocessor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerbb1b44f2007-07-16 06:55:01 +000018#include "clang/Basic/TargetInfo.h"
Steve Naroff4f88b312007-03-13 22:37:02 +000019#include "llvm/ADT/StringExtras.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000020using namespace clang;
21
Chris Lattner2f5add62007-04-05 06:57:15 +000022/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
23/// not valid.
24static int HexDigitValue(char C) {
25 if (C >= '0' && C <= '9') return C-'0';
26 if (C >= 'a' && C <= 'f') return C-'a'+10;
27 if (C >= 'A' && C <= 'F') return C-'A'+10;
28 return -1;
29}
30
31/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
32/// either a character or a string literal.
33static unsigned ProcessCharEscape(const char *&ThisTokBuf,
34 const char *ThisTokEnd, bool &HadError,
Chris Lattner7a02bfd2010-11-17 06:26:08 +000035 FullSourceLoc Loc, bool IsWide,
36 Diagnostic *Diags, const TargetInfo &Target) {
Chris Lattner2f5add62007-04-05 06:57:15 +000037 // Skip the '\' char.
38 ++ThisTokBuf;
39
40 // We know that this character can't be off the end of the buffer, because
41 // that would have been \", which would not have been the end of string.
42 unsigned ResultChar = *ThisTokBuf++;
43 switch (ResultChar) {
44 // These map to themselves.
45 case '\\': case '\'': case '"': case '?': break;
Mike Stump11289f42009-09-09 15:08:12 +000046
Chris Lattner2f5add62007-04-05 06:57:15 +000047 // These have fixed mappings.
48 case 'a':
49 // TODO: K&R: the meaning of '\\a' is different in traditional C
50 ResultChar = 7;
51 break;
52 case 'b':
53 ResultChar = 8;
54 break;
55 case 'e':
Chris Lattner7a02bfd2010-11-17 06:26:08 +000056 if (Diags)
57 Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
Chris Lattner2f5add62007-04-05 06:57:15 +000058 ResultChar = 27;
59 break;
Eli Friedman28a00aa2009-06-10 01:32:39 +000060 case 'E':
Chris Lattner7a02bfd2010-11-17 06:26:08 +000061 if (Diags)
62 Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
Eli Friedman28a00aa2009-06-10 01:32:39 +000063 ResultChar = 27;
64 break;
Chris Lattner2f5add62007-04-05 06:57:15 +000065 case 'f':
66 ResultChar = 12;
67 break;
68 case 'n':
69 ResultChar = 10;
70 break;
71 case 'r':
72 ResultChar = 13;
73 break;
74 case 't':
75 ResultChar = 9;
76 break;
77 case 'v':
78 ResultChar = 11;
79 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +000080 case 'x': { // Hex escape.
81 ResultChar = 0;
82 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner7a02bfd2010-11-17 06:26:08 +000083 if (Diags)
84 Diags->Report(Loc, diag::err_hex_escape_no_digits);
Chris Lattner2f5add62007-04-05 06:57:15 +000085 HadError = 1;
Chris Lattner2f5add62007-04-05 06:57:15 +000086 break;
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner812eda82007-05-20 05:17:04 +000089 // Hex escapes are a maximal series of hex digits.
Chris Lattnerc10adde2007-05-20 05:00:58 +000090 bool Overflow = false;
91 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
92 int CharVal = HexDigitValue(ThisTokBuf[0]);
93 if (CharVal == -1) break;
Chris Lattner59f09b62008-09-30 20:45:40 +000094 // About to shift out a digit?
95 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Chris Lattnerc10adde2007-05-20 05:00:58 +000096 ResultChar <<= 4;
97 ResultChar |= CharVal;
98 }
99
100 // See if any bits will be truncated when evaluated as a character.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000101 unsigned CharWidth =
102 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnerc10adde2007-05-20 05:00:58 +0000104 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
105 Overflow = true;
106 ResultChar &= ~0U >> (32-CharWidth);
107 }
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattnerc10adde2007-05-20 05:00:58 +0000109 // Check for overflow.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000110 if (Overflow && Diags) // Too many digits to fit in
111 Diags->Report(Loc, diag::warn_hex_escape_too_large);
Chris Lattner2f5add62007-04-05 06:57:15 +0000112 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000113 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000114 case '0': case '1': case '2': case '3':
Chris Lattner812eda82007-05-20 05:17:04 +0000115 case '4': case '5': case '6': case '7': {
Chris Lattner2f5add62007-04-05 06:57:15 +0000116 // Octal escapes.
Chris Lattner3f4b6e32007-06-09 06:20:47 +0000117 --ThisTokBuf;
Chris Lattner812eda82007-05-20 05:17:04 +0000118 ResultChar = 0;
119
120 // Octal escapes are a series of octal digits with maximum length 3.
121 // "\0123" is a two digit sequence equal to "\012" "3".
122 unsigned NumDigits = 0;
123 do {
124 ResultChar <<= 3;
125 ResultChar |= *ThisTokBuf++ - '0';
126 ++NumDigits;
127 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
128 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattner812eda82007-05-20 05:17:04 +0000130 // Check for overflow. Reject '\777', but not L'\777'.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000131 unsigned CharWidth =
132 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattner812eda82007-05-20 05:17:04 +0000134 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000135 if (Diags)
136 Diags->Report(Loc, diag::warn_octal_escape_too_large);
Chris Lattner812eda82007-05-20 05:17:04 +0000137 ResultChar &= ~0U >> (32-CharWidth);
138 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000139 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000140 }
Mike Stump11289f42009-09-09 15:08:12 +0000141
Chris Lattner2f5add62007-04-05 06:57:15 +0000142 // Otherwise, these are not valid escapes.
143 case '(': case '{': case '[': case '%':
144 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000145 if (Diags)
146 Diags->Report(Loc, diag::ext_nonstandard_escape)
Douglas Gregor9af03022010-05-26 05:35:51 +0000147 << std::string()+(char)ResultChar;
Eli Friedman5d72d412009-04-28 00:51:18 +0000148 break;
Chris Lattner2f5add62007-04-05 06:57:15 +0000149 default:
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000150 if (Diags == 0)
Douglas Gregor9af03022010-05-26 05:35:51 +0000151 break;
152
Ted Kremenek8c4c74f2010-12-03 00:09:56 +0000153 if (isgraph(ResultChar))
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000154 Diags->Report(Loc, diag::ext_unknown_escape)
155 << std::string()+(char)ResultChar;
Chris Lattner59acca52008-11-22 07:23:31 +0000156 else
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000157 Diags->Report(Loc, diag::ext_unknown_escape)
158 << "x"+llvm::utohexstr(ResultChar);
Chris Lattner2f5add62007-04-05 06:57:15 +0000159 break;
160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Chris Lattner2f5add62007-04-05 06:57:15 +0000162 return ResultChar;
163}
164
Steve Naroff7b753d22009-03-30 23:46:03 +0000165/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Webera6bde812010-10-09 00:27:47 +0000166/// return the UTF32.
167static bool ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
168 uint32_t &UcnVal, unsigned short &UcnLen,
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000169 FullSourceLoc Loc, Diagnostic *Diags,
Chris Lattnerbde1b812010-11-17 06:46:14 +0000170 const LangOptions &Features) {
171 if (!Features.CPlusPlus && !Features.C99 && Diags)
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000172 Diags->Report(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump11289f42009-09-09 15:08:12 +0000173
Steve Naroffc94adda2009-04-01 11:09:15 +0000174 // Save the beginning of the string (for error diagnostics).
175 const char *ThisTokBegin = ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000176
Steve Naroff7b753d22009-03-30 23:46:03 +0000177 // Skip the '\u' char's.
178 ThisTokBuf += 2;
Chris Lattner2f5add62007-04-05 06:57:15 +0000179
Steve Naroff7b753d22009-03-30 23:46:03 +0000180 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattnerbde1b812010-11-17 06:46:14 +0000181 if (Diags)
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000182 Diags->Report(Loc, diag::err_ucn_escape_no_digits);
Nico Webera6bde812010-10-09 00:27:47 +0000183 return false;
Steve Naroff7b753d22009-03-30 23:46:03 +0000184 }
Nico Webera6bde812010-10-09 00:27:47 +0000185 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000186 unsigned short UcnLenSave = UcnLen;
Nico Webera6bde812010-10-09 00:27:47 +0000187 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff7b753d22009-03-30 23:46:03 +0000188 int CharVal = HexDigitValue(ThisTokBuf[0]);
189 if (CharVal == -1) break;
190 UcnVal <<= 4;
191 UcnVal |= CharVal;
192 }
193 // If we didn't consume the proper number of digits, there is a problem.
Nico Webera6bde812010-10-09 00:27:47 +0000194 if (UcnLenSave) {
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000195 if (Diags) {
Chris Lattner2a6ee912010-11-17 07:05:50 +0000196 SourceLocation L =
197 Lexer::AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin,
198 Loc.getManager(), Features);
199 Diags->Report(FullSourceLoc(L, Loc.getManager()),
200 diag::err_ucn_escape_incomplete);
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000201 }
Nico Webera6bde812010-10-09 00:27:47 +0000202 return false;
Steve Naroff7b753d22009-03-30 23:46:03 +0000203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff7b753d22009-03-30 23:46:03 +0000205 if ((UcnVal < 0xa0 &&
206 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump11289f42009-09-09 15:08:12 +0000207 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Narofff2a880c2009-03-31 10:29:45 +0000208 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Chris Lattnerbde1b812010-11-17 06:46:14 +0000209 if (Diags)
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000210 Diags->Report(Loc, diag::err_ucn_escape_invalid);
Nico Webera6bde812010-10-09 00:27:47 +0000211 return false;
212 }
213 return true;
214}
215
216/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
217/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
218/// StringLiteralParser. When we decide to implement UCN's for identifiers,
219/// we will likely rework our support for UCN's.
220static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
Chris Lattner2be8aa92010-11-17 07:12:42 +0000221 char *&ResultBuf, bool &HadError,
222 FullSourceLoc Loc, bool wide, Diagnostic *Diags,
223 const LangOptions &Features) {
Nico Webera6bde812010-10-09 00:27:47 +0000224 typedef uint32_t UTF32;
225 UTF32 UcnVal = 0;
226 unsigned short UcnLen = 0;
Chris Lattner2be8aa92010-11-17 07:12:42 +0000227 if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, Loc, Diags,
228 Features)) {
Steve Naroff7b753d22009-03-30 23:46:03 +0000229 HadError = 1;
230 return;
231 }
Nico Webera6bde812010-10-09 00:27:47 +0000232
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000233 if (wide) {
Nico Webera6bde812010-10-09 00:27:47 +0000234 (void)UcnLen;
Chris Lattner2be8aa92010-11-17 07:12:42 +0000235 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Weber9762e0a2010-10-06 04:57:26 +0000236
Chris Lattner2be8aa92010-11-17 07:12:42 +0000237 if (!Features.ShortWChar) {
Nico Weber9762e0a2010-10-06 04:57:26 +0000238 // Note: our internal rep of wide char tokens is always little-endian.
239 *ResultBuf++ = (UcnVal & 0x000000FF);
240 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
241 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
242 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
243 return;
244 }
245
246 // Convert to UTF16.
247 if (UcnVal < (UTF32)0xFFFF) {
248 *ResultBuf++ = (UcnVal & 0x000000FF);
249 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
250 return;
251 }
Chris Lattner2be8aa92010-11-17 07:12:42 +0000252 if (Diags) Diags->Report(Loc, diag::warn_ucn_escape_too_large);
Nico Weber9762e0a2010-10-06 04:57:26 +0000253
254 typedef uint16_t UTF16;
255 UcnVal -= 0x10000;
256 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
257 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
258 *ResultBuf++ = (surrogate1 & 0x000000FF);
259 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
260 *ResultBuf++ = (surrogate2 & 0x000000FF);
261 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000262 return;
263 }
Steve Naroff7b753d22009-03-30 23:46:03 +0000264 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
265 // The conversion below was inspired by:
266 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump11289f42009-09-09 15:08:12 +0000267 // First, we determine how many bytes the result will require.
Steve Naroffc94adda2009-04-01 11:09:15 +0000268 typedef uint8_t UTF8;
Steve Naroff7b753d22009-03-30 23:46:03 +0000269
270 unsigned short bytesToWrite = 0;
271 if (UcnVal < (UTF32)0x80)
272 bytesToWrite = 1;
273 else if (UcnVal < (UTF32)0x800)
274 bytesToWrite = 2;
275 else if (UcnVal < (UTF32)0x10000)
276 bytesToWrite = 3;
277 else
278 bytesToWrite = 4;
Mike Stump11289f42009-09-09 15:08:12 +0000279
Steve Naroff7b753d22009-03-30 23:46:03 +0000280 const unsigned byteMask = 0xBF;
281 const unsigned byteMark = 0x80;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Steve Naroff7b753d22009-03-30 23:46:03 +0000283 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Narofff2a880c2009-03-31 10:29:45 +0000284 // into the first byte, depending on how many bytes follow.
Mike Stump11289f42009-09-09 15:08:12 +0000285 static const UTF8 firstByteMark[5] = {
Steve Narofff2a880c2009-03-31 10:29:45 +0000286 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff7b753d22009-03-30 23:46:03 +0000287 };
288 // Finally, we write the bytes into ResultBuf.
289 ResultBuf += bytesToWrite;
290 switch (bytesToWrite) { // note: everything falls through.
291 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
292 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
293 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
294 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
295 }
296 // Update the buffer.
297 ResultBuf += bytesToWrite;
298}
Chris Lattner2f5add62007-04-05 06:57:15 +0000299
300
Steve Naroff09ef4742007-03-09 23:16:33 +0000301/// integer-constant: [C99 6.4.4.1]
302/// decimal-constant integer-suffix
303/// octal-constant integer-suffix
304/// hexadecimal-constant integer-suffix
Mike Stump11289f42009-09-09 15:08:12 +0000305/// decimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000306/// nonzero-digit
307/// decimal-constant digit
Mike Stump11289f42009-09-09 15:08:12 +0000308/// octal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000309/// 0
310/// octal-constant octal-digit
Mike Stump11289f42009-09-09 15:08:12 +0000311/// hexadecimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000312/// hexadecimal-prefix hexadecimal-digit
313/// hexadecimal-constant hexadecimal-digit
314/// hexadecimal-prefix: one of
315/// 0x 0X
316/// integer-suffix:
317/// unsigned-suffix [long-suffix]
318/// unsigned-suffix [long-long-suffix]
319/// long-suffix [unsigned-suffix]
320/// long-long-suffix [unsigned-sufix]
321/// nonzero-digit:
322/// 1 2 3 4 5 6 7 8 9
323/// octal-digit:
324/// 0 1 2 3 4 5 6 7
325/// hexadecimal-digit:
326/// 0 1 2 3 4 5 6 7 8 9
327/// a b c d e f
328/// A B C D E F
329/// unsigned-suffix: one of
330/// u U
331/// long-suffix: one of
332/// l L
Mike Stump11289f42009-09-09 15:08:12 +0000333/// long-long-suffix: one of
Steve Naroff09ef4742007-03-09 23:16:33 +0000334/// ll LL
335///
336/// floating-constant: [C99 6.4.4.2]
337/// TODO: add rules...
338///
Steve Naroff09ef4742007-03-09 23:16:33 +0000339NumericLiteralParser::
340NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000341 SourceLocation TokLoc, Preprocessor &pp)
342 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner59f09b62008-09-30 20:45:40 +0000344 // This routine assumes that the range begin/end matches the regex for integer
345 // and FP constants (specifically, the 'pp-number' regex), and assumes that
346 // the byte at "*end" is both valid and not part of the regex. Because of
347 // this, it doesn't have to check for 'overscan' in various places.
348 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
349 "Lexer didn't maximally munch?");
Mike Stump11289f42009-09-09 15:08:12 +0000350
Steve Naroff09ef4742007-03-09 23:16:33 +0000351 s = DigitsBegin = begin;
352 saw_exponent = false;
353 saw_period = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000354 isLong = false;
355 isUnsigned = false;
356 isLongLong = false;
Chris Lattnered045422007-08-26 03:29:23 +0000357 isFloat = false;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000358 isImaginary = false;
Mike Stumpc99c0222009-10-08 22:55:36 +0000359 isMicrosoftInteger = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000360 hadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Steve Naroff09ef4742007-03-09 23:16:33 +0000362 if (*s == '0') { // parse radix
Chris Lattner6016a512008-06-30 06:39:54 +0000363 ParseNumberStartingWithZero(TokLoc);
364 if (hadError)
365 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000366 } else { // the first digit is non-zero
367 radix = 10;
368 s = SkipDigits(s);
369 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000370 // Done.
Christopher Lamb42e69f22007-11-29 06:06:27 +0000371 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattner59acca52008-11-22 07:23:31 +0000372 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000373 diag::err_invalid_decimal_digit) << StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000374 hadError = true;
Chris Lattner328fa5c2007-06-08 17:12:06 +0000375 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000376 } else if (*s == '.') {
377 s++;
378 saw_period = true;
379 s = SkipDigits(s);
Mike Stump11289f42009-09-09 15:08:12 +0000380 }
Chris Lattnerfb8b8f22008-09-29 23:12:31 +0000381 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner4885b972008-04-20 18:47:55 +0000382 const char *Exponent = s;
Steve Naroff09ef4742007-03-09 23:16:33 +0000383 s++;
384 saw_exponent = true;
385 if (*s == '+' || *s == '-') s++; // sign
386 const char *first_non_digit = SkipDigits(s);
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000387 if (first_non_digit != s) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000388 s = first_non_digit;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000389 } else {
Chris Lattner59acca52008-11-22 07:23:31 +0000390 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
391 diag::err_exponent_has_no_digits);
392 hadError = true;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000393 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000394 }
395 }
396 }
397
398 SuffixBegin = s;
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattnerf55ab182007-08-26 01:58:14 +0000400 // Parse the suffix. At this point we can classify whether we have an FP or
401 // integer constant.
402 bool isFPConstant = isFloatingLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerf55ab182007-08-26 01:58:14 +0000404 // Loop over all of the characters of the suffix. If we see something bad,
405 // we break out of the loop.
406 for (; s != ThisTokEnd; ++s) {
407 switch (*s) {
408 case 'f': // FP Suffix for "float"
409 case 'F':
410 if (!isFPConstant) break; // Error for integer constant.
Chris Lattnered045422007-08-26 03:29:23 +0000411 if (isFloat || isLong) break; // FF, LF invalid.
412 isFloat = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000413 continue; // Success.
414 case 'u':
415 case 'U':
416 if (isFPConstant) break; // Error for floating constant.
417 if (isUnsigned) break; // Cannot be repeated.
418 isUnsigned = true;
419 continue; // Success.
420 case 'l':
421 case 'L':
422 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattnered045422007-08-26 03:29:23 +0000423 if (isFloat) break; // LF invalid.
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattnerf55ab182007-08-26 01:58:14 +0000425 // Check for long long. The L's need to be adjacent and the same case.
426 if (s+1 != ThisTokEnd && s[1] == s[0]) {
427 if (isFPConstant) break; // long long invalid for floats.
428 isLongLong = true;
429 ++s; // Eat both of them.
430 } else {
Steve Naroff09ef4742007-03-09 23:16:33 +0000431 isLong = true;
Steve Naroff09ef4742007-03-09 23:16:33 +0000432 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000433 continue; // Success.
434 case 'i':
Chris Lattner26f6c222010-10-14 00:24:10 +0000435 case 'I':
Steve Naroffa1f41452008-04-04 21:02:54 +0000436 if (PP.getLangOptions().Microsoft) {
Fariborz Jahanian8c6c0b62010-01-22 21:36:53 +0000437 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000438
Steve Naroffa1f41452008-04-04 21:02:54 +0000439 // Allow i8, i16, i32, i64, and i128.
Mike Stumpc99c0222009-10-08 22:55:36 +0000440 if (s + 1 != ThisTokEnd) {
441 switch (s[1]) {
442 case '8':
443 s += 2; // i8 suffix
444 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000445 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000446 case '1':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000447 if (s + 2 == ThisTokEnd) break;
Francois Pichet12df1dc2011-01-11 11:57:53 +0000448 if (s[2] == '6') {
449 s += 3; // i16 suffix
450 isMicrosoftInteger = true;
451 }
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000452 else if (s[2] == '2') {
453 if (s + 3 == ThisTokEnd) break;
Francois Pichet12df1dc2011-01-11 11:57:53 +0000454 if (s[3] == '8') {
455 s += 4; // i128 suffix
456 isMicrosoftInteger = true;
457 }
Mike Stumpc99c0222009-10-08 22:55:36 +0000458 }
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000459 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000460 case '3':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000461 if (s + 2 == ThisTokEnd) break;
Francois Pichet12df1dc2011-01-11 11:57:53 +0000462 if (s[2] == '2') {
463 s += 3; // i32 suffix
464 isLong = true;
465 isMicrosoftInteger = true;
466 }
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000467 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000468 case '6':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000469 if (s + 2 == ThisTokEnd) break;
Francois Pichet12df1dc2011-01-11 11:57:53 +0000470 if (s[2] == '4') {
471 s += 3; // i64 suffix
472 isLongLong = true;
473 isMicrosoftInteger = true;
474 }
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000475 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000476 default:
477 break;
478 }
479 break;
Steve Naroffa1f41452008-04-04 21:02:54 +0000480 }
Steve Naroffa1f41452008-04-04 21:02:54 +0000481 }
482 // fall through.
Chris Lattnerf55ab182007-08-26 01:58:14 +0000483 case 'j':
484 case 'J':
485 if (isImaginary) break; // Cannot be repeated.
486 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
487 diag::ext_imaginary_constant);
488 isImaginary = true;
489 continue; // Success.
Steve Naroff09ef4742007-03-09 23:16:33 +0000490 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000491 // If we reached here, there was an error.
492 break;
493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattnerf55ab182007-08-26 01:58:14 +0000495 // Report an error if there are any.
496 if (s != ThisTokEnd) {
Chris Lattner59acca52008-11-22 07:23:31 +0000497 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
498 isFPConstant ? diag::err_invalid_suffix_float_constant :
499 diag::err_invalid_suffix_integer_constant)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000500 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattner59acca52008-11-22 07:23:31 +0000501 hadError = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000502 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000503 }
504}
505
Chris Lattner6016a512008-06-30 06:39:54 +0000506/// ParseNumberStartingWithZero - This method is called when the first character
507/// of the number is found to be a zero. This means it is either an octal
508/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump11289f42009-09-09 15:08:12 +0000509/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner6016a512008-06-30 06:39:54 +0000510/// radix etc.
511void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
512 assert(s[0] == '0' && "Invalid method call");
513 s++;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattner6016a512008-06-30 06:39:54 +0000515 // Handle a hex number like 0x1234.
516 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
517 s++;
518 radix = 16;
519 DigitsBegin = s;
520 s = SkipHexDigits(s);
521 if (s == ThisTokEnd) {
522 // Done.
523 } else if (*s == '.') {
524 s++;
525 saw_period = true;
526 s = SkipHexDigits(s);
527 }
528 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump11289f42009-09-09 15:08:12 +0000529 // binary exponent is required.
Alexis Hunt91b78382010-01-10 23:37:56 +0000530 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner6016a512008-06-30 06:39:54 +0000531 const char *Exponent = s;
532 s++;
533 saw_exponent = true;
534 if (*s == '+' || *s == '-') s++; // sign
535 const char *first_non_digit = SkipDigits(s);
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000536 if (first_non_digit == s) {
Chris Lattner59acca52008-11-22 07:23:31 +0000537 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
538 diag::err_exponent_has_no_digits);
539 hadError = true;
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000540 return;
Chris Lattner6016a512008-06-30 06:39:54 +0000541 }
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000542 s = first_non_digit;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Alexis Hunt91b78382010-01-10 23:37:56 +0000544 // In C++0x, we cannot support hexadecmial floating literals because
545 // they conflict with user-defined literals, so we warn in previous
546 // versions of C++ by default.
547 if (PP.getLangOptions().CPlusPlus)
548 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
549 else if (!PP.getLangOptions().HexFloats)
Chris Lattner59acca52008-11-22 07:23:31 +0000550 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner6016a512008-06-30 06:39:54 +0000551 } else if (saw_period) {
Chris Lattner59acca52008-11-22 07:23:31 +0000552 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
553 diag::err_hexconstant_requires_exponent);
554 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000555 }
556 return;
557 }
Mike Stump11289f42009-09-09 15:08:12 +0000558
Chris Lattner6016a512008-06-30 06:39:54 +0000559 // Handle simple binary numbers 0b01010
560 if (*s == 'b' || *s == 'B') {
561 // 0b101010 is a GCC extension.
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000562 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner6016a512008-06-30 06:39:54 +0000563 ++s;
564 radix = 2;
565 DigitsBegin = s;
566 s = SkipBinaryDigits(s);
567 if (s == ThisTokEnd) {
568 // Done.
569 } else if (isxdigit(*s)) {
Chris Lattner59acca52008-11-22 07:23:31 +0000570 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000571 diag::err_invalid_binary_digit) << StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000572 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000573 }
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000574 // Other suffixes will be diagnosed by the caller.
Chris Lattner6016a512008-06-30 06:39:54 +0000575 return;
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner6016a512008-06-30 06:39:54 +0000578 // For now, the radix is set to 8. If we discover that we have a
579 // floating point constant, the radix will change to 10. Octal floating
Mike Stump11289f42009-09-09 15:08:12 +0000580 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner6016a512008-06-30 06:39:54 +0000581 radix = 8;
582 DigitsBegin = s;
583 s = SkipOctalDigits(s);
584 if (s == ThisTokEnd)
585 return; // Done, simple octal number like 01234
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000587 // If we have some other non-octal digit that *is* a decimal digit, see if
588 // this is part of a floating point number like 094.123 or 09e1.
589 if (isdigit(*s)) {
590 const char *EndDecimal = SkipDigits(s);
591 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
592 s = EndDecimal;
593 radix = 10;
594 }
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000597 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
598 // the code is using an incorrect base.
Chris Lattner6016a512008-06-30 06:39:54 +0000599 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattner59acca52008-11-22 07:23:31 +0000600 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000601 diag::err_invalid_octal_digit) << StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000602 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000603 return;
604 }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattner6016a512008-06-30 06:39:54 +0000606 if (*s == '.') {
607 s++;
608 radix = 10;
609 saw_period = true;
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000610 s = SkipDigits(s); // Skip suffix.
Chris Lattner6016a512008-06-30 06:39:54 +0000611 }
612 if (*s == 'e' || *s == 'E') { // exponent
613 const char *Exponent = s;
614 s++;
615 radix = 10;
616 saw_exponent = true;
617 if (*s == '+' || *s == '-') s++; // sign
618 const char *first_non_digit = SkipDigits(s);
619 if (first_non_digit != s) {
620 s = first_non_digit;
621 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000622 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattner59acca52008-11-22 07:23:31 +0000623 diag::err_exponent_has_no_digits);
624 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000625 return;
626 }
627 }
628}
629
630
Chris Lattner5b743d32007-04-04 05:52:58 +0000631/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000632/// matches Val's input width. If there is an overflow, set Val to the low bits
633/// of the result and return true. Otherwise, return false.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000634bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbarbe947082008-10-16 07:32:01 +0000635 // Fast path: Compute a conservative bound on the maximum number of
636 // bits per digit in this radix. If we can't possibly overflow a
637 // uint64 based on that bound then do the simple conversion to
638 // integer. This avoids the expensive overflow checking below, and
639 // handles the common cases that matter (small decimal integers and
640 // hex/octal values which don't overflow).
641 unsigned MaxBitsPerDigit = 1;
Mike Stump11289f42009-09-09 15:08:12 +0000642 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbarbe947082008-10-16 07:32:01 +0000643 MaxBitsPerDigit += 1;
644 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
645 uint64_t N = 0;
646 for (s = DigitsBegin; s != SuffixBegin; ++s)
647 N = N*radix + HexDigitValue(*s);
648
649 // This will truncate the value to Val's input width. Simply check
650 // for overflow by comparing.
651 Val = N;
652 return Val.getZExtValue() != N;
653 }
654
Chris Lattner5b743d32007-04-04 05:52:58 +0000655 Val = 0;
656 s = DigitsBegin;
657
Chris Lattner23b7eb62007-06-15 23:05:46 +0000658 llvm::APInt RadixVal(Val.getBitWidth(), radix);
659 llvm::APInt CharVal(Val.getBitWidth(), 0);
660 llvm::APInt OldVal = Val;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Chris Lattner871b4e12007-04-04 06:36:34 +0000662 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000663 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000664 unsigned C = HexDigitValue(*s++);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattner5b743d32007-04-04 05:52:58 +0000666 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000667 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattner5b743d32007-04-04 05:52:58 +0000669 CharVal = C;
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner871b4e12007-04-04 06:36:34 +0000671 // Add the digit to the value in the appropriate radix. If adding in digits
672 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000673 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000674
675 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000676 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000677 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
678
Chris Lattner871b4e12007-04-04 06:36:34 +0000679 // Add value, did overflow occur on the value?
Daniel Dunbarb1f64422008-10-16 06:39:30 +0000680 // (a + b) ult b <=> overflow
Chris Lattner5b743d32007-04-04 05:52:58 +0000681 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000682 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000683 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000684 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000685}
686
John McCall53b93a02009-12-24 09:08:04 +0000687llvm::APFloat::opStatus
688NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000689 using llvm::APFloat;
Mike Stump11289f42009-09-09 15:08:12 +0000690
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000691 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall53b93a02009-12-24 09:08:04 +0000692 return Result.convertFromString(StringRef(ThisTokBegin, n),
693 APFloat::rmNearestTiesToEven);
Steve Naroff97b9e912007-07-09 23:53:58 +0000694}
Chris Lattner5b743d32007-04-04 05:52:58 +0000695
Chris Lattner2f5add62007-04-05 06:57:15 +0000696
697CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
698 SourceLocation Loc, Preprocessor &PP) {
699 // At this point we know that the character matches the regex "L?'.*'".
700 HadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000701
Chris Lattner2f5add62007-04-05 06:57:15 +0000702 // Determine if this is a wide character.
703 IsWide = begin[0] == 'L';
704 if (IsWide) ++begin;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattner2f5add62007-04-05 06:57:15 +0000706 // Skip over the entry quote.
707 assert(begin[0] == '\'' && "Invalid token lexed");
708 ++begin;
709
Mike Stump11289f42009-09-09 15:08:12 +0000710 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Chris Lattner57540c52011-04-15 05:22:18 +0000711 // up to 64-bits.
Chris Lattner2f5add62007-04-05 06:57:15 +0000712 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner37e05872008-03-05 18:54:05 +0000713 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Chris Lattner2f5add62007-04-05 06:57:15 +0000714 "Assumes char is 8 bits");
Chris Lattner8577f622009-04-28 21:51:46 +0000715 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
716 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
717 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
718 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
719 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000720
Mike Stump11289f42009-09-09 15:08:12 +0000721 // This is what we will use for overflow detection
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000722 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner8577f622009-04-28 21:51:46 +0000724 unsigned NumCharsSoFar = 0;
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000725 bool Warned = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000726 while (begin[0] != '\'') {
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000727 uint64_t ResultChar;
Nico Webera6bde812010-10-09 00:27:47 +0000728
729 // Is this a Universal Character Name escape?
Chris Lattner2f5add62007-04-05 06:57:15 +0000730 if (begin[0] != '\\') // If this is a normal character, consume it.
731 ResultChar = *begin++;
Nico Webera6bde812010-10-09 00:27:47 +0000732 else { // Otherwise, this is an escape character.
733 // Check for UCN.
734 if (begin[1] == 'u' || begin[1] == 'U') {
735 uint32_t utf32 = 0;
736 unsigned short UcnLen = 0;
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000737 if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
738 FullSourceLoc(Loc, PP.getSourceManager()),
Chris Lattnerbde1b812010-11-17 06:46:14 +0000739 &PP.getDiagnostics(), PP.getLangOptions())) {
Nico Webera6bde812010-10-09 00:27:47 +0000740 HadError = 1;
741 }
742 ResultChar = utf32;
743 } else {
744 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000745 ResultChar = ProcessCharEscape(begin, end, HadError,
746 FullSourceLoc(Loc,PP.getSourceManager()),
747 IsWide,
748 &PP.getDiagnostics(), PP.getTargetInfo());
Nico Webera6bde812010-10-09 00:27:47 +0000749 }
750 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000751
752 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
753 // implementation defined (C99 6.4.4.4p10).
Chris Lattner8577f622009-04-28 21:51:46 +0000754 if (NumCharsSoFar) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000755 if (IsWide) {
756 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000757 LitVal = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000758 } else {
759 // Narrow character literals act as though their value is concatenated
Chris Lattner8577f622009-04-28 21:51:46 +0000760 // in this implementation, but warn on overflow.
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000761 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000762 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000763 Warned = true;
764 }
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000765 LitVal <<= 8;
Chris Lattner2f5add62007-04-05 06:57:15 +0000766 }
767 }
Mike Stump11289f42009-09-09 15:08:12 +0000768
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000769 LitVal = LitVal + ResultChar;
Chris Lattner8577f622009-04-28 21:51:46 +0000770 ++NumCharsSoFar;
771 }
772
773 // If this is the second character being processed, do special handling.
774 if (NumCharsSoFar > 1) {
775 // Warn about discarding the top bits for multi-char wide-character
776 // constants (L'abcd').
777 if (IsWide)
778 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
779 else if (NumCharsSoFar != 4)
780 PP.Diag(Loc, diag::ext_multichar_character_literal);
781 else
782 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedmand8cec572009-06-01 05:25:02 +0000783 IsMultiChar = true;
Daniel Dunbara444cc22009-07-29 01:46:05 +0000784 } else
785 IsMultiChar = false;
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000786
787 // Transfer the value from APInt to uint64_t
788 Value = LitVal.getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000789
Nico Webera6bde812010-10-09 00:27:47 +0000790 if (IsWide && PP.getLangOptions().ShortWChar && Value > 0xFFFF)
791 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
792
Chris Lattner2f5add62007-04-05 06:57:15 +0000793 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
794 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
795 // character constants are not sign extended in the this implementation:
796 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattner8577f622009-04-28 21:51:46 +0000797 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000798 PP.getLangOptions().CharIsSigned)
Chris Lattner2f5add62007-04-05 06:57:15 +0000799 Value = (signed char)Value;
800}
801
802
Steve Naroff4f88b312007-03-13 22:37:02 +0000803/// string-literal: [C99 6.4.5]
804/// " [s-char-sequence] "
805/// L" [s-char-sequence] "
806/// s-char-sequence:
807/// s-char
808/// s-char-sequence s-char
809/// s-char:
810/// any source character except the double quote ",
811/// backslash \, or newline character
812/// escape-character
813/// universal-character-name
814/// escape-character: [C99 6.4.4.4]
815/// \ escape-code
816/// universal-character-name
817/// escape-code:
818/// character-escape-code
819/// octal-escape-code
820/// hex-escape-code
821/// character-escape-code: one of
822/// n t b r f v a
823/// \ ' " ?
824/// octal-escape-code:
825/// octal-digit
826/// octal-digit octal-digit
827/// octal-digit octal-digit octal-digit
828/// hex-escape-code:
829/// x hex-digit
830/// hex-escape-code hex-digit
831/// universal-character-name:
832/// \u hex-quad
833/// \U hex-quad hex-quad
834/// hex-quad:
835/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000836///
Steve Naroff4f88b312007-03-13 22:37:02 +0000837StringLiteralParser::
Chris Lattner146762e2007-07-20 16:59:19 +0000838StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner6bab4352010-11-17 07:21:13 +0000839 Preprocessor &PP, bool Complain)
840 : SM(PP.getSourceManager()), Features(PP.getLangOptions()),
Argyrios Kyrtzidis8b7252a2011-05-17 22:09:56 +0000841 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
842 MaxTokenLength(0), SizeBound(0), wchar_tByteWidth(0),
843 ResultPtr(ResultBuf.data()), hadError(false), AnyWide(false), Pascal(false) {
Chris Lattner6bab4352010-11-17 07:21:13 +0000844 init(StringToks, NumStringToks);
845}
846
847void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
Argyrios Kyrtzidis8b7252a2011-05-17 22:09:56 +0000848 // The literal token may have come from an invalid source location (e.g. due
849 // to a PCH error), in which case the token length will be 0.
850 if (NumStringToks == 0 || StringToks[0].getLength() < 2) {
851 hadError = true;
852 return;
853 }
854
Steve Naroff4f88b312007-03-13 22:37:02 +0000855 // Scan all of the string portions, remember the max individual token length,
856 // computing a bound on the concatenated string length, and see whether any
857 // piece is a wide-string. If any of the string portions is a wide-string
858 // literal, the result is a wide-string literal [C99 6.4.5p4].
Argyrios Kyrtzidis8b7252a2011-05-17 22:09:56 +0000859 assert(NumStringToks && "expected at least one token");
Alexis Hunt3b791862010-08-30 17:47:05 +0000860 MaxTokenLength = StringToks[0].getLength();
Argyrios Kyrtzidis8b7252a2011-05-17 22:09:56 +0000861 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
Alexis Hunt3b791862010-08-30 17:47:05 +0000862 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000863 AnyWide = StringToks[0].is(tok::wide_string_literal);
Alexis Hunt3b791862010-08-30 17:47:05 +0000864
865 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000866
867 // Implement Translation Phase #6: concatenation of string literals
868 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000869 for (unsigned i = 1; i != NumStringToks; ++i) {
Argyrios Kyrtzidis8b7252a2011-05-17 22:09:56 +0000870 if (StringToks[i].getLength() < 2) {
871 hadError = true;
872 return;
873 }
874
Steve Naroff4f88b312007-03-13 22:37:02 +0000875 // The string could be shorter than this if it needs cleaning, but this is a
876 // reasonable bound, which is all we need.
Argyrios Kyrtzidis8b7252a2011-05-17 22:09:56 +0000877 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
Alexis Hunt3b791862010-08-30 17:47:05 +0000878 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump11289f42009-09-09 15:08:12 +0000879
Steve Naroff4f88b312007-03-13 22:37:02 +0000880 // Remember maximum string piece length.
Alexis Hunt3b791862010-08-30 17:47:05 +0000881 if (StringToks[i].getLength() > MaxTokenLength)
882 MaxTokenLength = StringToks[i].getLength();
Mike Stump11289f42009-09-09 15:08:12 +0000883
Steve Naroff4f88b312007-03-13 22:37:02 +0000884 // Remember if we see any wide strings.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000885 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Steve Naroff4f88b312007-03-13 22:37:02 +0000886 }
Chris Lattnerd42c29f2009-02-26 23:01:51 +0000887
Steve Naroff4f88b312007-03-13 22:37:02 +0000888 // Include space for the null terminator.
889 ++SizeBound;
Mike Stump11289f42009-09-09 15:08:12 +0000890
Steve Naroff4f88b312007-03-13 22:37:02 +0000891 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump11289f42009-09-09 15:08:12 +0000892
Steve Naroff4f88b312007-03-13 22:37:02 +0000893 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
894 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
895 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000896 if (AnyWide) {
Chris Lattner2be8aa92010-11-17 07:12:42 +0000897 wchar_tByteWidth = Target.getWCharWidth();
Chris Lattner2f5add62007-04-05 06:57:15 +0000898 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
899 wchar_tByteWidth /= 8;
900 }
Mike Stump11289f42009-09-09 15:08:12 +0000901
Steve Naroff4f88b312007-03-13 22:37:02 +0000902 // The output buffer size needs to be large enough to hold wide characters.
903 // This is a worst-case assumption which basically corresponds to L"" "long".
904 if (AnyWide)
905 SizeBound *= wchar_tByteWidth;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Steve Naroff4f88b312007-03-13 22:37:02 +0000907 // Size the temporary buffer to hold the result string data.
908 ResultBuf.resize(SizeBound);
Mike Stump11289f42009-09-09 15:08:12 +0000909
Steve Naroff4f88b312007-03-13 22:37:02 +0000910 // Likewise, but for each string piece.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000911 llvm::SmallString<512> TokenBuf;
Steve Naroff4f88b312007-03-13 22:37:02 +0000912 TokenBuf.resize(MaxTokenLength);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Steve Naroff4f88b312007-03-13 22:37:02 +0000914 // Loop over all the strings, getting their spelling, and expanding them to
915 // wide strings as appropriate.
916 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump11289f42009-09-09 15:08:12 +0000917
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000918 Pascal = false;
Mike Stump11289f42009-09-09 15:08:12 +0000919
Steve Naroff4f88b312007-03-13 22:37:02 +0000920 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
921 const char *ThisTokBuf = &TokenBuf[0];
922 // Get the spelling of the token, which eliminates trigraphs, etc. We know
923 // that ThisTokBuf points to a buffer that is big enough for the whole token
924 // and 'spelled' tokens can only shrink.
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000925 bool StringInvalid = false;
Chris Lattner6bab4352010-11-17 07:21:13 +0000926 unsigned ThisTokLen =
Chris Lattner39720112010-11-17 07:26:20 +0000927 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
928 &StringInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000929 if (StringInvalid) {
930 hadError = 1;
931 continue;
932 }
933
Steve Naroff4f88b312007-03-13 22:37:02 +0000934 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000935 bool wide = false;
Steve Naroff4f88b312007-03-13 22:37:02 +0000936 // TODO: Input character set mapping support.
Mike Stump11289f42009-09-09 15:08:12 +0000937
Steve Naroff4f88b312007-03-13 22:37:02 +0000938 // Skip L marker for wide strings.
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000939 if (ThisTokBuf[0] == 'L') {
940 wide = true;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000941 ++ThisTokBuf;
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000942 }
Mike Stump11289f42009-09-09 15:08:12 +0000943
Steve Naroff4f88b312007-03-13 22:37:02 +0000944 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
945 ++ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000946
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000947 // Check if this is a pascal string
Chris Lattner2be8aa92010-11-17 07:12:42 +0000948 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000949 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump11289f42009-09-09 15:08:12 +0000950
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000951 // If the \p sequence is found in the first token, we have a pascal string
952 // Otherwise, if we already have a pascal string, ignore the first \p
953 if (i == 0) {
954 ++ThisTokBuf;
955 Pascal = true;
956 } else if (Pascal)
957 ThisTokBuf += 2;
958 }
Mike Stump11289f42009-09-09 15:08:12 +0000959
Steve Naroff4f88b312007-03-13 22:37:02 +0000960 while (ThisTokBuf != ThisTokEnd) {
961 // Is this a span of non-escape characters?
962 if (ThisTokBuf[0] != '\\') {
963 const char *InStart = ThisTokBuf;
964 do {
965 ++ThisTokBuf;
966 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump11289f42009-09-09 15:08:12 +0000967
Steve Naroff4f88b312007-03-13 22:37:02 +0000968 // Copy the character span over.
969 unsigned Len = ThisTokBuf-InStart;
970 if (!AnyWide) {
971 memcpy(ResultPtr, InStart, Len);
972 ResultPtr += Len;
973 } else {
974 // Note: our internal rep of wide char tokens is always little-endian.
975 for (; Len; --Len, ++InStart) {
976 *ResultPtr++ = InStart[0];
977 // Add zeros at the end.
978 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff7b753d22009-03-30 23:46:03 +0000979 *ResultPtr++ = 0;
Steve Naroff4f88b312007-03-13 22:37:02 +0000980 }
981 }
982 continue;
983 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000984 // Is this a Universal Character Name escape?
Steve Naroff7b753d22009-03-30 23:46:03 +0000985 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Nico Webera6bde812010-10-09 00:27:47 +0000986 EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Chris Lattner2be8aa92010-11-17 07:12:42 +0000987 hadError, FullSourceLoc(StringToks[i].getLocation(),SM),
988 wide, Diags, Features);
Steve Naroffc94adda2009-04-01 11:09:15 +0000989 continue;
990 }
991 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000992 unsigned ResultChar =
993 ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattnerbde1b812010-11-17 06:46:14 +0000994 FullSourceLoc(StringToks[i].getLocation(), SM),
995 AnyWide, Diags, Target);
Mike Stump11289f42009-09-09 15:08:12 +0000996
Steve Naroffc94adda2009-04-01 11:09:15 +0000997 // Note: our internal rep of wide char tokens is always little-endian.
998 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump11289f42009-09-09 15:08:12 +0000999
Steve Naroffc94adda2009-04-01 11:09:15 +00001000 if (AnyWide) {
1001 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
1002 *ResultPtr++ = ResultChar >> i*8;
Steve Naroff4f88b312007-03-13 22:37:02 +00001003 }
1004 }
1005 }
Mike Stump11289f42009-09-09 15:08:12 +00001006
Chris Lattner8a24e582009-01-16 18:51:42 +00001007 if (Pascal) {
Anders Carlssoncbfc4b82007-10-15 02:50:23 +00001008 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Fariborz Jahanian93bef102010-05-28 19:40:48 +00001009 if (AnyWide)
1010 ResultBuf[0] /= wchar_tByteWidth;
Chris Lattner8a24e582009-01-16 18:51:42 +00001011
1012 // Verify that pascal strings aren't too large.
Chris Lattner6bab4352010-11-17 07:21:13 +00001013 if (GetStringLength() > 256) {
1014 if (Diags)
1015 Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1016 diag::err_pascal_string_too_long)
1017 << SourceRange(StringToks[0].getLocation(),
1018 StringToks[NumStringToks-1].getLocation());
Eli Friedman1c3fb222009-04-01 03:17:08 +00001019 hadError = 1;
1020 return;
1021 }
Chris Lattner6bab4352010-11-17 07:21:13 +00001022 } else if (Diags) {
Douglas Gregorb37b46e2010-07-20 14:33:20 +00001023 // Complain if this string literal has too many characters.
Chris Lattner2be8aa92010-11-17 07:12:42 +00001024 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Douglas Gregorb37b46e2010-07-20 14:33:20 +00001025
1026 if (GetNumStringChars() > MaxChars)
Chris Lattner6bab4352010-11-17 07:21:13 +00001027 Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1028 diag::ext_string_too_long)
Douglas Gregorb37b46e2010-07-20 14:33:20 +00001029 << GetNumStringChars() << MaxChars
Chris Lattner2be8aa92010-11-17 07:12:42 +00001030 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregorb37b46e2010-07-20 14:33:20 +00001031 << SourceRange(StringToks[0].getLocation(),
1032 StringToks[NumStringToks-1].getLocation());
Chris Lattner8a24e582009-01-16 18:51:42 +00001033 }
Steve Naroff4f88b312007-03-13 22:37:02 +00001034}
Chris Lattnerddb71912009-02-18 19:21:10 +00001035
1036
1037/// getOffsetOfStringByte - This function returns the offset of the
1038/// specified byte of the string data represented by Token. This handles
1039/// advancing over escape sequences in the string.
1040unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattnerbde1b812010-11-17 06:46:14 +00001041 unsigned ByteNo) const {
Chris Lattnerddb71912009-02-18 19:21:10 +00001042 // Get the spelling of the token.
Chris Lattner3a324d32010-11-17 06:35:43 +00001043 llvm::SmallString<32> SpellingBuffer;
Alexis Hunt3b791862010-08-30 17:47:05 +00001044 SpellingBuffer.resize(Tok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001045
Douglas Gregor7bda4b82010-03-16 05:20:39 +00001046 bool StringInvalid = false;
Chris Lattnerddb71912009-02-18 19:21:10 +00001047 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattner39720112010-11-17 07:26:20 +00001048 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1049 &StringInvalid);
Chris Lattner7a02bfd2010-11-17 06:26:08 +00001050 if (StringInvalid)
Douglas Gregor7bda4b82010-03-16 05:20:39 +00001051 return 0;
Chris Lattnerddb71912009-02-18 19:21:10 +00001052
1053 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
1054
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattnerddb71912009-02-18 19:21:10 +00001056 const char *SpellingStart = SpellingPtr;
1057 const char *SpellingEnd = SpellingPtr+TokLen;
1058
1059 // Skip over the leading quote.
1060 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1061 ++SpellingPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001062
Chris Lattnerddb71912009-02-18 19:21:10 +00001063 // Skip over bytes until we find the offset we're looking for.
1064 while (ByteNo) {
1065 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump11289f42009-09-09 15:08:12 +00001066
Chris Lattnerddb71912009-02-18 19:21:10 +00001067 // Step over non-escapes simply.
1068 if (*SpellingPtr != '\\') {
1069 ++SpellingPtr;
1070 --ByteNo;
1071 continue;
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattnerddb71912009-02-18 19:21:10 +00001074 // Otherwise, this is an escape character. Advance over it.
1075 bool HadError = false;
1076 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Chris Lattner3a324d32010-11-17 06:35:43 +00001077 FullSourceLoc(Tok.getLocation(), SM),
Chris Lattner7a02bfd2010-11-17 06:26:08 +00001078 false, Diags, Target);
Chris Lattnerddb71912009-02-18 19:21:10 +00001079 assert(!HadError && "This method isn't valid on erroneous strings");
1080 --ByteNo;
1081 }
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattnerddb71912009-02-18 19:21:10 +00001083 return SpellingPtr-SpellingStart;
1084}