blob: 9b7c46f091166046399f8f689c9255019fae1c8e [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 Lattnerc10adde2007-05-20 05:00:58 +000036 SourceLocation Loc, bool IsWide,
Douglas Gregor9af03022010-05-26 05:35:51 +000037 Preprocessor &PP, bool Complain) {
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':
Douglas Gregor9af03022010-05-26 05:35:51 +000057 if (Complain)
58 PP.Diag(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':
Douglas Gregor9af03022010-05-26 05:35:51 +000062 if (Complain)
63 PP.Diag(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)) {
Douglas Gregor9af03022010-05-26 05:35:51 +000084 if (Complain)
85 PP.Diag(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.
Alisdair Meredithed28f6e2009-07-14 08:10:06 +0000102 unsigned CharWidth = IsWide
103 ? PP.getTargetInfo().getWCharWidth()
104 : PP.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattnerc10adde2007-05-20 05:00:58 +0000106 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
107 Overflow = true;
108 ResultChar &= ~0U >> (32-CharWidth);
109 }
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnerc10adde2007-05-20 05:00:58 +0000111 // Check for overflow.
Douglas Gregor9af03022010-05-26 05:35:51 +0000112 if (Overflow && Complain) // Too many digits to fit in
Chris Lattnerc10adde2007-05-20 05:00:58 +0000113 PP.Diag(Loc, diag::warn_hex_escape_too_large);
Chris Lattner2f5add62007-04-05 06:57:15 +0000114 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000115 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000116 case '0': case '1': case '2': case '3':
Chris Lattner812eda82007-05-20 05:17:04 +0000117 case '4': case '5': case '6': case '7': {
Chris Lattner2f5add62007-04-05 06:57:15 +0000118 // Octal escapes.
Chris Lattner3f4b6e32007-06-09 06:20:47 +0000119 --ThisTokBuf;
Chris Lattner812eda82007-05-20 05:17:04 +0000120 ResultChar = 0;
121
122 // Octal escapes are a series of octal digits with maximum length 3.
123 // "\0123" is a two digit sequence equal to "\012" "3".
124 unsigned NumDigits = 0;
125 do {
126 ResultChar <<= 3;
127 ResultChar |= *ThisTokBuf++ - '0';
128 ++NumDigits;
129 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
130 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump11289f42009-09-09 15:08:12 +0000131
Chris Lattner812eda82007-05-20 05:17:04 +0000132 // Check for overflow. Reject '\777', but not L'\777'.
Alisdair Meredithed28f6e2009-07-14 08:10:06 +0000133 unsigned CharWidth = IsWide
134 ? PP.getTargetInfo().getWCharWidth()
135 : PP.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000136
Chris Lattner812eda82007-05-20 05:17:04 +0000137 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Douglas Gregor9af03022010-05-26 05:35:51 +0000138 if (Complain)
139 PP.Diag(Loc, diag::warn_octal_escape_too_large);
Chris Lattner812eda82007-05-20 05:17:04 +0000140 ResultChar &= ~0U >> (32-CharWidth);
141 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000142 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000143 }
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattner2f5add62007-04-05 06:57:15 +0000145 // Otherwise, these are not valid escapes.
146 case '(': case '{': case '[': case '%':
147 // GCC accepts these as extensions. We warn about them as such though.
Douglas Gregor9af03022010-05-26 05:35:51 +0000148 if (Complain)
149 PP.Diag(Loc, diag::ext_nonstandard_escape)
150 << std::string()+(char)ResultChar;
Eli Friedman5d72d412009-04-28 00:51:18 +0000151 break;
Chris Lattner2f5add62007-04-05 06:57:15 +0000152 default:
Douglas Gregor9af03022010-05-26 05:35:51 +0000153 if (!Complain)
154 break;
155
Chris Lattner59acca52008-11-22 07:23:31 +0000156 if (isgraph(ThisTokBuf[0]))
Chris Lattnere05c4df2008-11-18 21:48:13 +0000157 PP.Diag(Loc, diag::ext_unknown_escape) << std::string()+(char)ResultChar;
Chris Lattner59acca52008-11-22 07:23:31 +0000158 else
Chris Lattnere05c4df2008-11-18 21:48:13 +0000159 PP.Diag(Loc, diag::ext_unknown_escape) << "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
167/// convert the UTF32 to UTF8. This is a subroutine of StringLiteralParser.
168/// When we decide to implement UCN's for character constants and identifiers,
169/// we will likely rework our support for UCN's.
Mike Stump11289f42009-09-09 15:08:12 +0000170static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
171 char *&ResultBuf, bool &HadError,
Chris Lattnerc548be92010-06-15 18:06:43 +0000172 SourceLocation Loc, Preprocessor &PP,
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000173 bool wide,
Chris Lattnerc548be92010-06-15 18:06:43 +0000174 bool Complain) {
Nico Weber9762e0a2010-10-06 04:57:26 +0000175 if (!PP.getLangOptions().CPlusPlus && !PP.getLangOptions().C99)
176 PP.Diag(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump11289f42009-09-09 15:08:12 +0000177
Steve Naroffc94adda2009-04-01 11:09:15 +0000178 // Save the beginning of the string (for error diagnostics).
179 const char *ThisTokBegin = ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Steve Naroff7b753d22009-03-30 23:46:03 +0000181 // Skip the '\u' char's.
182 ThisTokBuf += 2;
Chris Lattner2f5add62007-04-05 06:57:15 +0000183
Steve Naroff7b753d22009-03-30 23:46:03 +0000184 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Douglas Gregor9af03022010-05-26 05:35:51 +0000185 if (Complain)
186 PP.Diag(Loc, diag::err_ucn_escape_no_digits);
Steve Naroff7b753d22009-03-30 23:46:03 +0000187 HadError = 1;
188 return;
189 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000190 typedef uint32_t UTF32;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Steve Naroff7b753d22009-03-30 23:46:03 +0000192 UTF32 UcnVal = 0;
193 unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000194 unsigned short UcnLenSave = UcnLen;
Steve Naroff7b753d22009-03-30 23:46:03 +0000195 for (; ThisTokBuf != ThisTokEnd && UcnLen; ++ThisTokBuf, UcnLen--) {
196 int CharVal = HexDigitValue(ThisTokBuf[0]);
197 if (CharVal == -1) break;
198 UcnVal <<= 4;
199 UcnVal |= CharVal;
200 }
201 // If we didn't consume the proper number of digits, there is a problem.
202 if (UcnLen) {
Douglas Gregor9af03022010-05-26 05:35:51 +0000203 if (Complain)
204 PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
205 diag::err_ucn_escape_incomplete);
Steve Naroff7b753d22009-03-30 23:46:03 +0000206 HadError = 1;
207 return;
208 }
Mike Stump11289f42009-09-09 15:08:12 +0000209 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff7b753d22009-03-30 23:46:03 +0000210 if ((UcnVal < 0xa0 &&
211 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump11289f42009-09-09 15:08:12 +0000212 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Narofff2a880c2009-03-31 10:29:45 +0000213 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Douglas Gregor9af03022010-05-26 05:35:51 +0000214 if (Complain)
215 PP.Diag(Loc, diag::err_ucn_escape_invalid);
Steve Naroff7b753d22009-03-30 23:46:03 +0000216 HadError = 1;
217 return;
218 }
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000219 if (wide) {
Fariborz Jahanian39de0242010-08-31 23:54:38 +0000220 (void)UcnLenSave;
Nico Weber9762e0a2010-10-06 04:57:26 +0000221 assert((UcnLenSave == 4 || UcnLenSave == 8) &&
222 "ProcessUCNEscape - only ucn length of 4 or 8 supported");
223
224 if (!PP.getLangOptions().ShortWChar) {
225 // Note: our internal rep of wide char tokens is always little-endian.
226 *ResultBuf++ = (UcnVal & 0x000000FF);
227 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
228 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
229 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
230 return;
231 }
232
233 // Convert to UTF16.
234 if (UcnVal < (UTF32)0xFFFF) {
235 *ResultBuf++ = (UcnVal & 0x000000FF);
236 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
237 return;
238 }
239 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
240
241 typedef uint16_t UTF16;
242 UcnVal -= 0x10000;
243 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
244 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
245 *ResultBuf++ = (surrogate1 & 0x000000FF);
246 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
247 *ResultBuf++ = (surrogate2 & 0x000000FF);
248 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000249 return;
250 }
Steve Naroff7b753d22009-03-30 23:46:03 +0000251 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
252 // The conversion below was inspired by:
253 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump11289f42009-09-09 15:08:12 +0000254 // First, we determine how many bytes the result will require.
Steve Naroffc94adda2009-04-01 11:09:15 +0000255 typedef uint8_t UTF8;
Steve Naroff7b753d22009-03-30 23:46:03 +0000256
257 unsigned short bytesToWrite = 0;
258 if (UcnVal < (UTF32)0x80)
259 bytesToWrite = 1;
260 else if (UcnVal < (UTF32)0x800)
261 bytesToWrite = 2;
262 else if (UcnVal < (UTF32)0x10000)
263 bytesToWrite = 3;
264 else
265 bytesToWrite = 4;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Steve Naroff7b753d22009-03-30 23:46:03 +0000267 const unsigned byteMask = 0xBF;
268 const unsigned byteMark = 0x80;
Mike Stump11289f42009-09-09 15:08:12 +0000269
Steve Naroff7b753d22009-03-30 23:46:03 +0000270 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Narofff2a880c2009-03-31 10:29:45 +0000271 // into the first byte, depending on how many bytes follow.
Mike Stump11289f42009-09-09 15:08:12 +0000272 static const UTF8 firstByteMark[5] = {
Steve Narofff2a880c2009-03-31 10:29:45 +0000273 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff7b753d22009-03-30 23:46:03 +0000274 };
275 // Finally, we write the bytes into ResultBuf.
276 ResultBuf += bytesToWrite;
277 switch (bytesToWrite) { // note: everything falls through.
278 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
279 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
280 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
281 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
282 }
283 // Update the buffer.
284 ResultBuf += bytesToWrite;
285}
Chris Lattner2f5add62007-04-05 06:57:15 +0000286
287
Steve Naroff09ef4742007-03-09 23:16:33 +0000288/// integer-constant: [C99 6.4.4.1]
289/// decimal-constant integer-suffix
290/// octal-constant integer-suffix
291/// hexadecimal-constant integer-suffix
Mike Stump11289f42009-09-09 15:08:12 +0000292/// decimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000293/// nonzero-digit
294/// decimal-constant digit
Mike Stump11289f42009-09-09 15:08:12 +0000295/// octal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000296/// 0
297/// octal-constant octal-digit
Mike Stump11289f42009-09-09 15:08:12 +0000298/// hexadecimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000299/// hexadecimal-prefix hexadecimal-digit
300/// hexadecimal-constant hexadecimal-digit
301/// hexadecimal-prefix: one of
302/// 0x 0X
303/// integer-suffix:
304/// unsigned-suffix [long-suffix]
305/// unsigned-suffix [long-long-suffix]
306/// long-suffix [unsigned-suffix]
307/// long-long-suffix [unsigned-sufix]
308/// nonzero-digit:
309/// 1 2 3 4 5 6 7 8 9
310/// octal-digit:
311/// 0 1 2 3 4 5 6 7
312/// hexadecimal-digit:
313/// 0 1 2 3 4 5 6 7 8 9
314/// a b c d e f
315/// A B C D E F
316/// unsigned-suffix: one of
317/// u U
318/// long-suffix: one of
319/// l L
Mike Stump11289f42009-09-09 15:08:12 +0000320/// long-long-suffix: one of
Steve Naroff09ef4742007-03-09 23:16:33 +0000321/// ll LL
322///
323/// floating-constant: [C99 6.4.4.2]
324/// TODO: add rules...
325///
Steve Naroff09ef4742007-03-09 23:16:33 +0000326NumericLiteralParser::
327NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000328 SourceLocation TokLoc, Preprocessor &pp)
329 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattner59f09b62008-09-30 20:45:40 +0000331 // This routine assumes that the range begin/end matches the regex for integer
332 // and FP constants (specifically, the 'pp-number' regex), and assumes that
333 // the byte at "*end" is both valid and not part of the regex. Because of
334 // this, it doesn't have to check for 'overscan' in various places.
335 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
336 "Lexer didn't maximally munch?");
Mike Stump11289f42009-09-09 15:08:12 +0000337
Steve Naroff09ef4742007-03-09 23:16:33 +0000338 s = DigitsBegin = begin;
339 saw_exponent = false;
340 saw_period = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000341 isLong = false;
342 isUnsigned = false;
343 isLongLong = false;
Chris Lattnered045422007-08-26 03:29:23 +0000344 isFloat = false;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000345 isImaginary = false;
Mike Stumpc99c0222009-10-08 22:55:36 +0000346 isMicrosoftInteger = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000347 hadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Steve Naroff09ef4742007-03-09 23:16:33 +0000349 if (*s == '0') { // parse radix
Chris Lattner6016a512008-06-30 06:39:54 +0000350 ParseNumberStartingWithZero(TokLoc);
351 if (hadError)
352 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000353 } else { // the first digit is non-zero
354 radix = 10;
355 s = SkipDigits(s);
356 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000357 // Done.
Christopher Lamb42e69f22007-11-29 06:06:27 +0000358 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattner59acca52008-11-22 07:23:31 +0000359 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Benjamin Kramere8394df2010-08-11 14:47:12 +0000360 diag::err_invalid_decimal_digit) << llvm::StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000361 hadError = true;
Chris Lattner328fa5c2007-06-08 17:12:06 +0000362 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000363 } else if (*s == '.') {
364 s++;
365 saw_period = true;
366 s = SkipDigits(s);
Mike Stump11289f42009-09-09 15:08:12 +0000367 }
Chris Lattnerfb8b8f22008-09-29 23:12:31 +0000368 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner4885b972008-04-20 18:47:55 +0000369 const char *Exponent = s;
Steve Naroff09ef4742007-03-09 23:16:33 +0000370 s++;
371 saw_exponent = true;
372 if (*s == '+' || *s == '-') s++; // sign
373 const char *first_non_digit = SkipDigits(s);
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000374 if (first_non_digit != s) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000375 s = first_non_digit;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000376 } else {
Chris Lattner59acca52008-11-22 07:23:31 +0000377 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
378 diag::err_exponent_has_no_digits);
379 hadError = true;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000380 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000381 }
382 }
383 }
384
385 SuffixBegin = s;
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattnerf55ab182007-08-26 01:58:14 +0000387 // Parse the suffix. At this point we can classify whether we have an FP or
388 // integer constant.
389 bool isFPConstant = isFloatingLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnerf55ab182007-08-26 01:58:14 +0000391 // Loop over all of the characters of the suffix. If we see something bad,
392 // we break out of the loop.
393 for (; s != ThisTokEnd; ++s) {
394 switch (*s) {
395 case 'f': // FP Suffix for "float"
396 case 'F':
397 if (!isFPConstant) break; // Error for integer constant.
Chris Lattnered045422007-08-26 03:29:23 +0000398 if (isFloat || isLong) break; // FF, LF invalid.
399 isFloat = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000400 continue; // Success.
401 case 'u':
402 case 'U':
403 if (isFPConstant) break; // Error for floating constant.
404 if (isUnsigned) break; // Cannot be repeated.
405 isUnsigned = true;
406 continue; // Success.
407 case 'l':
408 case 'L':
409 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattnered045422007-08-26 03:29:23 +0000410 if (isFloat) break; // LF invalid.
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattnerf55ab182007-08-26 01:58:14 +0000412 // Check for long long. The L's need to be adjacent and the same case.
413 if (s+1 != ThisTokEnd && s[1] == s[0]) {
414 if (isFPConstant) break; // long long invalid for floats.
415 isLongLong = true;
416 ++s; // Eat both of them.
417 } else {
Steve Naroff09ef4742007-03-09 23:16:33 +0000418 isLong = true;
Steve Naroff09ef4742007-03-09 23:16:33 +0000419 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000420 continue; // Success.
421 case 'i':
Steve Naroffa1f41452008-04-04 21:02:54 +0000422 if (PP.getLangOptions().Microsoft) {
Fariborz Jahanian8c6c0b62010-01-22 21:36:53 +0000423 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000424
Steve Naroffa1f41452008-04-04 21:02:54 +0000425 // Allow i8, i16, i32, i64, and i128.
Mike Stumpc99c0222009-10-08 22:55:36 +0000426 if (s + 1 != ThisTokEnd) {
427 switch (s[1]) {
428 case '8':
429 s += 2; // i8 suffix
430 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000431 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000432 case '1':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000433 if (s + 2 == ThisTokEnd) break;
434 if (s[2] == '6') s += 3; // i16 suffix
435 else if (s[2] == '2') {
436 if (s + 3 == ThisTokEnd) break;
437 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000438 }
439 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000440 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000441 case '3':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000442 if (s + 2 == ThisTokEnd) break;
443 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000444 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000445 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000446 case '6':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000447 if (s + 2 == ThisTokEnd) break;
448 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000449 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000450 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000451 default:
452 break;
453 }
454 break;
Steve Naroffa1f41452008-04-04 21:02:54 +0000455 }
Steve Naroffa1f41452008-04-04 21:02:54 +0000456 }
457 // fall through.
Chris Lattnerf55ab182007-08-26 01:58:14 +0000458 case 'I':
459 case 'j':
460 case 'J':
461 if (isImaginary) break; // Cannot be repeated.
462 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
463 diag::ext_imaginary_constant);
464 isImaginary = true;
465 continue; // Success.
Steve Naroff09ef4742007-03-09 23:16:33 +0000466 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000467 // If we reached here, there was an error.
468 break;
469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattnerf55ab182007-08-26 01:58:14 +0000471 // Report an error if there are any.
472 if (s != ThisTokEnd) {
Chris Lattner59acca52008-11-22 07:23:31 +0000473 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
474 isFPConstant ? diag::err_invalid_suffix_float_constant :
475 diag::err_invalid_suffix_integer_constant)
Benjamin Kramere8394df2010-08-11 14:47:12 +0000476 << llvm::StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattner59acca52008-11-22 07:23:31 +0000477 hadError = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000478 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000479 }
480}
481
Chris Lattner6016a512008-06-30 06:39:54 +0000482/// ParseNumberStartingWithZero - This method is called when the first character
483/// of the number is found to be a zero. This means it is either an octal
484/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump11289f42009-09-09 15:08:12 +0000485/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner6016a512008-06-30 06:39:54 +0000486/// radix etc.
487void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
488 assert(s[0] == '0' && "Invalid method call");
489 s++;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattner6016a512008-06-30 06:39:54 +0000491 // Handle a hex number like 0x1234.
492 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
493 s++;
494 radix = 16;
495 DigitsBegin = s;
496 s = SkipHexDigits(s);
497 if (s == ThisTokEnd) {
498 // Done.
499 } else if (*s == '.') {
500 s++;
501 saw_period = true;
502 s = SkipHexDigits(s);
503 }
504 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump11289f42009-09-09 15:08:12 +0000505 // binary exponent is required.
Alexis Hunt91b78382010-01-10 23:37:56 +0000506 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner6016a512008-06-30 06:39:54 +0000507 const char *Exponent = s;
508 s++;
509 saw_exponent = true;
510 if (*s == '+' || *s == '-') s++; // sign
511 const char *first_non_digit = SkipDigits(s);
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000512 if (first_non_digit == s) {
Chris Lattner59acca52008-11-22 07:23:31 +0000513 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
514 diag::err_exponent_has_no_digits);
515 hadError = true;
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000516 return;
Chris Lattner6016a512008-06-30 06:39:54 +0000517 }
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000518 s = first_non_digit;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Alexis Hunt91b78382010-01-10 23:37:56 +0000520 // In C++0x, we cannot support hexadecmial floating literals because
521 // they conflict with user-defined literals, so we warn in previous
522 // versions of C++ by default.
523 if (PP.getLangOptions().CPlusPlus)
524 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
525 else if (!PP.getLangOptions().HexFloats)
Chris Lattner59acca52008-11-22 07:23:31 +0000526 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner6016a512008-06-30 06:39:54 +0000527 } else if (saw_period) {
Chris Lattner59acca52008-11-22 07:23:31 +0000528 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
529 diag::err_hexconstant_requires_exponent);
530 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000531 }
532 return;
533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner6016a512008-06-30 06:39:54 +0000535 // Handle simple binary numbers 0b01010
536 if (*s == 'b' || *s == 'B') {
537 // 0b101010 is a GCC extension.
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000538 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner6016a512008-06-30 06:39:54 +0000539 ++s;
540 radix = 2;
541 DigitsBegin = s;
542 s = SkipBinaryDigits(s);
543 if (s == ThisTokEnd) {
544 // Done.
545 } else if (isxdigit(*s)) {
Chris Lattner59acca52008-11-22 07:23:31 +0000546 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramere8394df2010-08-11 14:47:12 +0000547 diag::err_invalid_binary_digit) << llvm::StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000548 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000549 }
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000550 // Other suffixes will be diagnosed by the caller.
Chris Lattner6016a512008-06-30 06:39:54 +0000551 return;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner6016a512008-06-30 06:39:54 +0000554 // For now, the radix is set to 8. If we discover that we have a
555 // floating point constant, the radix will change to 10. Octal floating
Mike Stump11289f42009-09-09 15:08:12 +0000556 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner6016a512008-06-30 06:39:54 +0000557 radix = 8;
558 DigitsBegin = s;
559 s = SkipOctalDigits(s);
560 if (s == ThisTokEnd)
561 return; // Done, simple octal number like 01234
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000563 // If we have some other non-octal digit that *is* a decimal digit, see if
564 // this is part of a floating point number like 094.123 or 09e1.
565 if (isdigit(*s)) {
566 const char *EndDecimal = SkipDigits(s);
567 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
568 s = EndDecimal;
569 radix = 10;
570 }
571 }
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000573 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
574 // the code is using an incorrect base.
Chris Lattner6016a512008-06-30 06:39:54 +0000575 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattner59acca52008-11-22 07:23:31 +0000576 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramere8394df2010-08-11 14:47:12 +0000577 diag::err_invalid_octal_digit) << llvm::StringRef(s, 1);
Chris Lattner59acca52008-11-22 07:23:31 +0000578 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000579 return;
580 }
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattner6016a512008-06-30 06:39:54 +0000582 if (*s == '.') {
583 s++;
584 radix = 10;
585 saw_period = true;
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000586 s = SkipDigits(s); // Skip suffix.
Chris Lattner6016a512008-06-30 06:39:54 +0000587 }
588 if (*s == 'e' || *s == 'E') { // exponent
589 const char *Exponent = s;
590 s++;
591 radix = 10;
592 saw_exponent = true;
593 if (*s == '+' || *s == '-') s++; // sign
594 const char *first_non_digit = SkipDigits(s);
595 if (first_non_digit != s) {
596 s = first_non_digit;
597 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000598 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattner59acca52008-11-22 07:23:31 +0000599 diag::err_exponent_has_no_digits);
600 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000601 return;
602 }
603 }
604}
605
606
Chris Lattner5b743d32007-04-04 05:52:58 +0000607/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000608/// matches Val's input width. If there is an overflow, set Val to the low bits
609/// of the result and return true. Otherwise, return false.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000610bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbarbe947082008-10-16 07:32:01 +0000611 // Fast path: Compute a conservative bound on the maximum number of
612 // bits per digit in this radix. If we can't possibly overflow a
613 // uint64 based on that bound then do the simple conversion to
614 // integer. This avoids the expensive overflow checking below, and
615 // handles the common cases that matter (small decimal integers and
616 // hex/octal values which don't overflow).
617 unsigned MaxBitsPerDigit = 1;
Mike Stump11289f42009-09-09 15:08:12 +0000618 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbarbe947082008-10-16 07:32:01 +0000619 MaxBitsPerDigit += 1;
620 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
621 uint64_t N = 0;
622 for (s = DigitsBegin; s != SuffixBegin; ++s)
623 N = N*radix + HexDigitValue(*s);
624
625 // This will truncate the value to Val's input width. Simply check
626 // for overflow by comparing.
627 Val = N;
628 return Val.getZExtValue() != N;
629 }
630
Chris Lattner5b743d32007-04-04 05:52:58 +0000631 Val = 0;
632 s = DigitsBegin;
633
Chris Lattner23b7eb62007-06-15 23:05:46 +0000634 llvm::APInt RadixVal(Val.getBitWidth(), radix);
635 llvm::APInt CharVal(Val.getBitWidth(), 0);
636 llvm::APInt OldVal = Val;
Mike Stump11289f42009-09-09 15:08:12 +0000637
Chris Lattner871b4e12007-04-04 06:36:34 +0000638 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000639 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000640 unsigned C = HexDigitValue(*s++);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner5b743d32007-04-04 05:52:58 +0000642 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000643 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump11289f42009-09-09 15:08:12 +0000644
Chris Lattner5b743d32007-04-04 05:52:58 +0000645 CharVal = C;
Mike Stump11289f42009-09-09 15:08:12 +0000646
Chris Lattner871b4e12007-04-04 06:36:34 +0000647 // Add the digit to the value in the appropriate radix. If adding in digits
648 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000649 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000650
651 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000652 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000653 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
654
Chris Lattner871b4e12007-04-04 06:36:34 +0000655 // Add value, did overflow occur on the value?
Daniel Dunbarb1f64422008-10-16 06:39:30 +0000656 // (a + b) ult b <=> overflow
Chris Lattner5b743d32007-04-04 05:52:58 +0000657 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000658 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000659 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000660 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000661}
662
John McCall53b93a02009-12-24 09:08:04 +0000663llvm::APFloat::opStatus
664NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000665 using llvm::APFloat;
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000666 using llvm::StringRef;
Mike Stump11289f42009-09-09 15:08:12 +0000667
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000668 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall53b93a02009-12-24 09:08:04 +0000669 return Result.convertFromString(StringRef(ThisTokBegin, n),
670 APFloat::rmNearestTiesToEven);
Steve Naroff97b9e912007-07-09 23:53:58 +0000671}
Chris Lattner5b743d32007-04-04 05:52:58 +0000672
Chris Lattner2f5add62007-04-05 06:57:15 +0000673
674CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
675 SourceLocation Loc, Preprocessor &PP) {
676 // At this point we know that the character matches the regex "L?'.*'".
677 HadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattner2f5add62007-04-05 06:57:15 +0000679 // Determine if this is a wide character.
680 IsWide = begin[0] == 'L';
681 if (IsWide) ++begin;
Mike Stump11289f42009-09-09 15:08:12 +0000682
Chris Lattner2f5add62007-04-05 06:57:15 +0000683 // Skip over the entry quote.
684 assert(begin[0] == '\'' && "Invalid token lexed");
685 ++begin;
686
Mike Stump11289f42009-09-09 15:08:12 +0000687 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000688 // upto 64-bits.
Chris Lattner2f5add62007-04-05 06:57:15 +0000689 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner37e05872008-03-05 18:54:05 +0000690 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Chris Lattner2f5add62007-04-05 06:57:15 +0000691 "Assumes char is 8 bits");
Chris Lattner8577f622009-04-28 21:51:46 +0000692 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
693 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
694 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
695 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
696 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000697
Mike Stump11289f42009-09-09 15:08:12 +0000698 // This is what we will use for overflow detection
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000699 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattner8577f622009-04-28 21:51:46 +0000701 unsigned NumCharsSoFar = 0;
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000702 bool Warned = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000703 while (begin[0] != '\'') {
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000704 uint64_t ResultChar;
Chris Lattner2f5add62007-04-05 06:57:15 +0000705 if (begin[0] != '\\') // If this is a normal character, consume it.
706 ResultChar = *begin++;
707 else // Otherwise, this is an escape character.
Douglas Gregor9af03022010-05-26 05:35:51 +0000708 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP,
709 /*Complain=*/true);
Chris Lattner2f5add62007-04-05 06:57:15 +0000710
711 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
712 // implementation defined (C99 6.4.4.4p10).
Chris Lattner8577f622009-04-28 21:51:46 +0000713 if (NumCharsSoFar) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000714 if (IsWide) {
715 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000716 LitVal = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000717 } else {
718 // Narrow character literals act as though their value is concatenated
Chris Lattner8577f622009-04-28 21:51:46 +0000719 // in this implementation, but warn on overflow.
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000720 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000721 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1cf5bdd2010-04-16 23:44:05 +0000722 Warned = true;
723 }
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000724 LitVal <<= 8;
Chris Lattner2f5add62007-04-05 06:57:15 +0000725 }
726 }
Mike Stump11289f42009-09-09 15:08:12 +0000727
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000728 LitVal = LitVal + ResultChar;
Chris Lattner8577f622009-04-28 21:51:46 +0000729 ++NumCharsSoFar;
730 }
731
732 // If this is the second character being processed, do special handling.
733 if (NumCharsSoFar > 1) {
734 // Warn about discarding the top bits for multi-char wide-character
735 // constants (L'abcd').
736 if (IsWide)
737 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
738 else if (NumCharsSoFar != 4)
739 PP.Diag(Loc, diag::ext_multichar_character_literal);
740 else
741 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedmand8cec572009-06-01 05:25:02 +0000742 IsMultiChar = true;
Daniel Dunbara444cc22009-07-29 01:46:05 +0000743 } else
744 IsMultiChar = false;
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000745
746 // Transfer the value from APInt to uint64_t
747 Value = LitVal.getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner2f5add62007-04-05 06:57:15 +0000749 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
750 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
751 // character constants are not sign extended in the this implementation:
752 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattner8577f622009-04-28 21:51:46 +0000753 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000754 PP.getLangOptions().CharIsSigned)
Chris Lattner2f5add62007-04-05 06:57:15 +0000755 Value = (signed char)Value;
756}
757
758
Steve Naroff4f88b312007-03-13 22:37:02 +0000759/// string-literal: [C99 6.4.5]
760/// " [s-char-sequence] "
761/// L" [s-char-sequence] "
762/// s-char-sequence:
763/// s-char
764/// s-char-sequence s-char
765/// s-char:
766/// any source character except the double quote ",
767/// backslash \, or newline character
768/// escape-character
769/// universal-character-name
770/// escape-character: [C99 6.4.4.4]
771/// \ escape-code
772/// universal-character-name
773/// escape-code:
774/// character-escape-code
775/// octal-escape-code
776/// hex-escape-code
777/// character-escape-code: one of
778/// n t b r f v a
779/// \ ' " ?
780/// octal-escape-code:
781/// octal-digit
782/// octal-digit octal-digit
783/// octal-digit octal-digit octal-digit
784/// hex-escape-code:
785/// x hex-digit
786/// hex-escape-code hex-digit
787/// universal-character-name:
788/// \u hex-quad
789/// \U hex-quad hex-quad
790/// hex-quad:
791/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000792///
Steve Naroff4f88b312007-03-13 22:37:02 +0000793StringLiteralParser::
Chris Lattner146762e2007-07-20 16:59:19 +0000794StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Alexis Hunt3b791862010-08-30 17:47:05 +0000795 Preprocessor &pp, bool Complain) : PP(pp) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000796 // Scan all of the string portions, remember the max individual token length,
797 // computing a bound on the concatenated string length, and see whether any
798 // piece is a wide-string. If any of the string portions is a wide-string
799 // literal, the result is a wide-string literal [C99 6.4.5p4].
Alexis Hunt3b791862010-08-30 17:47:05 +0000800 MaxTokenLength = StringToks[0].getLength();
801 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000802 AnyWide = StringToks[0].is(tok::wide_string_literal);
Alexis Hunt3b791862010-08-30 17:47:05 +0000803
804 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000805
806 // Implement Translation Phase #6: concatenation of string literals
807 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000808 for (unsigned i = 1; i != NumStringToks; ++i) {
809 // The string could be shorter than this if it needs cleaning, but this is a
810 // reasonable bound, which is all we need.
Alexis Hunt3b791862010-08-30 17:47:05 +0000811 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump11289f42009-09-09 15:08:12 +0000812
Steve Naroff4f88b312007-03-13 22:37:02 +0000813 // Remember maximum string piece length.
Alexis Hunt3b791862010-08-30 17:47:05 +0000814 if (StringToks[i].getLength() > MaxTokenLength)
815 MaxTokenLength = StringToks[i].getLength();
Mike Stump11289f42009-09-09 15:08:12 +0000816
Steve Naroff4f88b312007-03-13 22:37:02 +0000817 // Remember if we see any wide strings.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000818 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Steve Naroff4f88b312007-03-13 22:37:02 +0000819 }
Chris Lattnerd42c29f2009-02-26 23:01:51 +0000820
Steve Naroff4f88b312007-03-13 22:37:02 +0000821 // Include space for the null terminator.
822 ++SizeBound;
Mike Stump11289f42009-09-09 15:08:12 +0000823
Steve Naroff4f88b312007-03-13 22:37:02 +0000824 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump11289f42009-09-09 15:08:12 +0000825
Steve Naroff4f88b312007-03-13 22:37:02 +0000826 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
827 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
828 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000829 if (AnyWide) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000830 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Chris Lattner2f5add62007-04-05 06:57:15 +0000831 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
832 wchar_tByteWidth /= 8;
833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
Steve Naroff4f88b312007-03-13 22:37:02 +0000835 // The output buffer size needs to be large enough to hold wide characters.
836 // This is a worst-case assumption which basically corresponds to L"" "long".
837 if (AnyWide)
838 SizeBound *= wchar_tByteWidth;
Mike Stump11289f42009-09-09 15:08:12 +0000839
Steve Naroff4f88b312007-03-13 22:37:02 +0000840 // Size the temporary buffer to hold the result string data.
841 ResultBuf.resize(SizeBound);
Mike Stump11289f42009-09-09 15:08:12 +0000842
Steve Naroff4f88b312007-03-13 22:37:02 +0000843 // Likewise, but for each string piece.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000844 llvm::SmallString<512> TokenBuf;
Steve Naroff4f88b312007-03-13 22:37:02 +0000845 TokenBuf.resize(MaxTokenLength);
Mike Stump11289f42009-09-09 15:08:12 +0000846
Steve Naroff4f88b312007-03-13 22:37:02 +0000847 // Loop over all the strings, getting their spelling, and expanding them to
848 // wide strings as appropriate.
849 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump11289f42009-09-09 15:08:12 +0000850
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000851 Pascal = false;
Mike Stump11289f42009-09-09 15:08:12 +0000852
Steve Naroff4f88b312007-03-13 22:37:02 +0000853 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
854 const char *ThisTokBuf = &TokenBuf[0];
855 // Get the spelling of the token, which eliminates trigraphs, etc. We know
856 // that ThisTokBuf points to a buffer that is big enough for the whole token
857 // and 'spelled' tokens can only shrink.
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000858 bool StringInvalid = false;
859 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf,
Alexis Hunt3b791862010-08-30 17:47:05 +0000860 &StringInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000861 if (StringInvalid) {
862 hadError = 1;
863 continue;
864 }
865
Steve Naroff4f88b312007-03-13 22:37:02 +0000866 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000867 bool wide = false;
Steve Naroff4f88b312007-03-13 22:37:02 +0000868 // TODO: Input character set mapping support.
Mike Stump11289f42009-09-09 15:08:12 +0000869
Steve Naroff4f88b312007-03-13 22:37:02 +0000870 // Skip L marker for wide strings.
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000871 if (ThisTokBuf[0] == 'L') {
872 wide = true;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000873 ++ThisTokBuf;
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Steve Naroff4f88b312007-03-13 22:37:02 +0000876 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
877 ++ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000879 // Check if this is a pascal string
880 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
881 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump11289f42009-09-09 15:08:12 +0000882
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000883 // If the \p sequence is found in the first token, we have a pascal string
884 // Otherwise, if we already have a pascal string, ignore the first \p
885 if (i == 0) {
886 ++ThisTokBuf;
887 Pascal = true;
888 } else if (Pascal)
889 ThisTokBuf += 2;
890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Steve Naroff4f88b312007-03-13 22:37:02 +0000892 while (ThisTokBuf != ThisTokEnd) {
893 // Is this a span of non-escape characters?
894 if (ThisTokBuf[0] != '\\') {
895 const char *InStart = ThisTokBuf;
896 do {
897 ++ThisTokBuf;
898 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump11289f42009-09-09 15:08:12 +0000899
Steve Naroff4f88b312007-03-13 22:37:02 +0000900 // Copy the character span over.
901 unsigned Len = ThisTokBuf-InStart;
902 if (!AnyWide) {
903 memcpy(ResultPtr, InStart, Len);
904 ResultPtr += Len;
905 } else {
906 // Note: our internal rep of wide char tokens is always little-endian.
907 for (; Len; --Len, ++InStart) {
908 *ResultPtr++ = InStart[0];
909 // Add zeros at the end.
910 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff7b753d22009-03-30 23:46:03 +0000911 *ResultPtr++ = 0;
Steve Naroff4f88b312007-03-13 22:37:02 +0000912 }
913 }
914 continue;
915 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000916 // Is this a Universal Character Name escape?
Steve Naroff7b753d22009-03-30 23:46:03 +0000917 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Mike Stump11289f42009-09-09 15:08:12 +0000918 ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Fariborz Jahanianabaae2b2010-08-31 23:34:27 +0000919 hadError, StringToks[i].getLocation(), PP, wide,
920 Complain);
Steve Naroffc94adda2009-04-01 11:09:15 +0000921 continue;
922 }
923 // Otherwise, this is a non-UCN escape character. Process it.
924 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
925 StringToks[i].getLocation(),
Chris Lattnerc548be92010-06-15 18:06:43 +0000926 AnyWide, PP, Complain);
Mike Stump11289f42009-09-09 15:08:12 +0000927
Steve Naroffc94adda2009-04-01 11:09:15 +0000928 // Note: our internal rep of wide char tokens is always little-endian.
929 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump11289f42009-09-09 15:08:12 +0000930
Steve Naroffc94adda2009-04-01 11:09:15 +0000931 if (AnyWide) {
932 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
933 *ResultPtr++ = ResultChar >> i*8;
Steve Naroff4f88b312007-03-13 22:37:02 +0000934 }
935 }
936 }
Mike Stump11289f42009-09-09 15:08:12 +0000937
Chris Lattner8a24e582009-01-16 18:51:42 +0000938 if (Pascal) {
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000939 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Fariborz Jahanian93bef102010-05-28 19:40:48 +0000940 if (AnyWide)
941 ResultBuf[0] /= wchar_tByteWidth;
Chris Lattner8a24e582009-01-16 18:51:42 +0000942
943 // Verify that pascal strings aren't too large.
Douglas Gregor9af03022010-05-26 05:35:51 +0000944 if (GetStringLength() > 256 && Complain) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000945 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
946 << SourceRange(StringToks[0].getLocation(),
947 StringToks[NumStringToks-1].getLocation());
Eli Friedman1c3fb222009-04-01 03:17:08 +0000948 hadError = 1;
949 return;
950 }
Douglas Gregorb37b46e2010-07-20 14:33:20 +0000951 } else if (Complain) {
952 // Complain if this string literal has too many characters.
953 unsigned MaxChars = PP.getLangOptions().CPlusPlus? 65536
954 : PP.getLangOptions().C99 ? 4095
955 : 509;
956
957 if (GetNumStringChars() > MaxChars)
958 PP.Diag(StringToks[0].getLocation(), diag::ext_string_too_long)
959 << GetNumStringChars() << MaxChars
960 << (PP.getLangOptions().CPlusPlus? 2
961 : PP.getLangOptions().C99 ? 1
962 : 0)
963 << SourceRange(StringToks[0].getLocation(),
964 StringToks[NumStringToks-1].getLocation());
Chris Lattner8a24e582009-01-16 18:51:42 +0000965 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000966}
Chris Lattnerddb71912009-02-18 19:21:10 +0000967
968
969/// getOffsetOfStringByte - This function returns the offset of the
970/// specified byte of the string data represented by Token. This handles
971/// advancing over escape sequences in the string.
972unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
973 unsigned ByteNo,
Douglas Gregor9af03022010-05-26 05:35:51 +0000974 Preprocessor &PP,
975 bool Complain) {
Chris Lattnerddb71912009-02-18 19:21:10 +0000976 // Get the spelling of the token.
977 llvm::SmallString<16> SpellingBuffer;
Alexis Hunt3b791862010-08-30 17:47:05 +0000978 SpellingBuffer.resize(Tok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +0000979
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000980 bool StringInvalid = false;
Chris Lattnerddb71912009-02-18 19:21:10 +0000981 const char *SpellingPtr = &SpellingBuffer[0];
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000982 unsigned TokLen = PP.getSpelling(Tok, SpellingPtr, &StringInvalid);
983 if (StringInvalid) {
984 return 0;
985 }
Chris Lattnerddb71912009-02-18 19:21:10 +0000986
987 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
988
Mike Stump11289f42009-09-09 15:08:12 +0000989
Chris Lattnerddb71912009-02-18 19:21:10 +0000990 const char *SpellingStart = SpellingPtr;
991 const char *SpellingEnd = SpellingPtr+TokLen;
992
993 // Skip over the leading quote.
994 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
995 ++SpellingPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattnerddb71912009-02-18 19:21:10 +0000997 // Skip over bytes until we find the offset we're looking for.
998 while (ByteNo) {
999 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump11289f42009-09-09 15:08:12 +00001000
Chris Lattnerddb71912009-02-18 19:21:10 +00001001 // Step over non-escapes simply.
1002 if (*SpellingPtr != '\\') {
1003 ++SpellingPtr;
1004 --ByteNo;
1005 continue;
1006 }
Mike Stump11289f42009-09-09 15:08:12 +00001007
Chris Lattnerddb71912009-02-18 19:21:10 +00001008 // Otherwise, this is an escape character. Advance over it.
1009 bool HadError = false;
1010 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Douglas Gregor9af03022010-05-26 05:35:51 +00001011 Tok.getLocation(), false, PP, Complain);
Chris Lattnerddb71912009-02-18 19:21:10 +00001012 assert(!HadError && "This method isn't valid on erroneous strings");
1013 --ByteNo;
1014 }
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattnerddb71912009-02-18 19:21:10 +00001016 return SpellingPtr-SpellingStart;
1017}