blob: 8ad1669647fc194e1fdc39d618fad998dd98ce51 [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"
Erick Tryzelaarb9073112009-08-16 23:36:28 +000019#include "llvm/ADT/StringRef.h"
Steve Naroff4f88b312007-03-13 22:37:02 +000020#include "llvm/ADT/StringExtras.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000021using namespace clang;
22
Chris Lattner2f5add62007-04-05 06:57:15 +000023/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
24/// not valid.
25static int HexDigitValue(char C) {
26 if (C >= '0' && C <= '9') return C-'0';
27 if (C >= 'a' && C <= 'f') return C-'a'+10;
28 if (C >= 'A' && C <= 'F') return C-'A'+10;
29 return -1;
30}
31
32/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
33/// either a character or a string literal.
34static unsigned ProcessCharEscape(const char *&ThisTokBuf,
35 const char *ThisTokEnd, bool &HadError,
Chris Lattner7a02bfd2010-11-17 06:26:08 +000036 FullSourceLoc Loc, bool IsWide,
37 Diagnostic *Diags, const TargetInfo &Target) {
Chris Lattner2f5add62007-04-05 06:57:15 +000038 // Skip the '\' char.
39 ++ThisTokBuf;
40
41 // We know that this character can't be off the end of the buffer, because
42 // that would have been \", which would not have been the end of string.
43 unsigned ResultChar = *ThisTokBuf++;
44 switch (ResultChar) {
45 // These map to themselves.
46 case '\\': case '\'': case '"': case '?': break;
Mike Stump11289f42009-09-09 15:08:12 +000047
Chris Lattner2f5add62007-04-05 06:57:15 +000048 // These have fixed mappings.
49 case 'a':
50 // TODO: K&R: the meaning of '\\a' is different in traditional C
51 ResultChar = 7;
52 break;
53 case 'b':
54 ResultChar = 8;
55 break;
56 case 'e':
Chris Lattner7a02bfd2010-11-17 06:26:08 +000057 if (Diags)
58 Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
Chris Lattner2f5add62007-04-05 06:57:15 +000059 ResultChar = 27;
60 break;
Eli Friedman28a00aa2009-06-10 01:32:39 +000061 case 'E':
Chris Lattner7a02bfd2010-11-17 06:26:08 +000062 if (Diags)
63 Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
Eli Friedman28a00aa2009-06-10 01:32:39 +000064 ResultChar = 27;
65 break;
Chris Lattner2f5add62007-04-05 06:57:15 +000066 case 'f':
67 ResultChar = 12;
68 break;
69 case 'n':
70 ResultChar = 10;
71 break;
72 case 'r':
73 ResultChar = 13;
74 break;
75 case 't':
76 ResultChar = 9;
77 break;
78 case 'v':
79 ResultChar = 11;
80 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +000081 case 'x': { // Hex escape.
82 ResultChar = 0;
83 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner7a02bfd2010-11-17 06:26:08 +000084 if (Diags)
85 Diags->Report(Loc, diag::err_hex_escape_no_digits);
Chris Lattner2f5add62007-04-05 06:57:15 +000086 HadError = 1;
Chris Lattner2f5add62007-04-05 06:57:15 +000087 break;
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner812eda82007-05-20 05:17:04 +000090 // Hex escapes are a maximal series of hex digits.
Chris Lattnerc10adde2007-05-20 05:00:58 +000091 bool Overflow = false;
92 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
93 int CharVal = HexDigitValue(ThisTokBuf[0]);
94 if (CharVal == -1) break;
Chris Lattner59f09b62008-09-30 20:45:40 +000095 // About to shift out a digit?
96 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Chris Lattnerc10adde2007-05-20 05:00:58 +000097 ResultChar <<= 4;
98 ResultChar |= CharVal;
99 }
100
101 // See if any bits will be truncated when evaluated as a character.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000102 unsigned CharWidth =
103 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattnerc10adde2007-05-20 05:00:58 +0000105 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
106 Overflow = true;
107 ResultChar &= ~0U >> (32-CharWidth);
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnerc10adde2007-05-20 05:00:58 +0000110 // Check for overflow.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000111 if (Overflow && Diags) // Too many digits to fit in
112 Diags->Report(Loc, diag::warn_hex_escape_too_large);
Chris Lattner2f5add62007-04-05 06:57:15 +0000113 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000114 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000115 case '0': case '1': case '2': case '3':
Chris Lattner812eda82007-05-20 05:17:04 +0000116 case '4': case '5': case '6': case '7': {
Chris Lattner2f5add62007-04-05 06:57:15 +0000117 // Octal escapes.
Chris Lattner3f4b6e32007-06-09 06:20:47 +0000118 --ThisTokBuf;
Chris Lattner812eda82007-05-20 05:17:04 +0000119 ResultChar = 0;
120
121 // Octal escapes are a series of octal digits with maximum length 3.
122 // "\0123" is a two digit sequence equal to "\012" "3".
123 unsigned NumDigits = 0;
124 do {
125 ResultChar <<= 3;
126 ResultChar |= *ThisTokBuf++ - '0';
127 ++NumDigits;
128 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
129 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump11289f42009-09-09 15:08:12 +0000130
Chris Lattner812eda82007-05-20 05:17:04 +0000131 // Check for overflow. Reject '\777', but not L'\777'.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000132 unsigned CharWidth =
133 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattner812eda82007-05-20 05:17:04 +0000135 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000136 if (Diags)
137 Diags->Report(Loc, diag::warn_octal_escape_too_large);
Chris Lattner812eda82007-05-20 05:17:04 +0000138 ResultChar &= ~0U >> (32-CharWidth);
139 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000140 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattner2f5add62007-04-05 06:57:15 +0000143 // Otherwise, these are not valid escapes.
144 case '(': case '{': case '[': case '%':
145 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000146 if (Diags)
147 Diags->Report(Loc, diag::ext_nonstandard_escape)
Douglas Gregor9af03022010-05-26 05:35:51 +0000148 << std::string()+(char)ResultChar;
Eli Friedman5d72d412009-04-28 00:51:18 +0000149 break;
Chris Lattner2f5add62007-04-05 06:57:15 +0000150 default:
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000151 if (Diags == 0)
Douglas Gregor9af03022010-05-26 05:35:51 +0000152 break;
153
Chris Lattner59acca52008-11-22 07:23:31 +0000154 if (isgraph(ThisTokBuf[0]))
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000155 Diags->Report(Loc, diag::ext_unknown_escape)
156 << std::string()+(char)ResultChar;
Chris Lattner59acca52008-11-22 07:23:31 +0000157 else
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000158 Diags->Report(Loc, diag::ext_unknown_escape)
159 << "x"+llvm::utohexstr(ResultChar);
Chris Lattner2f5add62007-04-05 06:57:15 +0000160 break;
161 }
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner2f5add62007-04-05 06:57:15 +0000163 return ResultChar;
164}
165
Steve Naroff7b753d22009-03-30 23:46:03 +0000166/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Webera6bde812010-10-09 00:27:47 +0000167/// return the UTF32.
168static bool ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
169 uint32_t &UcnVal, unsigned short &UcnLen,
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000170 FullSourceLoc Loc, Diagnostic *Diags,
Chris Lattnerbde1b812010-11-17 06:46:14 +0000171 const LangOptions &Features) {
172 if (!Features.CPlusPlus && !Features.C99 && Diags)
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000173 Diags->Report(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump11289f42009-09-09 15:08:12 +0000174
Steve Naroffc94adda2009-04-01 11:09:15 +0000175 // Save the beginning of the string (for error diagnostics).
176 const char *ThisTokBegin = ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000177
Steve Naroff7b753d22009-03-30 23:46:03 +0000178 // Skip the '\u' char's.
179 ThisTokBuf += 2;
Chris Lattner2f5add62007-04-05 06:57:15 +0000180
Steve Naroff7b753d22009-03-30 23:46:03 +0000181 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattnerbde1b812010-11-17 06:46:14 +0000182 if (Diags)
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000183 Diags->Report(Loc, diag::err_ucn_escape_no_digits);
Nico Webera6bde812010-10-09 00:27:47 +0000184 return false;
Steve Naroff7b753d22009-03-30 23:46:03 +0000185 }
Nico Webera6bde812010-10-09 00:27:47 +0000186 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000187 unsigned short UcnLenSave = UcnLen;
Nico Webera6bde812010-10-09 00:27:47 +0000188 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff7b753d22009-03-30 23:46:03 +0000189 int CharVal = HexDigitValue(ThisTokBuf[0]);
190 if (CharVal == -1) break;
191 UcnVal <<= 4;
192 UcnVal |= CharVal;
193 }
194 // If we didn't consume the proper number of digits, there is a problem.
Nico Webera6bde812010-10-09 00:27:47 +0000195 if (UcnLenSave) {
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000196 if (Diags) {
197 Loc = Preprocessor::AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin,
198 Features);
199 Diags->Report(Loc, diag::err_ucn_escape_incomplete);
200 }
Nico Webera6bde812010-10-09 00:27:47 +0000201 return false;
Steve Naroff7b753d22009-03-30 23:46:03 +0000202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff7b753d22009-03-30 23:46:03 +0000204 if ((UcnVal < 0xa0 &&
205 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump11289f42009-09-09 15:08:12 +0000206 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Narofff2a880c2009-03-31 10:29:45 +0000207 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Chris Lattnerbde1b812010-11-17 06:46:14 +0000208 if (Diags)
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000209 Diags->Report(Loc, diag::err_ucn_escape_invalid);
Nico Webera6bde812010-10-09 00:27:47 +0000210 return false;
211 }
212 return true;
213}
214
215/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
216/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
217/// StringLiteralParser. When we decide to implement UCN's for identifiers,
218/// we will likely rework our support for UCN's.
219static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
220 char *&ResultBuf, bool &HadError,
221 SourceLocation Loc, Preprocessor &PP,
Chris Lattnerbde1b812010-11-17 06:46:14 +0000222 bool wide, bool Complain) {
Nico Webera6bde812010-10-09 00:27:47 +0000223 typedef uint32_t UTF32;
224 UTF32 UcnVal = 0;
225 unsigned short UcnLen = 0;
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000226 if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
227 FullSourceLoc(Loc, PP.getSourceManager()),
Chris Lattnerbde1b812010-11-17 06:46:14 +0000228 Complain ? &PP.getDiagnostics() : 0,
229 PP.getLangOptions())){
Steve Naroff7b753d22009-03-30 23:46:03 +0000230 HadError = 1;
231 return;
232 }
Nico Webera6bde812010-10-09 00:27:47 +0000233
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000234 if (wide) {
Nico Webera6bde812010-10-09 00:27:47 +0000235 (void)UcnLen;
236 assert((UcnLen== 4 || UcnLen== 8) &&
237 "EncodeUCNEscape - only ucn length of 4 or 8 supported");
Nico Weber9762e0a2010-10-06 04:57:26 +0000238
239 if (!PP.getLangOptions().ShortWChar) {
240 // Note: our internal rep of wide char tokens is always little-endian.
241 *ResultBuf++ = (UcnVal & 0x000000FF);
242 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
243 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
244 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
245 return;
246 }
247
248 // Convert to UTF16.
249 if (UcnVal < (UTF32)0xFFFF) {
250 *ResultBuf++ = (UcnVal & 0x000000FF);
251 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
252 return;
253 }
254 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
255
256 typedef uint16_t UTF16;
257 UcnVal -= 0x10000;
258 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
259 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
260 *ResultBuf++ = (surrogate1 & 0x000000FF);
261 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
262 *ResultBuf++ = (surrogate2 & 0x000000FF);
263 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000264 return;
265 }
Steve Naroff7b753d22009-03-30 23:46:03 +0000266 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
267 // The conversion below was inspired by:
268 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump11289f42009-09-09 15:08:12 +0000269 // First, we determine how many bytes the result will require.
Steve Naroffc94adda2009-04-01 11:09:15 +0000270 typedef uint8_t UTF8;
Steve Naroff7b753d22009-03-30 23:46:03 +0000271
272 unsigned short bytesToWrite = 0;
273 if (UcnVal < (UTF32)0x80)
274 bytesToWrite = 1;
275 else if (UcnVal < (UTF32)0x800)
276 bytesToWrite = 2;
277 else if (UcnVal < (UTF32)0x10000)
278 bytesToWrite = 3;
279 else
280 bytesToWrite = 4;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Steve Naroff7b753d22009-03-30 23:46:03 +0000282 const unsigned byteMask = 0xBF;
283 const unsigned byteMark = 0x80;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Steve Naroff7b753d22009-03-30 23:46:03 +0000285 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Narofff2a880c2009-03-31 10:29:45 +0000286 // into the first byte, depending on how many bytes follow.
Mike Stump11289f42009-09-09 15:08:12 +0000287 static const UTF8 firstByteMark[5] = {
Steve Narofff2a880c2009-03-31 10:29:45 +0000288 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff7b753d22009-03-30 23:46:03 +0000289 };
290 // Finally, we write the bytes into ResultBuf.
291 ResultBuf += bytesToWrite;
292 switch (bytesToWrite) { // note: everything falls through.
293 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
294 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
295 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
296 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
297 }
298 // Update the buffer.
299 ResultBuf += bytesToWrite;
300}
Chris Lattner2f5add62007-04-05 06:57:15 +0000301
302
Steve Naroff09ef4742007-03-09 23:16:33 +0000303/// integer-constant: [C99 6.4.4.1]
304/// decimal-constant integer-suffix
305/// octal-constant integer-suffix
306/// hexadecimal-constant integer-suffix
Mike Stump11289f42009-09-09 15:08:12 +0000307/// decimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000308/// nonzero-digit
309/// decimal-constant digit
Mike Stump11289f42009-09-09 15:08:12 +0000310/// octal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000311/// 0
312/// octal-constant octal-digit
Mike Stump11289f42009-09-09 15:08:12 +0000313/// hexadecimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000314/// hexadecimal-prefix hexadecimal-digit
315/// hexadecimal-constant hexadecimal-digit
316/// hexadecimal-prefix: one of
317/// 0x 0X
318/// integer-suffix:
319/// unsigned-suffix [long-suffix]
320/// unsigned-suffix [long-long-suffix]
321/// long-suffix [unsigned-suffix]
322/// long-long-suffix [unsigned-sufix]
323/// nonzero-digit:
324/// 1 2 3 4 5 6 7 8 9
325/// octal-digit:
326/// 0 1 2 3 4 5 6 7
327/// hexadecimal-digit:
328/// 0 1 2 3 4 5 6 7 8 9
329/// a b c d e f
330/// A B C D E F
331/// unsigned-suffix: one of
332/// u U
333/// long-suffix: one of
334/// l L
Mike Stump11289f42009-09-09 15:08:12 +0000335/// long-long-suffix: one of
Steve Naroff09ef4742007-03-09 23:16:33 +0000336/// ll LL
337///
338/// floating-constant: [C99 6.4.4.2]
339/// TODO: add rules...
340///
Steve Naroff09ef4742007-03-09 23:16:33 +0000341NumericLiteralParser::
342NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000343 SourceLocation TokLoc, Preprocessor &pp)
344 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattner59f09b62008-09-30 20:45:40 +0000346 // This routine assumes that the range begin/end matches the regex for integer
347 // and FP constants (specifically, the 'pp-number' regex), and assumes that
348 // the byte at "*end" is both valid and not part of the regex. Because of
349 // this, it doesn't have to check for 'overscan' in various places.
350 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
351 "Lexer didn't maximally munch?");
Mike Stump11289f42009-09-09 15:08:12 +0000352
Steve Naroff09ef4742007-03-09 23:16:33 +0000353 s = DigitsBegin = begin;
354 saw_exponent = false;
355 saw_period = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000356 isLong = false;
357 isUnsigned = false;
358 isLongLong = false;
Chris Lattnered045422007-08-26 03:29:23 +0000359 isFloat = false;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000360 isImaginary = false;
Mike Stumpc99c0222009-10-08 22:55:36 +0000361 isMicrosoftInteger = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000362 hadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Steve Naroff09ef4742007-03-09 23:16:33 +0000364 if (*s == '0') { // parse radix
Chris Lattner6016a512008-06-30 06:39:54 +0000365 ParseNumberStartingWithZero(TokLoc);
366 if (hadError)
367 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000368 } else { // the first digit is non-zero
369 radix = 10;
370 s = SkipDigits(s);
371 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000372 // Done.
Christopher Lamb42e69f22007-11-29 06:06:27 +0000373 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattner59acca52008-11-22 07:23:31 +0000374 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Benjamin Kramere8394df2010-08-11 14:47:12 +0000375 diag::err_invalid_decimal_digit) << llvm::StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000376 hadError = true;
Chris Lattner328fa5c2007-06-08 17:12:06 +0000377 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000378 } else if (*s == '.') {
379 s++;
380 saw_period = true;
381 s = SkipDigits(s);
Mike Stump11289f42009-09-09 15:08:12 +0000382 }
Chris Lattnerfb8b8f22008-09-29 23:12:31 +0000383 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner4885b972008-04-20 18:47:55 +0000384 const char *Exponent = s;
Steve Naroff09ef4742007-03-09 23:16:33 +0000385 s++;
386 saw_exponent = true;
387 if (*s == '+' || *s == '-') s++; // sign
388 const char *first_non_digit = SkipDigits(s);
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000389 if (first_non_digit != s) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000390 s = first_non_digit;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000391 } else {
Chris Lattner59acca52008-11-22 07:23:31 +0000392 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
393 diag::err_exponent_has_no_digits);
394 hadError = true;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000395 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000396 }
397 }
398 }
399
400 SuffixBegin = s;
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattnerf55ab182007-08-26 01:58:14 +0000402 // Parse the suffix. At this point we can classify whether we have an FP or
403 // integer constant.
404 bool isFPConstant = isFloatingLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattnerf55ab182007-08-26 01:58:14 +0000406 // Loop over all of the characters of the suffix. If we see something bad,
407 // we break out of the loop.
408 for (; s != ThisTokEnd; ++s) {
409 switch (*s) {
410 case 'f': // FP Suffix for "float"
411 case 'F':
412 if (!isFPConstant) break; // Error for integer constant.
Chris Lattnered045422007-08-26 03:29:23 +0000413 if (isFloat || isLong) break; // FF, LF invalid.
414 isFloat = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000415 continue; // Success.
416 case 'u':
417 case 'U':
418 if (isFPConstant) break; // Error for floating constant.
419 if (isUnsigned) break; // Cannot be repeated.
420 isUnsigned = true;
421 continue; // Success.
422 case 'l':
423 case 'L':
424 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattnered045422007-08-26 03:29:23 +0000425 if (isFloat) break; // LF invalid.
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerf55ab182007-08-26 01:58:14 +0000427 // Check for long long. The L's need to be adjacent and the same case.
428 if (s+1 != ThisTokEnd && s[1] == s[0]) {
429 if (isFPConstant) break; // long long invalid for floats.
430 isLongLong = true;
431 ++s; // Eat both of them.
432 } else {
Steve Naroff09ef4742007-03-09 23:16:33 +0000433 isLong = true;
Steve Naroff09ef4742007-03-09 23:16:33 +0000434 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000435 continue; // Success.
436 case 'i':
Chris Lattner26f6c222010-10-14 00:24:10 +0000437 case 'I':
Steve Naroffa1f41452008-04-04 21:02:54 +0000438 if (PP.getLangOptions().Microsoft) {
Fariborz Jahanian8c6c0b62010-01-22 21:36:53 +0000439 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000440
Steve Naroffa1f41452008-04-04 21:02:54 +0000441 // Allow i8, i16, i32, i64, and i128.
Mike Stumpc99c0222009-10-08 22:55:36 +0000442 if (s + 1 != ThisTokEnd) {
443 switch (s[1]) {
444 case '8':
445 s += 2; // i8 suffix
446 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000447 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000448 case '1':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000449 if (s + 2 == ThisTokEnd) break;
450 if (s[2] == '6') s += 3; // i16 suffix
451 else if (s[2] == '2') {
452 if (s + 3 == ThisTokEnd) break;
453 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000454 }
455 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000456 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000457 case '3':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000458 if (s + 2 == ThisTokEnd) break;
459 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000460 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000461 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000462 case '6':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000463 if (s + 2 == ThisTokEnd) break;
464 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000465 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000466 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000467 default:
468 break;
469 }
470 break;
Steve Naroffa1f41452008-04-04 21:02:54 +0000471 }
Steve Naroffa1f41452008-04-04 21:02:54 +0000472 }
473 // fall through.
Chris Lattnerf55ab182007-08-26 01:58:14 +0000474 case 'j':
475 case 'J':
476 if (isImaginary) break; // Cannot be repeated.
477 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
478 diag::ext_imaginary_constant);
479 isImaginary = true;
480 continue; // Success.
Steve Naroff09ef4742007-03-09 23:16:33 +0000481 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000482 // If we reached here, there was an error.
483 break;
484 }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattnerf55ab182007-08-26 01:58:14 +0000486 // Report an error if there are any.
487 if (s != ThisTokEnd) {
Chris Lattner59acca52008-11-22 07:23:31 +0000488 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
489 isFPConstant ? diag::err_invalid_suffix_float_constant :
490 diag::err_invalid_suffix_integer_constant)
Benjamin Kramere8394df2010-08-11 14:47:12 +0000491 << llvm::StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattner59acca52008-11-22 07:23:31 +0000492 hadError = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000493 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000494 }
495}
496
Chris Lattner6016a512008-06-30 06:39:54 +0000497/// ParseNumberStartingWithZero - This method is called when the first character
498/// of the number is found to be a zero. This means it is either an octal
499/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump11289f42009-09-09 15:08:12 +0000500/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner6016a512008-06-30 06:39:54 +0000501/// radix etc.
502void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
503 assert(s[0] == '0' && "Invalid method call");
504 s++;
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattner6016a512008-06-30 06:39:54 +0000506 // Handle a hex number like 0x1234.
507 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
508 s++;
509 radix = 16;
510 DigitsBegin = s;
511 s = SkipHexDigits(s);
512 if (s == ThisTokEnd) {
513 // Done.
514 } else if (*s == '.') {
515 s++;
516 saw_period = true;
517 s = SkipHexDigits(s);
518 }
519 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump11289f42009-09-09 15:08:12 +0000520 // binary exponent is required.
Alexis Hunt91b78382010-01-10 23:37:56 +0000521 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner6016a512008-06-30 06:39:54 +0000522 const char *Exponent = s;
523 s++;
524 saw_exponent = true;
525 if (*s == '+' || *s == '-') s++; // sign
526 const char *first_non_digit = SkipDigits(s);
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000527 if (first_non_digit == s) {
Chris Lattner59acca52008-11-22 07:23:31 +0000528 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
529 diag::err_exponent_has_no_digits);
530 hadError = true;
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000531 return;
Chris Lattner6016a512008-06-30 06:39:54 +0000532 }
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000533 s = first_non_digit;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Alexis Hunt91b78382010-01-10 23:37:56 +0000535 // In C++0x, we cannot support hexadecmial floating literals because
536 // they conflict with user-defined literals, so we warn in previous
537 // versions of C++ by default.
538 if (PP.getLangOptions().CPlusPlus)
539 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
540 else if (!PP.getLangOptions().HexFloats)
Chris Lattner59acca52008-11-22 07:23:31 +0000541 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner6016a512008-06-30 06:39:54 +0000542 } else if (saw_period) {
Chris Lattner59acca52008-11-22 07:23:31 +0000543 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
544 diag::err_hexconstant_requires_exponent);
545 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000546 }
547 return;
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattner6016a512008-06-30 06:39:54 +0000550 // Handle simple binary numbers 0b01010
551 if (*s == 'b' || *s == 'B') {
552 // 0b101010 is a GCC extension.
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000553 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner6016a512008-06-30 06:39:54 +0000554 ++s;
555 radix = 2;
556 DigitsBegin = s;
557 s = SkipBinaryDigits(s);
558 if (s == ThisTokEnd) {
559 // Done.
560 } else if (isxdigit(*s)) {
Chris Lattner59acca52008-11-22 07:23:31 +0000561 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramere8394df2010-08-11 14:47:12 +0000562 diag::err_invalid_binary_digit) << llvm::StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000563 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000564 }
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000565 // Other suffixes will be diagnosed by the caller.
Chris Lattner6016a512008-06-30 06:39:54 +0000566 return;
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner6016a512008-06-30 06:39:54 +0000569 // For now, the radix is set to 8. If we discover that we have a
570 // floating point constant, the radix will change to 10. Octal floating
Mike Stump11289f42009-09-09 15:08:12 +0000571 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner6016a512008-06-30 06:39:54 +0000572 radix = 8;
573 DigitsBegin = s;
574 s = SkipOctalDigits(s);
575 if (s == ThisTokEnd)
576 return; // Done, simple octal number like 01234
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000578 // If we have some other non-octal digit that *is* a decimal digit, see if
579 // this is part of a floating point number like 094.123 or 09e1.
580 if (isdigit(*s)) {
581 const char *EndDecimal = SkipDigits(s);
582 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
583 s = EndDecimal;
584 radix = 10;
585 }
586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000588 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
589 // the code is using an incorrect base.
Chris Lattner6016a512008-06-30 06:39:54 +0000590 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattner59acca52008-11-22 07:23:31 +0000591 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramere8394df2010-08-11 14:47:12 +0000592 diag::err_invalid_octal_digit) << llvm::StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000593 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000594 return;
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattner6016a512008-06-30 06:39:54 +0000597 if (*s == '.') {
598 s++;
599 radix = 10;
600 saw_period = true;
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000601 s = SkipDigits(s); // Skip suffix.
Chris Lattner6016a512008-06-30 06:39:54 +0000602 }
603 if (*s == 'e' || *s == 'E') { // exponent
604 const char *Exponent = s;
605 s++;
606 radix = 10;
607 saw_exponent = true;
608 if (*s == '+' || *s == '-') s++; // sign
609 const char *first_non_digit = SkipDigits(s);
610 if (first_non_digit != s) {
611 s = first_non_digit;
612 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000613 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattner59acca52008-11-22 07:23:31 +0000614 diag::err_exponent_has_no_digits);
615 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000616 return;
617 }
618 }
619}
620
621
Chris Lattner5b743d32007-04-04 05:52:58 +0000622/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000623/// matches Val's input width. If there is an overflow, set Val to the low bits
624/// of the result and return true. Otherwise, return false.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000625bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbarbe947082008-10-16 07:32:01 +0000626 // Fast path: Compute a conservative bound on the maximum number of
627 // bits per digit in this radix. If we can't possibly overflow a
628 // uint64 based on that bound then do the simple conversion to
629 // integer. This avoids the expensive overflow checking below, and
630 // handles the common cases that matter (small decimal integers and
631 // hex/octal values which don't overflow).
632 unsigned MaxBitsPerDigit = 1;
Mike Stump11289f42009-09-09 15:08:12 +0000633 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbarbe947082008-10-16 07:32:01 +0000634 MaxBitsPerDigit += 1;
635 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
636 uint64_t N = 0;
637 for (s = DigitsBegin; s != SuffixBegin; ++s)
638 N = N*radix + HexDigitValue(*s);
639
640 // This will truncate the value to Val's input width. Simply check
641 // for overflow by comparing.
642 Val = N;
643 return Val.getZExtValue() != N;
644 }
645
Chris Lattner5b743d32007-04-04 05:52:58 +0000646 Val = 0;
647 s = DigitsBegin;
648
Chris Lattner23b7eb62007-06-15 23:05:46 +0000649 llvm::APInt RadixVal(Val.getBitWidth(), radix);
650 llvm::APInt CharVal(Val.getBitWidth(), 0);
651 llvm::APInt OldVal = Val;
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner871b4e12007-04-04 06:36:34 +0000653 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000654 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000655 unsigned C = HexDigitValue(*s++);
Mike Stump11289f42009-09-09 15:08:12 +0000656
Chris Lattner5b743d32007-04-04 05:52:58 +0000657 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000658 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump11289f42009-09-09 15:08:12 +0000659
Chris Lattner5b743d32007-04-04 05:52:58 +0000660 CharVal = C;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Chris Lattner871b4e12007-04-04 06:36:34 +0000662 // Add the digit to the value in the appropriate radix. If adding in digits
663 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000664 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000665
666 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000667 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000668 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
669
Chris Lattner871b4e12007-04-04 06:36:34 +0000670 // Add value, did overflow occur on the value?
Daniel Dunbarb1f64422008-10-16 06:39:30 +0000671 // (a + b) ult b <=> overflow
Chris Lattner5b743d32007-04-04 05:52:58 +0000672 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000673 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000674 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000675 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000676}
677
John McCall53b93a02009-12-24 09:08:04 +0000678llvm::APFloat::opStatus
679NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000680 using llvm::APFloat;
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000681 using llvm::StringRef;
Mike Stump11289f42009-09-09 15:08:12 +0000682
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000683 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall53b93a02009-12-24 09:08:04 +0000684 return Result.convertFromString(StringRef(ThisTokBegin, n),
685 APFloat::rmNearestTiesToEven);
Steve Naroff97b9e912007-07-09 23:53:58 +0000686}
Chris Lattner5b743d32007-04-04 05:52:58 +0000687
Chris Lattner2f5add62007-04-05 06:57:15 +0000688
689CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
690 SourceLocation Loc, Preprocessor &PP) {
691 // At this point we know that the character matches the regex "L?'.*'".
692 HadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000693
Chris Lattner2f5add62007-04-05 06:57:15 +0000694 // Determine if this is a wide character.
695 IsWide = begin[0] == 'L';
696 if (IsWide) ++begin;
Mike Stump11289f42009-09-09 15:08:12 +0000697
Chris Lattner2f5add62007-04-05 06:57:15 +0000698 // Skip over the entry quote.
699 assert(begin[0] == '\'' && "Invalid token lexed");
700 ++begin;
701
Mike Stump11289f42009-09-09 15:08:12 +0000702 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000703 // upto 64-bits.
Chris Lattner2f5add62007-04-05 06:57:15 +0000704 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner37e05872008-03-05 18:54:05 +0000705 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Chris Lattner2f5add62007-04-05 06:57:15 +0000706 "Assumes char is 8 bits");
Chris Lattner8577f622009-04-28 21:51:46 +0000707 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
708 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
709 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
710 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
711 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000712
Mike Stump11289f42009-09-09 15:08:12 +0000713 // This is what we will use for overflow detection
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000714 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner8577f622009-04-28 21:51:46 +0000716 unsigned NumCharsSoFar = 0;
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000717 bool Warned = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000718 while (begin[0] != '\'') {
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000719 uint64_t ResultChar;
Nico Webera6bde812010-10-09 00:27:47 +0000720
721 // Is this a Universal Character Name escape?
Chris Lattner2f5add62007-04-05 06:57:15 +0000722 if (begin[0] != '\\') // If this is a normal character, consume it.
723 ResultChar = *begin++;
Nico Webera6bde812010-10-09 00:27:47 +0000724 else { // Otherwise, this is an escape character.
725 // Check for UCN.
726 if (begin[1] == 'u' || begin[1] == 'U') {
727 uint32_t utf32 = 0;
728 unsigned short UcnLen = 0;
Chris Lattnerb1ab2c22010-11-17 06:55:10 +0000729 if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
730 FullSourceLoc(Loc, PP.getSourceManager()),
Chris Lattnerbde1b812010-11-17 06:46:14 +0000731 &PP.getDiagnostics(), PP.getLangOptions())) {
Nico Webera6bde812010-10-09 00:27:47 +0000732 HadError = 1;
733 }
734 ResultChar = utf32;
735 } else {
736 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000737 ResultChar = ProcessCharEscape(begin, end, HadError,
738 FullSourceLoc(Loc,PP.getSourceManager()),
739 IsWide,
740 &PP.getDiagnostics(), PP.getTargetInfo());
Nico Webera6bde812010-10-09 00:27:47 +0000741 }
742 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000743
744 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
745 // implementation defined (C99 6.4.4.4p10).
Chris Lattner8577f622009-04-28 21:51:46 +0000746 if (NumCharsSoFar) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000747 if (IsWide) {
748 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000749 LitVal = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000750 } else {
751 // Narrow character literals act as though their value is concatenated
Chris Lattner8577f622009-04-28 21:51:46 +0000752 // in this implementation, but warn on overflow.
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000753 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000754 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000755 Warned = true;
756 }
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000757 LitVal <<= 8;
Chris Lattner2f5add62007-04-05 06:57:15 +0000758 }
759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000761 LitVal = LitVal + ResultChar;
Chris Lattner8577f622009-04-28 21:51:46 +0000762 ++NumCharsSoFar;
763 }
764
765 // If this is the second character being processed, do special handling.
766 if (NumCharsSoFar > 1) {
767 // Warn about discarding the top bits for multi-char wide-character
768 // constants (L'abcd').
769 if (IsWide)
770 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
771 else if (NumCharsSoFar != 4)
772 PP.Diag(Loc, diag::ext_multichar_character_literal);
773 else
774 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedmand8cec572009-06-01 05:25:02 +0000775 IsMultiChar = true;
Daniel Dunbara444cc22009-07-29 01:46:05 +0000776 } else
777 IsMultiChar = false;
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000778
779 // Transfer the value from APInt to uint64_t
780 Value = LitVal.getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000781
Nico Webera6bde812010-10-09 00:27:47 +0000782 if (IsWide && PP.getLangOptions().ShortWChar && Value > 0xFFFF)
783 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
784
Chris Lattner2f5add62007-04-05 06:57:15 +0000785 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
786 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
787 // character constants are not sign extended in the this implementation:
788 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattner8577f622009-04-28 21:51:46 +0000789 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000790 PP.getLangOptions().CharIsSigned)
Chris Lattner2f5add62007-04-05 06:57:15 +0000791 Value = (signed char)Value;
792}
793
794
Steve Naroff4f88b312007-03-13 22:37:02 +0000795/// string-literal: [C99 6.4.5]
796/// " [s-char-sequence] "
797/// L" [s-char-sequence] "
798/// s-char-sequence:
799/// s-char
800/// s-char-sequence s-char
801/// s-char:
802/// any source character except the double quote ",
803/// backslash \, or newline character
804/// escape-character
805/// universal-character-name
806/// escape-character: [C99 6.4.4.4]
807/// \ escape-code
808/// universal-character-name
809/// escape-code:
810/// character-escape-code
811/// octal-escape-code
812/// hex-escape-code
813/// character-escape-code: one of
814/// n t b r f v a
815/// \ ' " ?
816/// octal-escape-code:
817/// octal-digit
818/// octal-digit octal-digit
819/// octal-digit octal-digit octal-digit
820/// hex-escape-code:
821/// x hex-digit
822/// hex-escape-code hex-digit
823/// universal-character-name:
824/// \u hex-quad
825/// \U hex-quad hex-quad
826/// hex-quad:
827/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000828///
Steve Naroff4f88b312007-03-13 22:37:02 +0000829StringLiteralParser::
Chris Lattner146762e2007-07-20 16:59:19 +0000830StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattnerbde1b812010-11-17 06:46:14 +0000831 Preprocessor &pp, bool Complain)
832 : PP(pp), SM(PP.getSourceManager()), Features(PP.getLangOptions()),
833 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000834 // Scan all of the string portions, remember the max individual token length,
835 // computing a bound on the concatenated string length, and see whether any
836 // piece is a wide-string. If any of the string portions is a wide-string
837 // literal, the result is a wide-string literal [C99 6.4.5p4].
Alexis Hunt3b791862010-08-30 17:47:05 +0000838 MaxTokenLength = StringToks[0].getLength();
839 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000840 AnyWide = StringToks[0].is(tok::wide_string_literal);
Alexis Hunt3b791862010-08-30 17:47:05 +0000841
842 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000843
844 // Implement Translation Phase #6: concatenation of string literals
845 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000846 for (unsigned i = 1; i != NumStringToks; ++i) {
847 // The string could be shorter than this if it needs cleaning, but this is a
848 // reasonable bound, which is all we need.
Alexis Hunt3b791862010-08-30 17:47:05 +0000849 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump11289f42009-09-09 15:08:12 +0000850
Steve Naroff4f88b312007-03-13 22:37:02 +0000851 // Remember maximum string piece length.
Alexis Hunt3b791862010-08-30 17:47:05 +0000852 if (StringToks[i].getLength() > MaxTokenLength)
853 MaxTokenLength = StringToks[i].getLength();
Mike Stump11289f42009-09-09 15:08:12 +0000854
Steve Naroff4f88b312007-03-13 22:37:02 +0000855 // Remember if we see any wide strings.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000856 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Steve Naroff4f88b312007-03-13 22:37:02 +0000857 }
Chris Lattnerd42c29f2009-02-26 23:01:51 +0000858
Steve Naroff4f88b312007-03-13 22:37:02 +0000859 // Include space for the null terminator.
860 ++SizeBound;
Mike Stump11289f42009-09-09 15:08:12 +0000861
Steve Naroff4f88b312007-03-13 22:37:02 +0000862 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump11289f42009-09-09 15:08:12 +0000863
Steve Naroff4f88b312007-03-13 22:37:02 +0000864 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
865 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
866 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000867 if (AnyWide) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000868 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Chris Lattner2f5add62007-04-05 06:57:15 +0000869 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
870 wchar_tByteWidth /= 8;
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Steve Naroff4f88b312007-03-13 22:37:02 +0000873 // The output buffer size needs to be large enough to hold wide characters.
874 // This is a worst-case assumption which basically corresponds to L"" "long".
875 if (AnyWide)
876 SizeBound *= wchar_tByteWidth;
Mike Stump11289f42009-09-09 15:08:12 +0000877
Steve Naroff4f88b312007-03-13 22:37:02 +0000878 // Size the temporary buffer to hold the result string data.
879 ResultBuf.resize(SizeBound);
Mike Stump11289f42009-09-09 15:08:12 +0000880
Steve Naroff4f88b312007-03-13 22:37:02 +0000881 // Likewise, but for each string piece.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000882 llvm::SmallString<512> TokenBuf;
Steve Naroff4f88b312007-03-13 22:37:02 +0000883 TokenBuf.resize(MaxTokenLength);
Mike Stump11289f42009-09-09 15:08:12 +0000884
Steve Naroff4f88b312007-03-13 22:37:02 +0000885 // Loop over all the strings, getting their spelling, and expanding them to
886 // wide strings as appropriate.
887 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump11289f42009-09-09 15:08:12 +0000888
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000889 Pascal = false;
Mike Stump11289f42009-09-09 15:08:12 +0000890
Steve Naroff4f88b312007-03-13 22:37:02 +0000891 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
892 const char *ThisTokBuf = &TokenBuf[0];
893 // Get the spelling of the token, which eliminates trigraphs, etc. We know
894 // that ThisTokBuf points to a buffer that is big enough for the whole token
895 // and 'spelled' tokens can only shrink.
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000896 bool StringInvalid = false;
897 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf,
Alexis Hunt3b791862010-08-30 17:47:05 +0000898 &StringInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000899 if (StringInvalid) {
900 hadError = 1;
901 continue;
902 }
903
Steve Naroff4f88b312007-03-13 22:37:02 +0000904 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000905 bool wide = false;
Steve Naroff4f88b312007-03-13 22:37:02 +0000906 // TODO: Input character set mapping support.
Mike Stump11289f42009-09-09 15:08:12 +0000907
Steve Naroff4f88b312007-03-13 22:37:02 +0000908 // Skip L marker for wide strings.
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000909 if (ThisTokBuf[0] == 'L') {
910 wide = true;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000911 ++ThisTokBuf;
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Steve Naroff4f88b312007-03-13 22:37:02 +0000914 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
915 ++ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000916
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000917 // Check if this is a pascal string
918 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
919 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump11289f42009-09-09 15:08:12 +0000920
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000921 // If the \p sequence is found in the first token, we have a pascal string
922 // Otherwise, if we already have a pascal string, ignore the first \p
923 if (i == 0) {
924 ++ThisTokBuf;
925 Pascal = true;
926 } else if (Pascal)
927 ThisTokBuf += 2;
928 }
Mike Stump11289f42009-09-09 15:08:12 +0000929
Steve Naroff4f88b312007-03-13 22:37:02 +0000930 while (ThisTokBuf != ThisTokEnd) {
931 // Is this a span of non-escape characters?
932 if (ThisTokBuf[0] != '\\') {
933 const char *InStart = ThisTokBuf;
934 do {
935 ++ThisTokBuf;
936 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump11289f42009-09-09 15:08:12 +0000937
Steve Naroff4f88b312007-03-13 22:37:02 +0000938 // Copy the character span over.
939 unsigned Len = ThisTokBuf-InStart;
940 if (!AnyWide) {
941 memcpy(ResultPtr, InStart, Len);
942 ResultPtr += Len;
943 } else {
944 // Note: our internal rep of wide char tokens is always little-endian.
945 for (; Len; --Len, ++InStart) {
946 *ResultPtr++ = InStart[0];
947 // Add zeros at the end.
948 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff7b753d22009-03-30 23:46:03 +0000949 *ResultPtr++ = 0;
Steve Naroff4f88b312007-03-13 22:37:02 +0000950 }
951 }
952 continue;
953 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000954 // Is this a Universal Character Name escape?
Steve Naroff7b753d22009-03-30 23:46:03 +0000955 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Nico Webera6bde812010-10-09 00:27:47 +0000956 EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
957 hadError, StringToks[i].getLocation(), PP, wide,
958 Complain);
Steve Naroffc94adda2009-04-01 11:09:15 +0000959 continue;
960 }
961 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner7a02bfd2010-11-17 06:26:08 +0000962 unsigned ResultChar =
963 ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattnerbde1b812010-11-17 06:46:14 +0000964 FullSourceLoc(StringToks[i].getLocation(), SM),
965 AnyWide, Diags, Target);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Steve Naroffc94adda2009-04-01 11:09:15 +0000967 // Note: our internal rep of wide char tokens is always little-endian.
968 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump11289f42009-09-09 15:08:12 +0000969
Steve Naroffc94adda2009-04-01 11:09:15 +0000970 if (AnyWide) {
971 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
972 *ResultPtr++ = ResultChar >> i*8;
Steve Naroff4f88b312007-03-13 22:37:02 +0000973 }
974 }
975 }
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner8a24e582009-01-16 18:51:42 +0000977 if (Pascal) {
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000978 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Fariborz Jahanian93bef102010-05-28 19:40:48 +0000979 if (AnyWide)
980 ResultBuf[0] /= wchar_tByteWidth;
Chris Lattner8a24e582009-01-16 18:51:42 +0000981
982 // Verify that pascal strings aren't too large.
Douglas Gregor9af03022010-05-26 05:35:51 +0000983 if (GetStringLength() > 256 && Complain) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000984 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
985 << SourceRange(StringToks[0].getLocation(),
986 StringToks[NumStringToks-1].getLocation());
Eli Friedman1c3fb222009-04-01 03:17:08 +0000987 hadError = 1;
988 return;
989 }
Douglas Gregorb37b46e2010-07-20 14:33:20 +0000990 } else if (Complain) {
991 // Complain if this string literal has too many characters.
992 unsigned MaxChars = PP.getLangOptions().CPlusPlus? 65536
993 : PP.getLangOptions().C99 ? 4095
994 : 509;
995
996 if (GetNumStringChars() > MaxChars)
997 PP.Diag(StringToks[0].getLocation(), diag::ext_string_too_long)
998 << GetNumStringChars() << MaxChars
999 << (PP.getLangOptions().CPlusPlus? 2
1000 : PP.getLangOptions().C99 ? 1
1001 : 0)
1002 << SourceRange(StringToks[0].getLocation(),
1003 StringToks[NumStringToks-1].getLocation());
Chris Lattner8a24e582009-01-16 18:51:42 +00001004 }
Steve Naroff4f88b312007-03-13 22:37:02 +00001005}
Chris Lattnerddb71912009-02-18 19:21:10 +00001006
1007
1008/// getOffsetOfStringByte - This function returns the offset of the
1009/// specified byte of the string data represented by Token. This handles
1010/// advancing over escape sequences in the string.
1011unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattnerbde1b812010-11-17 06:46:14 +00001012 unsigned ByteNo) const {
Chris Lattnerddb71912009-02-18 19:21:10 +00001013 // Get the spelling of the token.
Chris Lattner3a324d32010-11-17 06:35:43 +00001014 llvm::SmallString<32> SpellingBuffer;
Alexis Hunt3b791862010-08-30 17:47:05 +00001015 SpellingBuffer.resize(Tok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregor7bda4b82010-03-16 05:20:39 +00001017 bool StringInvalid = false;
Chris Lattnerddb71912009-02-18 19:21:10 +00001018 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattner3a324d32010-11-17 06:35:43 +00001019 unsigned TokLen = Preprocessor::getSpelling(Tok, SpellingPtr, SM, Features,
Chris Lattner30d4c922010-11-17 06:31:48 +00001020 &StringInvalid);
Chris Lattner7a02bfd2010-11-17 06:26:08 +00001021 if (StringInvalid)
Douglas Gregor7bda4b82010-03-16 05:20:39 +00001022 return 0;
Chris Lattnerddb71912009-02-18 19:21:10 +00001023
1024 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
1025
Mike Stump11289f42009-09-09 15:08:12 +00001026
Chris Lattnerddb71912009-02-18 19:21:10 +00001027 const char *SpellingStart = SpellingPtr;
1028 const char *SpellingEnd = SpellingPtr+TokLen;
1029
1030 // Skip over the leading quote.
1031 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1032 ++SpellingPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001033
Chris Lattnerddb71912009-02-18 19:21:10 +00001034 // Skip over bytes until we find the offset we're looking for.
1035 while (ByteNo) {
1036 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump11289f42009-09-09 15:08:12 +00001037
Chris Lattnerddb71912009-02-18 19:21:10 +00001038 // Step over non-escapes simply.
1039 if (*SpellingPtr != '\\') {
1040 ++SpellingPtr;
1041 --ByteNo;
1042 continue;
1043 }
Mike Stump11289f42009-09-09 15:08:12 +00001044
Chris Lattnerddb71912009-02-18 19:21:10 +00001045 // Otherwise, this is an escape character. Advance over it.
1046 bool HadError = false;
1047 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Chris Lattner3a324d32010-11-17 06:35:43 +00001048 FullSourceLoc(Tok.getLocation(), SM),
Chris Lattner7a02bfd2010-11-17 06:26:08 +00001049 false, Diags, Target);
Chris Lattnerddb71912009-02-18 19:21:10 +00001050 assert(!HadError && "This method isn't valid on erroneous strings");
1051 --ByteNo;
1052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattnerddb71912009-02-18 19:21:10 +00001054 return SpellingPtr-SpellingStart;
1055}