blob: 0739c88d20ce53bedc38b1dbd799f8172072ed84 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the NumericLiteralParser, CharLiteralParser, and
11// StringLiteralParser interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/LiteralSupport.h"
16#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Lex/LexDiagnostic.h"
Chris Lattner136f93a2007-07-16 06:55:01 +000018#include "clang/Basic/TargetInfo.h"
Erick Tryzelaare9f195f2009-08-16 23:36:28 +000019#include "llvm/ADT/StringRef.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
21using namespace clang;
22
23/// 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 Lattner91f54ce2010-11-17 06:26:08 +000036 FullSourceLoc Loc, bool IsWide,
37 Diagnostic *Diags, const TargetInfo &Target) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +000047
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner91f54ce2010-11-17 06:26:08 +000057 if (Diags)
58 Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +000059 ResultChar = 27;
60 break;
Eli Friedman3c548012009-06-10 01:32:39 +000061 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +000062 if (Diags)
63 Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +000064 ResultChar = 27;
65 break;
Reid Spencer5f016e22007-07-11 17:01:13 +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;
Reid Spencer5f016e22007-07-11 17:01:13 +000081 case 'x': { // Hex escape.
82 ResultChar = 0;
83 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +000084 if (Diags)
85 Diags->Report(Loc, diag::err_hex_escape_no_digits);
Reid Spencer5f016e22007-07-11 17:01:13 +000086 HadError = 1;
87 break;
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Hex escapes are a maximal series of hex digits.
91 bool Overflow = false;
92 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
93 int CharVal = HexDigitValue(ThisTokBuf[0]);
94 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +000095 // About to shift out a digit?
96 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ResultChar <<= 4;
98 ResultChar |= CharVal;
99 }
100
101 // See if any bits will be truncated when evaluated as a character.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000102 unsigned CharWidth =
103 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
106 Overflow = true;
107 ResultChar &= ~0U >> (32-CharWidth);
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // Check for overflow.
Chris Lattner91f54ce2010-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 break;
114 }
115 case '0': case '1': case '2': case '3':
116 case '4': case '5': case '6': case '7': {
117 // Octal escapes.
118 --ThisTokBuf;
119 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 Stump1eb44332009-09-09 15:08:12 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // Check for overflow. Reject '\777', but not L'\777'.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000132 unsigned CharWidth =
133 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000136 if (Diags)
137 Diags->Report(Loc, diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 ResultChar &= ~0U >> (32-CharWidth);
139 }
140 break;
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner91f54ce2010-11-17 06:26:08 +0000146 if (Diags)
147 Diags->Report(Loc, diag::ext_nonstandard_escape)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000148 << std::string()+(char)ResultChar;
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000149 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000151 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000152 break;
153
Chris Lattnerac92d822008-11-22 07:23:31 +0000154 if (isgraph(ThisTokBuf[0]))
Chris Lattner91f54ce2010-11-17 06:26:08 +0000155 Diags->Report(Loc, diag::ext_unknown_escape)
156 << std::string()+(char)ResultChar;
Chris Lattnerac92d822008-11-22 07:23:31 +0000157 else
Chris Lattner91f54ce2010-11-17 06:26:08 +0000158 Diags->Report(Loc, diag::ext_unknown_escape)
159 << "x"+llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 break;
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 return ResultChar;
164}
165
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000166/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-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 Lattner37dd3ec2010-06-15 18:06:43 +0000170 SourceLocation Loc, Preprocessor &PP,
171 bool Complain) {
Nico Webera0f15b02010-10-06 04:57:26 +0000172 if (!PP.getLangOptions().CPlusPlus && !PP.getLangOptions().C99)
173 PP.Diag(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Steve Naroff4e93b342009-04-01 11:09:15 +0000175 // Save the beginning of the string (for error diagnostics).
176 const char *ThisTokBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000178 // Skip the '\u' char's.
179 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000180
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000181 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000182 if (Complain)
183 PP.Diag(Loc, diag::err_ucn_escape_no_digits);
Nico Weber59705ae2010-10-09 00:27:47 +0000184 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000185 }
Nico Weber59705ae2010-10-09 00:27:47 +0000186 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000187 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000188 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff0e3e3eb2009-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 Weber59705ae2010-10-09 00:27:47 +0000195 if (UcnLenSave) {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000196 if (Complain)
197 PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
198 diag::err_ucn_escape_incomplete);
Nico Weber59705ae2010-10-09 00:27:47 +0000199 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000202 if ((UcnVal < 0xa0 &&
203 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump1eb44332009-09-09 15:08:12 +0000204 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000205 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000206 if (Complain)
207 PP.Diag(Loc, diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000208 return false;
209 }
210 return true;
211}
212
213/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
214/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
215/// StringLiteralParser. When we decide to implement UCN's for identifiers,
216/// we will likely rework our support for UCN's.
217static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
218 char *&ResultBuf, bool &HadError,
219 SourceLocation Loc, Preprocessor &PP,
220 bool wide,
221 bool Complain) {
222 typedef uint32_t UTF32;
223 UTF32 UcnVal = 0;
224 unsigned short UcnLen = 0;
225 if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd,
226 UcnVal, UcnLen, Loc, PP, Complain)) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000227 HadError = 1;
228 return;
229 }
Nico Weber59705ae2010-10-09 00:27:47 +0000230
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000231 if (wide) {
Nico Weber59705ae2010-10-09 00:27:47 +0000232 (void)UcnLen;
233 assert((UcnLen== 4 || UcnLen== 8) &&
234 "EncodeUCNEscape - only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000235
236 if (!PP.getLangOptions().ShortWChar) {
237 // Note: our internal rep of wide char tokens is always little-endian.
238 *ResultBuf++ = (UcnVal & 0x000000FF);
239 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
240 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
241 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
242 return;
243 }
244
245 // Convert to UTF16.
246 if (UcnVal < (UTF32)0xFFFF) {
247 *ResultBuf++ = (UcnVal & 0x000000FF);
248 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
249 return;
250 }
251 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
252
253 typedef uint16_t UTF16;
254 UcnVal -= 0x10000;
255 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
256 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
257 *ResultBuf++ = (surrogate1 & 0x000000FF);
258 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
259 *ResultBuf++ = (surrogate2 & 0x000000FF);
260 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000261 return;
262 }
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000263 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
264 // The conversion below was inspired by:
265 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000266 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000267 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000268
269 unsigned short bytesToWrite = 0;
270 if (UcnVal < (UTF32)0x80)
271 bytesToWrite = 1;
272 else if (UcnVal < (UTF32)0x800)
273 bytesToWrite = 2;
274 else if (UcnVal < (UTF32)0x10000)
275 bytesToWrite = 3;
276 else
277 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000279 const unsigned byteMask = 0xBF;
280 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000282 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000283 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000284 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000285 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000286 };
287 // Finally, we write the bytes into ResultBuf.
288 ResultBuf += bytesToWrite;
289 switch (bytesToWrite) { // note: everything falls through.
290 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
291 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
292 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
293 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
294 }
295 // Update the buffer.
296 ResultBuf += bytesToWrite;
297}
Reid Spencer5f016e22007-07-11 17:01:13 +0000298
299
300/// integer-constant: [C99 6.4.4.1]
301/// decimal-constant integer-suffix
302/// octal-constant integer-suffix
303/// hexadecimal-constant integer-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000304/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000305/// nonzero-digit
306/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000307/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000308/// 0
309/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000310/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000311/// hexadecimal-prefix hexadecimal-digit
312/// hexadecimal-constant hexadecimal-digit
313/// hexadecimal-prefix: one of
314/// 0x 0X
315/// integer-suffix:
316/// unsigned-suffix [long-suffix]
317/// unsigned-suffix [long-long-suffix]
318/// long-suffix [unsigned-suffix]
319/// long-long-suffix [unsigned-sufix]
320/// nonzero-digit:
321/// 1 2 3 4 5 6 7 8 9
322/// octal-digit:
323/// 0 1 2 3 4 5 6 7
324/// hexadecimal-digit:
325/// 0 1 2 3 4 5 6 7 8 9
326/// a b c d e f
327/// A B C D E F
328/// unsigned-suffix: one of
329/// u U
330/// long-suffix: one of
331/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000332/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000333/// ll LL
334///
335/// floating-constant: [C99 6.4.4.2]
336/// TODO: add rules...
337///
Reid Spencer5f016e22007-07-11 17:01:13 +0000338NumericLiteralParser::
339NumericLiteralParser(const char *begin, const char *end,
340 SourceLocation TokLoc, Preprocessor &pp)
341 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000343 // This routine assumes that the range begin/end matches the regex for integer
344 // and FP constants (specifically, the 'pp-number' regex), and assumes that
345 // the byte at "*end" is both valid and not part of the regex. Because of
346 // this, it doesn't have to check for 'overscan' in various places.
347 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
348 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 s = DigitsBegin = begin;
351 saw_exponent = false;
352 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 isLong = false;
354 isUnsigned = false;
355 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000356 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000357 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000358 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000362 ParseNumberStartingWithZero(TokLoc);
363 if (hadError)
364 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 } else { // the first digit is non-zero
366 radix = 10;
367 s = SkipDigits(s);
368 if (s == ThisTokEnd) {
369 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000370 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000371 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000372 diag::err_invalid_decimal_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000373 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 return;
375 } else if (*s == '.') {
376 s++;
377 saw_period = true;
378 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000379 }
Chris Lattner4411f462008-09-29 23:12:31 +0000380 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000381 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 s++;
383 saw_exponent = true;
384 if (*s == '+' || *s == '-') s++; // sign
385 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000386 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000388 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000389 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
390 diag::err_exponent_has_no_digits);
391 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000392 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 }
394 }
395 }
396
397 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattner506b8de2007-08-26 01:58:14 +0000399 // Parse the suffix. At this point we can classify whether we have an FP or
400 // integer constant.
401 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner506b8de2007-08-26 01:58:14 +0000403 // Loop over all of the characters of the suffix. If we see something bad,
404 // we break out of the loop.
405 for (; s != ThisTokEnd; ++s) {
406 switch (*s) {
407 case 'f': // FP Suffix for "float"
408 case 'F':
409 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000410 if (isFloat || isLong) break; // FF, LF invalid.
411 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000412 continue; // Success.
413 case 'u':
414 case 'U':
415 if (isFPConstant) break; // Error for floating constant.
416 if (isUnsigned) break; // Cannot be repeated.
417 isUnsigned = true;
418 continue; // Success.
419 case 'l':
420 case 'L':
421 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000422 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner506b8de2007-08-26 01:58:14 +0000424 // Check for long long. The L's need to be adjacent and the same case.
425 if (s+1 != ThisTokEnd && s[1] == s[0]) {
426 if (isFPConstant) break; // long long invalid for floats.
427 isLongLong = true;
428 ++s; // Eat both of them.
429 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000432 continue; // Success.
433 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000434 case 'I':
Steve Naroff0c29b222008-04-04 21:02:54 +0000435 if (PP.getLangOptions().Microsoft) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000436 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000437
Steve Naroff0c29b222008-04-04 21:02:54 +0000438 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000439 if (s + 1 != ThisTokEnd) {
440 switch (s[1]) {
441 case '8':
442 s += 2; // i8 suffix
443 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000444 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000445 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000446 if (s + 2 == ThisTokEnd) break;
447 if (s[2] == '6') s += 3; // i16 suffix
448 else if (s[2] == '2') {
449 if (s + 3 == ThisTokEnd) break;
450 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000451 }
452 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000453 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000454 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000455 if (s + 2 == ThisTokEnd) break;
456 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000457 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000458 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000459 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000460 if (s + 2 == ThisTokEnd) break;
461 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000462 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000463 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000464 default:
465 break;
466 }
467 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000468 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000469 }
470 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000471 case 'j':
472 case 'J':
473 if (isImaginary) break; // Cannot be repeated.
474 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
475 diag::ext_imaginary_constant);
476 isImaginary = true;
477 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000479 // If we reached here, there was an error.
480 break;
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Chris Lattner506b8de2007-08-26 01:58:14 +0000483 // Report an error if there are any.
484 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000485 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
486 isFPConstant ? diag::err_invalid_suffix_float_constant :
487 diag::err_invalid_suffix_integer_constant)
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000488 << llvm::StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000489 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000490 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 }
492}
493
Chris Lattner368328c2008-06-30 06:39:54 +0000494/// ParseNumberStartingWithZero - This method is called when the first character
495/// of the number is found to be a zero. This means it is either an octal
496/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000497/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000498/// radix etc.
499void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
500 assert(s[0] == '0' && "Invalid method call");
501 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner368328c2008-06-30 06:39:54 +0000503 // Handle a hex number like 0x1234.
504 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
505 s++;
506 radix = 16;
507 DigitsBegin = s;
508 s = SkipHexDigits(s);
509 if (s == ThisTokEnd) {
510 // Done.
511 } else if (*s == '.') {
512 s++;
513 saw_period = true;
514 s = SkipHexDigits(s);
515 }
516 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000517 // binary exponent is required.
Sean Hunt8c723402010-01-10 23:37:56 +0000518 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner368328c2008-06-30 06:39:54 +0000519 const char *Exponent = s;
520 s++;
521 saw_exponent = true;
522 if (*s == '+' || *s == '-') s++; // sign
523 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000524 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000525 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
526 diag::err_exponent_has_no_digits);
527 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000528 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000529 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000530 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Sean Hunt8c723402010-01-10 23:37:56 +0000532 // In C++0x, we cannot support hexadecmial floating literals because
533 // they conflict with user-defined literals, so we warn in previous
534 // versions of C++ by default.
535 if (PP.getLangOptions().CPlusPlus)
536 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
537 else if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000538 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000539 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000540 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
541 diag::err_hexconstant_requires_exponent);
542 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000543 }
544 return;
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattner368328c2008-06-30 06:39:54 +0000547 // Handle simple binary numbers 0b01010
548 if (*s == 'b' || *s == 'B') {
549 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000550 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000551 ++s;
552 radix = 2;
553 DigitsBegin = s;
554 s = SkipBinaryDigits(s);
555 if (s == ThisTokEnd) {
556 // Done.
557 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000558 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000559 diag::err_invalid_binary_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000560 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000561 }
Chris Lattner413d3552008-06-30 06:44:49 +0000562 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000563 return;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Chris Lattner368328c2008-06-30 06:39:54 +0000566 // For now, the radix is set to 8. If we discover that we have a
567 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000568 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000569 radix = 8;
570 DigitsBegin = s;
571 s = SkipOctalDigits(s);
572 if (s == ThisTokEnd)
573 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner413d3552008-06-30 06:44:49 +0000575 // If we have some other non-octal digit that *is* a decimal digit, see if
576 // this is part of a floating point number like 094.123 or 09e1.
577 if (isdigit(*s)) {
578 const char *EndDecimal = SkipDigits(s);
579 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
580 s = EndDecimal;
581 radix = 10;
582 }
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner413d3552008-06-30 06:44:49 +0000585 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
586 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000587 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000588 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000589 diag::err_invalid_octal_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000590 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000591 return;
592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Chris Lattner368328c2008-06-30 06:39:54 +0000594 if (*s == '.') {
595 s++;
596 radix = 10;
597 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000598 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000599 }
600 if (*s == 'e' || *s == 'E') { // exponent
601 const char *Exponent = s;
602 s++;
603 radix = 10;
604 saw_exponent = true;
605 if (*s == '+' || *s == '-') s++; // sign
606 const char *first_non_digit = SkipDigits(s);
607 if (first_non_digit != s) {
608 s = first_non_digit;
609 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000610 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000611 diag::err_exponent_has_no_digits);
612 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000613 return;
614 }
615 }
616}
617
618
Reid Spencer5f016e22007-07-11 17:01:13 +0000619/// GetIntegerValue - Convert this numeric literal value to an APInt that
620/// matches Val's input width. If there is an overflow, set Val to the low bits
621/// of the result and return true. Otherwise, return false.
622bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000623 // Fast path: Compute a conservative bound on the maximum number of
624 // bits per digit in this radix. If we can't possibly overflow a
625 // uint64 based on that bound then do the simple conversion to
626 // integer. This avoids the expensive overflow checking below, and
627 // handles the common cases that matter (small decimal integers and
628 // hex/octal values which don't overflow).
629 unsigned MaxBitsPerDigit = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000630 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbara179be32008-10-16 07:32:01 +0000631 MaxBitsPerDigit += 1;
632 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
633 uint64_t N = 0;
634 for (s = DigitsBegin; s != SuffixBegin; ++s)
635 N = N*radix + HexDigitValue(*s);
636
637 // This will truncate the value to Val's input width. Simply check
638 // for overflow by comparing.
639 Val = N;
640 return Val.getZExtValue() != N;
641 }
642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 Val = 0;
644 s = DigitsBegin;
645
646 llvm::APInt RadixVal(Val.getBitWidth(), radix);
647 llvm::APInt CharVal(Val.getBitWidth(), 0);
648 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 bool OverflowOccurred = false;
651 while (s < SuffixBegin) {
652 unsigned C = HexDigitValue(*s++);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 // If this letter is out of bound for this radix, reject it.
655 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 // Add the digit to the value in the appropriate radix. If adding in digits
660 // made the value smaller, then this overflowed.
661 OldVal = Val;
662
663 // Multiply by radix, did overflow occur on the multiply?
664 Val *= RadixVal;
665 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000668 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 OverflowOccurred |= Val.ult(CharVal);
671 }
672 return OverflowOccurred;
673}
674
John McCall94c939d2009-12-24 09:08:04 +0000675llvm::APFloat::opStatus
676NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000677 using llvm::APFloat;
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000678 using llvm::StringRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000680 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000681 return Result.convertFromString(StringRef(ThisTokBegin, n),
682 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000683}
684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685
686CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
687 SourceLocation Loc, Preprocessor &PP) {
688 // At this point we know that the character matches the regex "L?'.*'".
689 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 // Determine if this is a wide character.
692 IsWide = begin[0] == 'L';
693 if (IsWide) ++begin;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // Skip over the entry quote.
696 assert(begin[0] == '\'' && "Invalid token lexed");
697 ++begin;
698
Mike Stump1eb44332009-09-09 15:08:12 +0000699 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000700 // upto 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000702 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000704 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
705 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
706 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
707 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
708 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000709
Mike Stump1eb44332009-09-09 15:08:12 +0000710 // This is what we will use for overflow detection
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000711 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattnere3ad8812009-04-28 21:51:46 +0000713 unsigned NumCharsSoFar = 0;
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000714 bool Warned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 while (begin[0] != '\'') {
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000716 uint64_t ResultChar;
Nico Weber59705ae2010-10-09 00:27:47 +0000717
718 // Is this a Universal Character Name escape?
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 if (begin[0] != '\\') // If this is a normal character, consume it.
720 ResultChar = *begin++;
Nico Weber59705ae2010-10-09 00:27:47 +0000721 else { // Otherwise, this is an escape character.
722 // Check for UCN.
723 if (begin[1] == 'u' || begin[1] == 'U') {
724 uint32_t utf32 = 0;
725 unsigned short UcnLen = 0;
726 if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
727 Loc, PP, /*Complain=*/true)) {
728 HadError = 1;
729 }
730 ResultChar = utf32;
731 } else {
732 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000733 ResultChar = ProcessCharEscape(begin, end, HadError,
734 FullSourceLoc(Loc,PP.getSourceManager()),
735 IsWide,
736 &PP.getDiagnostics(), PP.getTargetInfo());
Nico Weber59705ae2010-10-09 00:27:47 +0000737 }
738 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000739
740 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
741 // implementation defined (C99 6.4.4.4p10).
Chris Lattnere3ad8812009-04-28 21:51:46 +0000742 if (NumCharsSoFar) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 if (IsWide) {
744 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000745 LitVal = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 } else {
747 // Narrow character literals act as though their value is concatenated
Chris Lattnere3ad8812009-04-28 21:51:46 +0000748 // in this implementation, but warn on overflow.
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000749 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000751 Warned = true;
752 }
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000753 LitVal <<= 8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 }
755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000757 LitVal = LitVal + ResultChar;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000758 ++NumCharsSoFar;
759 }
760
761 // If this is the second character being processed, do special handling.
762 if (NumCharsSoFar > 1) {
763 // Warn about discarding the top bits for multi-char wide-character
764 // constants (L'abcd').
765 if (IsWide)
766 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
767 else if (NumCharsSoFar != 4)
768 PP.Diag(Loc, diag::ext_multichar_character_literal);
769 else
770 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +0000771 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +0000772 } else
773 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000774
775 // Transfer the value from APInt to uint64_t
776 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Nico Weber59705ae2010-10-09 00:27:47 +0000778 if (IsWide && PP.getLangOptions().ShortWChar && Value > 0xFFFF)
779 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
782 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
783 // character constants are not sign extended in the this implementation:
784 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattnere3ad8812009-04-28 21:51:46 +0000785 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman15b91762009-06-05 07:05:05 +0000786 PP.getLangOptions().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 Value = (signed char)Value;
788}
789
790
791/// string-literal: [C99 6.4.5]
792/// " [s-char-sequence] "
793/// L" [s-char-sequence] "
794/// s-char-sequence:
795/// s-char
796/// s-char-sequence s-char
797/// s-char:
798/// any source character except the double quote ",
799/// backslash \, or newline character
800/// escape-character
801/// universal-character-name
802/// escape-character: [C99 6.4.4.4]
803/// \ escape-code
804/// universal-character-name
805/// escape-code:
806/// character-escape-code
807/// octal-escape-code
808/// hex-escape-code
809/// character-escape-code: one of
810/// n t b r f v a
811/// \ ' " ?
812/// octal-escape-code:
813/// octal-digit
814/// octal-digit octal-digit
815/// octal-digit octal-digit octal-digit
816/// hex-escape-code:
817/// x hex-digit
818/// hex-escape-code hex-digit
819/// universal-character-name:
820/// \u hex-quad
821/// \U hex-quad hex-quad
822/// hex-quad:
823/// hex-digit hex-digit hex-digit hex-digit
824///
825StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000826StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Sean Hunt6cf75022010-08-30 17:47:05 +0000827 Preprocessor &pp, bool Complain) : PP(pp) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 // Scan all of the string portions, remember the max individual token length,
829 // computing a bound on the concatenated string length, and see whether any
830 // piece is a wide-string. If any of the string portions is a wide-string
831 // literal, the result is a wide-string literal [C99 6.4.5p4].
Sean Hunt6cf75022010-08-30 17:47:05 +0000832 MaxTokenLength = StringToks[0].getLength();
833 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000834 AnyWide = StringToks[0].is(tok::wide_string_literal);
Sean Hunt6cf75022010-08-30 17:47:05 +0000835
836 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000837
838 // Implement Translation Phase #6: concatenation of string literals
839 /// (C99 5.1.1.2p1). The common case is only one string fragment.
840 for (unsigned i = 1; i != NumStringToks; ++i) {
841 // The string could be shorter than this if it needs cleaning, but this is a
842 // reasonable bound, which is all we need.
Sean Hunt6cf75022010-08-30 17:47:05 +0000843 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +0000846 if (StringToks[i].getLength() > MaxTokenLength)
847 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 // Remember if we see any wide strings.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000850 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 // Include space for the null terminator.
854 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
859 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
860 wchar_tByteWidth = ~0U;
861 if (AnyWide) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000862 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
864 wchar_tByteWidth /= 8;
865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 // The output buffer size needs to be large enough to hold wide characters.
868 // This is a worst-case assumption which basically corresponds to L"" "long".
869 if (AnyWide)
870 SizeBound *= wchar_tByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 // Size the temporary buffer to hold the result string data.
873 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 // Likewise, but for each string piece.
876 llvm::SmallString<512> TokenBuf;
877 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 // Loop over all the strings, getting their spelling, and expanding them to
880 // wide strings as appropriate.
881 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Anders Carlssonee98ac52007-10-15 02:50:23 +0000883 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
886 const char *ThisTokBuf = &TokenBuf[0];
887 // Get the spelling of the token, which eliminates trigraphs, etc. We know
888 // that ThisTokBuf points to a buffer that is big enough for the whole token
889 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000890 bool StringInvalid = false;
891 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf,
Sean Hunt6cf75022010-08-30 17:47:05 +0000892 &StringInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000893 if (StringInvalid) {
894 hadError = 1;
895 continue;
896 }
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000899 bool wide = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 // Skip L marker for wide strings.
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000903 if (ThisTokBuf[0] == 'L') {
904 wide = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
909 ++ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Anders Carlssonee98ac52007-10-15 02:50:23 +0000911 // Check if this is a pascal string
912 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
913 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Anders Carlssonee98ac52007-10-15 02:50:23 +0000915 // If the \p sequence is found in the first token, we have a pascal string
916 // Otherwise, if we already have a pascal string, ignore the first \p
917 if (i == 0) {
918 ++ThisTokBuf;
919 Pascal = true;
920 } else if (Pascal)
921 ThisTokBuf += 2;
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 while (ThisTokBuf != ThisTokEnd) {
925 // Is this a span of non-escape characters?
926 if (ThisTokBuf[0] != '\\') {
927 const char *InStart = ThisTokBuf;
928 do {
929 ++ThisTokBuf;
930 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 // Copy the character span over.
933 unsigned Len = ThisTokBuf-InStart;
934 if (!AnyWide) {
935 memcpy(ResultPtr, InStart, Len);
936 ResultPtr += Len;
937 } else {
938 // Note: our internal rep of wide char tokens is always little-endian.
939 for (; Len; --Len, ++InStart) {
940 *ResultPtr++ = InStart[0];
941 // Add zeros at the end.
942 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000943 *ResultPtr++ = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 }
945 }
946 continue;
947 }
Steve Naroff4e93b342009-04-01 11:09:15 +0000948 // Is this a Universal Character Name escape?
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000949 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Nico Weber59705ae2010-10-09 00:27:47 +0000950 EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
951 hadError, StringToks[i].getLocation(), PP, wide,
952 Complain);
Steve Naroff4e93b342009-04-01 11:09:15 +0000953 continue;
954 }
955 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000956 Diagnostic *Diags = Complain ? &PP.getDiagnostics() : 0;
957 unsigned ResultChar =
958 ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
959 FullSourceLoc(StringToks[i].getLocation(),
960 PP.getSourceManager()),
961 AnyWide, Diags,
962 PP.getTargetInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Steve Naroff4e93b342009-04-01 11:09:15 +0000964 // Note: our internal rep of wide char tokens is always little-endian.
965 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Steve Naroff4e93b342009-04-01 11:09:15 +0000967 if (AnyWide) {
968 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
969 *ResultPtr++ = ResultChar >> i*8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 }
971 }
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000974 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +0000975 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Fariborz Jahanian64a80342010-05-28 19:40:48 +0000976 if (AnyWide)
977 ResultBuf[0] /= wchar_tByteWidth;
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000978
979 // Verify that pascal strings aren't too large.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000980 if (GetStringLength() > 256 && Complain) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000981 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
982 << SourceRange(StringToks[0].getLocation(),
983 StringToks[NumStringToks-1].getLocation());
Eli Friedman57d7dde2009-04-01 03:17:08 +0000984 hadError = 1;
985 return;
986 }
Douglas Gregor427c4922010-07-20 14:33:20 +0000987 } else if (Complain) {
988 // Complain if this string literal has too many characters.
989 unsigned MaxChars = PP.getLangOptions().CPlusPlus? 65536
990 : PP.getLangOptions().C99 ? 4095
991 : 509;
992
993 if (GetNumStringChars() > MaxChars)
994 PP.Diag(StringToks[0].getLocation(), diag::ext_string_too_long)
995 << GetNumStringChars() << MaxChars
996 << (PP.getLangOptions().CPlusPlus? 2
997 : PP.getLangOptions().C99 ? 1
998 : 0)
999 << SourceRange(StringToks[0].getLocation(),
1000 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +00001001 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001002}
Chris Lattner719e6152009-02-18 19:21:10 +00001003
1004
1005/// getOffsetOfStringByte - This function returns the offset of the
1006/// specified byte of the string data represented by Token. This handles
1007/// advancing over escape sequences in the string.
1008unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
1009 unsigned ByteNo,
Chris Lattnerca1475e2010-11-17 06:35:43 +00001010 const SourceManager &SM,
1011 const LangOptions &Features,
Chris Lattner91f54ce2010-11-17 06:26:08 +00001012 const TargetInfo &Target,
1013 Diagnostic *Diags) {
Chris Lattner719e6152009-02-18 19:21:10 +00001014 // Get the spelling of the token.
Chris Lattnerca1475e2010-11-17 06:35:43 +00001015 llvm::SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001016 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregor50f6af72010-03-16 05:20:39 +00001018 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001019 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerca1475e2010-11-17 06:35:43 +00001020 unsigned TokLen = Preprocessor::getSpelling(Tok, SpellingPtr, SM, Features,
Chris Lattner48cf9822010-11-17 06:31:48 +00001021 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001022 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001023 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001024
1025 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
1026
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner719e6152009-02-18 19:21:10 +00001028 const char *SpellingStart = SpellingPtr;
1029 const char *SpellingEnd = SpellingPtr+TokLen;
1030
1031 // Skip over the leading quote.
1032 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1033 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Chris Lattner719e6152009-02-18 19:21:10 +00001035 // Skip over bytes until we find the offset we're looking for.
1036 while (ByteNo) {
1037 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Chris Lattner719e6152009-02-18 19:21:10 +00001039 // Step over non-escapes simply.
1040 if (*SpellingPtr != '\\') {
1041 ++SpellingPtr;
1042 --ByteNo;
1043 continue;
1044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Chris Lattner719e6152009-02-18 19:21:10 +00001046 // Otherwise, this is an escape character. Advance over it.
1047 bool HadError = false;
1048 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Chris Lattnerca1475e2010-11-17 06:35:43 +00001049 FullSourceLoc(Tok.getLocation(), SM),
Chris Lattner91f54ce2010-11-17 06:26:08 +00001050 false, Diags, Target);
Chris Lattner719e6152009-02-18 19:21:10 +00001051 assert(!HadError && "This method isn't valid on erroneous strings");
1052 --ByteNo;
1053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner719e6152009-02-18 19:21:10 +00001055 return SpellingPtr-SpellingStart;
1056}