blob: fb543d0f03b3d24626886b0a0113698ea8d53879 [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,
36 SourceLocation Loc, bool IsWide,
Douglas Gregorb90f4b32010-05-26 05:35:51 +000037 Preprocessor &PP, bool Complain) {
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':
Douglas Gregorb90f4b32010-05-26 05:35:51 +000057 if (Complain)
58 PP.Diag(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':
Douglas Gregorb90f4b32010-05-26 05:35:51 +000062 if (Complain)
63 PP.Diag(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)) {
Douglas Gregorb90f4b32010-05-26 05:35:51 +000084 if (Complain)
85 PP.Diag(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.
Alisdair Meredith1a75ee22009-07-14 08:10:06 +0000102 unsigned CharWidth = IsWide
103 ? PP.getTargetInfo().getWCharWidth()
104 : PP.getTargetInfo().getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
107 Overflow = true;
108 ResultChar &= ~0U >> (32-CharWidth);
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // Check for overflow.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000112 if (Overflow && Complain) // Too many digits to fit in
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 PP.Diag(Loc, diag::warn_hex_escape_too_large);
114 break;
115 }
116 case '0': case '1': case '2': case '3':
117 case '4': case '5': case '6': case '7': {
118 // Octal escapes.
119 --ThisTokBuf;
120 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 Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // Check for overflow. Reject '\777', but not L'\777'.
Alisdair Meredith1a75ee22009-07-14 08:10:06 +0000133 unsigned CharWidth = IsWide
134 ? PP.getTargetInfo().getWCharWidth()
135 : PP.getTargetInfo().getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000138 if (Complain)
139 PP.Diag(Loc, diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 ResultChar &= ~0U >> (32-CharWidth);
141 }
142 break;
143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorb90f4b32010-05-26 05:35:51 +0000148 if (Complain)
149 PP.Diag(Loc, diag::ext_nonstandard_escape)
150 << std::string()+(char)ResultChar;
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000151 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 default:
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000153 if (!Complain)
154 break;
155
Chris Lattnerac92d822008-11-22 07:23:31 +0000156 if (isgraph(ThisTokBuf[0]))
Chris Lattner204b2fe2008-11-18 21:48:13 +0000157 PP.Diag(Loc, diag::ext_unknown_escape) << std::string()+(char)ResultChar;
Chris Lattnerac92d822008-11-22 07:23:31 +0000158 else
Chris Lattner204b2fe2008-11-18 21:48:13 +0000159 PP.Diag(Loc, diag::ext_unknown_escape) << "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
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 Stump1eb44332009-09-09 15:08:12 +0000170static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
171 char *&ResultBuf, bool &HadError,
Chris Lattner37dd3ec2010-06-15 18:06:43 +0000172 SourceLocation Loc, Preprocessor &PP,
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000173 bool wide,
Chris Lattner37dd3ec2010-06-15 18:06:43 +0000174 bool Complain) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000175 // FIXME: Add a warning - UCN's are only valid in C++ & C99.
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000176 // FIXME: Handle wide strings.
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Steve Naroff4e93b342009-04-01 11:09:15 +0000178 // Save the beginning of the string (for error diagnostics).
179 const char *ThisTokBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000181 // Skip the '\u' char's.
182 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000183
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000184 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000185 if (Complain)
186 PP.Diag(Loc, diag::err_ucn_escape_no_digits);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000187 HadError = 1;
188 return;
189 }
Steve Naroff4e93b342009-04-01 11:09:15 +0000190 typedef uint32_t UTF32;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000192 UTF32 UcnVal = 0;
193 unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000194 unsigned short UcnLenSave = UcnLen;
Steve Naroff0e3e3eb2009-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 Gregorb90f4b32010-05-26 05:35:51 +0000203 if (Complain)
204 PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
205 diag::err_ucn_escape_incomplete);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000206 HadError = 1;
207 return;
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000210 if ((UcnVal < 0xa0 &&
211 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump1eb44332009-09-09 15:08:12 +0000212 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000213 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000214 if (Complain)
215 PP.Diag(Loc, diag::err_ucn_escape_invalid);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000216 HadError = 1;
217 return;
218 }
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000219 if (wide) {
Fariborz Jahanian0b6542c2010-08-31 23:54:38 +0000220 (void)UcnLenSave;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000221 assert(UcnLenSave == 4 &&
222 "ProcessUCNEscape - only ucn length of 4 supported");
223 // little endian assumed.
224 *ResultBuf++ = (UcnVal & 0x000000FF);
225 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
226 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
227 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
228 return;
229 }
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000230 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
231 // The conversion below was inspired by:
232 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000233 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000234 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000235
236 unsigned short bytesToWrite = 0;
237 if (UcnVal < (UTF32)0x80)
238 bytesToWrite = 1;
239 else if (UcnVal < (UTF32)0x800)
240 bytesToWrite = 2;
241 else if (UcnVal < (UTF32)0x10000)
242 bytesToWrite = 3;
243 else
244 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000246 const unsigned byteMask = 0xBF;
247 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000249 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000250 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000251 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000252 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000253 };
254 // Finally, we write the bytes into ResultBuf.
255 ResultBuf += bytesToWrite;
256 switch (bytesToWrite) { // note: everything falls through.
257 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
258 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
259 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
260 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
261 }
262 // Update the buffer.
263 ResultBuf += bytesToWrite;
264}
Reid Spencer5f016e22007-07-11 17:01:13 +0000265
266
267/// integer-constant: [C99 6.4.4.1]
268/// decimal-constant integer-suffix
269/// octal-constant integer-suffix
270/// hexadecimal-constant integer-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000271/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000272/// nonzero-digit
273/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000274/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000275/// 0
276/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000277/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000278/// hexadecimal-prefix hexadecimal-digit
279/// hexadecimal-constant hexadecimal-digit
280/// hexadecimal-prefix: one of
281/// 0x 0X
282/// integer-suffix:
283/// unsigned-suffix [long-suffix]
284/// unsigned-suffix [long-long-suffix]
285/// long-suffix [unsigned-suffix]
286/// long-long-suffix [unsigned-sufix]
287/// nonzero-digit:
288/// 1 2 3 4 5 6 7 8 9
289/// octal-digit:
290/// 0 1 2 3 4 5 6 7
291/// hexadecimal-digit:
292/// 0 1 2 3 4 5 6 7 8 9
293/// a b c d e f
294/// A B C D E F
295/// unsigned-suffix: one of
296/// u U
297/// long-suffix: one of
298/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000299/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000300/// ll LL
301///
302/// floating-constant: [C99 6.4.4.2]
303/// TODO: add rules...
304///
Reid Spencer5f016e22007-07-11 17:01:13 +0000305NumericLiteralParser::
306NumericLiteralParser(const char *begin, const char *end,
307 SourceLocation TokLoc, Preprocessor &pp)
308 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000310 // This routine assumes that the range begin/end matches the regex for integer
311 // and FP constants (specifically, the 'pp-number' regex), and assumes that
312 // the byte at "*end" is both valid and not part of the regex. Because of
313 // this, it doesn't have to check for 'overscan' in various places.
314 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
315 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 s = DigitsBegin = begin;
318 saw_exponent = false;
319 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 isLong = false;
321 isUnsigned = false;
322 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000323 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000324 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000325 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000329 ParseNumberStartingWithZero(TokLoc);
330 if (hadError)
331 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 } else { // the first digit is non-zero
333 radix = 10;
334 s = SkipDigits(s);
335 if (s == ThisTokEnd) {
336 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000337 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000338 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000339 diag::err_invalid_decimal_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000340 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 return;
342 } else if (*s == '.') {
343 s++;
344 saw_period = true;
345 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000346 }
Chris Lattner4411f462008-09-29 23:12:31 +0000347 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000348 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 s++;
350 saw_exponent = true;
351 if (*s == '+' || *s == '-') s++; // sign
352 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000353 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000355 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000356 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
357 diag::err_exponent_has_no_digits);
358 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000359 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 }
361 }
362 }
363
364 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattner506b8de2007-08-26 01:58:14 +0000366 // Parse the suffix. At this point we can classify whether we have an FP or
367 // integer constant.
368 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner506b8de2007-08-26 01:58:14 +0000370 // Loop over all of the characters of the suffix. If we see something bad,
371 // we break out of the loop.
372 for (; s != ThisTokEnd; ++s) {
373 switch (*s) {
374 case 'f': // FP Suffix for "float"
375 case 'F':
376 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000377 if (isFloat || isLong) break; // FF, LF invalid.
378 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000379 continue; // Success.
380 case 'u':
381 case 'U':
382 if (isFPConstant) break; // Error for floating constant.
383 if (isUnsigned) break; // Cannot be repeated.
384 isUnsigned = true;
385 continue; // Success.
386 case 'l':
387 case 'L':
388 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000389 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner506b8de2007-08-26 01:58:14 +0000391 // Check for long long. The L's need to be adjacent and the same case.
392 if (s+1 != ThisTokEnd && s[1] == s[0]) {
393 if (isFPConstant) break; // long long invalid for floats.
394 isLongLong = true;
395 ++s; // Eat both of them.
396 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000399 continue; // Success.
400 case 'i':
Steve Naroff0c29b222008-04-04 21:02:54 +0000401 if (PP.getLangOptions().Microsoft) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000402 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000403
Steve Naroff0c29b222008-04-04 21:02:54 +0000404 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000405 if (s + 1 != ThisTokEnd) {
406 switch (s[1]) {
407 case '8':
408 s += 2; // i8 suffix
409 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000410 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000411 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000412 if (s + 2 == ThisTokEnd) break;
413 if (s[2] == '6') s += 3; // i16 suffix
414 else if (s[2] == '2') {
415 if (s + 3 == ThisTokEnd) break;
416 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000417 }
418 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000419 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000420 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000421 if (s + 2 == ThisTokEnd) break;
422 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000423 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000424 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000425 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000426 if (s + 2 == ThisTokEnd) break;
427 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000428 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000429 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000430 default:
431 break;
432 }
433 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000434 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000435 }
436 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000437 case 'I':
438 case 'j':
439 case 'J':
440 if (isImaginary) break; // Cannot be repeated.
441 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
442 diag::ext_imaginary_constant);
443 isImaginary = true;
444 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000446 // If we reached here, there was an error.
447 break;
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner506b8de2007-08-26 01:58:14 +0000450 // Report an error if there are any.
451 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000452 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
453 isFPConstant ? diag::err_invalid_suffix_float_constant :
454 diag::err_invalid_suffix_integer_constant)
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000455 << llvm::StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000456 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000457 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 }
459}
460
Chris Lattner368328c2008-06-30 06:39:54 +0000461/// ParseNumberStartingWithZero - This method is called when the first character
462/// of the number is found to be a zero. This means it is either an octal
463/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000464/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000465/// radix etc.
466void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
467 assert(s[0] == '0' && "Invalid method call");
468 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner368328c2008-06-30 06:39:54 +0000470 // Handle a hex number like 0x1234.
471 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
472 s++;
473 radix = 16;
474 DigitsBegin = s;
475 s = SkipHexDigits(s);
476 if (s == ThisTokEnd) {
477 // Done.
478 } else if (*s == '.') {
479 s++;
480 saw_period = true;
481 s = SkipHexDigits(s);
482 }
483 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000484 // binary exponent is required.
Sean Hunt8c723402010-01-10 23:37:56 +0000485 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner368328c2008-06-30 06:39:54 +0000486 const char *Exponent = s;
487 s++;
488 saw_exponent = true;
489 if (*s == '+' || *s == '-') s++; // sign
490 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000491 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000492 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
493 diag::err_exponent_has_no_digits);
494 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000495 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000496 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000497 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Sean Hunt8c723402010-01-10 23:37:56 +0000499 // In C++0x, we cannot support hexadecmial floating literals because
500 // they conflict with user-defined literals, so we warn in previous
501 // versions of C++ by default.
502 if (PP.getLangOptions().CPlusPlus)
503 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
504 else if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000505 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000506 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000507 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
508 diag::err_hexconstant_requires_exponent);
509 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000510 }
511 return;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner368328c2008-06-30 06:39:54 +0000514 // Handle simple binary numbers 0b01010
515 if (*s == 'b' || *s == 'B') {
516 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000517 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000518 ++s;
519 radix = 2;
520 DigitsBegin = s;
521 s = SkipBinaryDigits(s);
522 if (s == ThisTokEnd) {
523 // Done.
524 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000525 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000526 diag::err_invalid_binary_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000527 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000528 }
Chris Lattner413d3552008-06-30 06:44:49 +0000529 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000530 return;
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner368328c2008-06-30 06:39:54 +0000533 // For now, the radix is set to 8. If we discover that we have a
534 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000535 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000536 radix = 8;
537 DigitsBegin = s;
538 s = SkipOctalDigits(s);
539 if (s == ThisTokEnd)
540 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattner413d3552008-06-30 06:44:49 +0000542 // If we have some other non-octal digit that *is* a decimal digit, see if
543 // this is part of a floating point number like 094.123 or 09e1.
544 if (isdigit(*s)) {
545 const char *EndDecimal = SkipDigits(s);
546 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
547 s = EndDecimal;
548 radix = 10;
549 }
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner413d3552008-06-30 06:44:49 +0000552 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
553 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000554 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000555 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000556 diag::err_invalid_octal_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000557 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000558 return;
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner368328c2008-06-30 06:39:54 +0000561 if (*s == '.') {
562 s++;
563 radix = 10;
564 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000565 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000566 }
567 if (*s == 'e' || *s == 'E') { // exponent
568 const char *Exponent = s;
569 s++;
570 radix = 10;
571 saw_exponent = true;
572 if (*s == '+' || *s == '-') s++; // sign
573 const char *first_non_digit = SkipDigits(s);
574 if (first_non_digit != s) {
575 s = first_non_digit;
576 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000577 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000578 diag::err_exponent_has_no_digits);
579 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000580 return;
581 }
582 }
583}
584
585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586/// GetIntegerValue - Convert this numeric literal value to an APInt that
587/// matches Val's input width. If there is an overflow, set Val to the low bits
588/// of the result and return true. Otherwise, return false.
589bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000590 // Fast path: Compute a conservative bound on the maximum number of
591 // bits per digit in this radix. If we can't possibly overflow a
592 // uint64 based on that bound then do the simple conversion to
593 // integer. This avoids the expensive overflow checking below, and
594 // handles the common cases that matter (small decimal integers and
595 // hex/octal values which don't overflow).
596 unsigned MaxBitsPerDigit = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000597 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbara179be32008-10-16 07:32:01 +0000598 MaxBitsPerDigit += 1;
599 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
600 uint64_t N = 0;
601 for (s = DigitsBegin; s != SuffixBegin; ++s)
602 N = N*radix + HexDigitValue(*s);
603
604 // This will truncate the value to Val's input width. Simply check
605 // for overflow by comparing.
606 Val = N;
607 return Val.getZExtValue() != N;
608 }
609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 Val = 0;
611 s = DigitsBegin;
612
613 llvm::APInt RadixVal(Val.getBitWidth(), radix);
614 llvm::APInt CharVal(Val.getBitWidth(), 0);
615 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 bool OverflowOccurred = false;
618 while (s < SuffixBegin) {
619 unsigned C = HexDigitValue(*s++);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 // If this letter is out of bound for this radix, reject it.
622 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // Add the digit to the value in the appropriate radix. If adding in digits
627 // made the value smaller, then this overflowed.
628 OldVal = Val;
629
630 // Multiply by radix, did overflow occur on the multiply?
631 Val *= RadixVal;
632 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
633
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000635 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 OverflowOccurred |= Val.ult(CharVal);
638 }
639 return OverflowOccurred;
640}
641
John McCall94c939d2009-12-24 09:08:04 +0000642llvm::APFloat::opStatus
643NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000644 using llvm::APFloat;
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000645 using llvm::StringRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000647 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000648 return Result.convertFromString(StringRef(ThisTokBegin, n),
649 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650}
651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652
653CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
654 SourceLocation Loc, Preprocessor &PP) {
655 // At this point we know that the character matches the regex "L?'.*'".
656 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // Determine if this is a wide character.
659 IsWide = begin[0] == 'L';
660 if (IsWide) ++begin;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // Skip over the entry quote.
663 assert(begin[0] == '\'' && "Invalid token lexed");
664 ++begin;
665
Mike Stump1eb44332009-09-09 15:08:12 +0000666 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000667 // upto 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000669 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000671 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
672 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
673 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
674 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
675 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000676
Mike Stump1eb44332009-09-09 15:08:12 +0000677 // This is what we will use for overflow detection
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000678 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Chris Lattnere3ad8812009-04-28 21:51:46 +0000680 unsigned NumCharsSoFar = 0;
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000681 bool Warned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 while (begin[0] != '\'') {
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000683 uint64_t ResultChar;
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 if (begin[0] != '\\') // If this is a normal character, consume it.
685 ResultChar = *begin++;
686 else // Otherwise, this is an escape character.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000687 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP,
688 /*Complain=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000689
690 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
691 // implementation defined (C99 6.4.4.4p10).
Chris Lattnere3ad8812009-04-28 21:51:46 +0000692 if (NumCharsSoFar) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 if (IsWide) {
694 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000695 LitVal = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 } else {
697 // Narrow character literals act as though their value is concatenated
Chris Lattnere3ad8812009-04-28 21:51:46 +0000698 // in this implementation, but warn on overflow.
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000699 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000701 Warned = true;
702 }
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000703 LitVal <<= 8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 }
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000707 LitVal = LitVal + ResultChar;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000708 ++NumCharsSoFar;
709 }
710
711 // If this is the second character being processed, do special handling.
712 if (NumCharsSoFar > 1) {
713 // Warn about discarding the top bits for multi-char wide-character
714 // constants (L'abcd').
715 if (IsWide)
716 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
717 else if (NumCharsSoFar != 4)
718 PP.Diag(Loc, diag::ext_multichar_character_literal);
719 else
720 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +0000721 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +0000722 } else
723 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000724
725 // Transfer the value from APInt to uint64_t
726 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
729 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
730 // character constants are not sign extended in the this implementation:
731 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattnere3ad8812009-04-28 21:51:46 +0000732 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman15b91762009-06-05 07:05:05 +0000733 PP.getLangOptions().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 Value = (signed char)Value;
735}
736
737
738/// string-literal: [C99 6.4.5]
739/// " [s-char-sequence] "
740/// L" [s-char-sequence] "
741/// s-char-sequence:
742/// s-char
743/// s-char-sequence s-char
744/// s-char:
745/// any source character except the double quote ",
746/// backslash \, or newline character
747/// escape-character
748/// universal-character-name
749/// escape-character: [C99 6.4.4.4]
750/// \ escape-code
751/// universal-character-name
752/// escape-code:
753/// character-escape-code
754/// octal-escape-code
755/// hex-escape-code
756/// character-escape-code: one of
757/// n t b r f v a
758/// \ ' " ?
759/// octal-escape-code:
760/// octal-digit
761/// octal-digit octal-digit
762/// octal-digit octal-digit octal-digit
763/// hex-escape-code:
764/// x hex-digit
765/// hex-escape-code hex-digit
766/// universal-character-name:
767/// \u hex-quad
768/// \U hex-quad hex-quad
769/// hex-quad:
770/// hex-digit hex-digit hex-digit hex-digit
771///
772StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000773StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Sean Hunt6cf75022010-08-30 17:47:05 +0000774 Preprocessor &pp, bool Complain) : PP(pp) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 // Scan all of the string portions, remember the max individual token length,
776 // computing a bound on the concatenated string length, and see whether any
777 // piece is a wide-string. If any of the string portions is a wide-string
778 // literal, the result is a wide-string literal [C99 6.4.5p4].
Sean Hunt6cf75022010-08-30 17:47:05 +0000779 MaxTokenLength = StringToks[0].getLength();
780 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000781 AnyWide = StringToks[0].is(tok::wide_string_literal);
Sean Hunt6cf75022010-08-30 17:47:05 +0000782
783 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000784
785 // Implement Translation Phase #6: concatenation of string literals
786 /// (C99 5.1.1.2p1). The common case is only one string fragment.
787 for (unsigned i = 1; i != NumStringToks; ++i) {
788 // The string could be shorter than this if it needs cleaning, but this is a
789 // reasonable bound, which is all we need.
Sean Hunt6cf75022010-08-30 17:47:05 +0000790 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +0000793 if (StringToks[i].getLength() > MaxTokenLength)
794 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 // Remember if we see any wide strings.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000797 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +0000799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 // Include space for the null terminator.
801 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
806 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
807 wchar_tByteWidth = ~0U;
808 if (AnyWide) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000809 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
811 wchar_tByteWidth /= 8;
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 // The output buffer size needs to be large enough to hold wide characters.
815 // This is a worst-case assumption which basically corresponds to L"" "long".
816 if (AnyWide)
817 SizeBound *= wchar_tByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 // Size the temporary buffer to hold the result string data.
820 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 // Likewise, but for each string piece.
823 llvm::SmallString<512> TokenBuf;
824 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 // Loop over all the strings, getting their spelling, and expanding them to
827 // wide strings as appropriate.
828 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Anders Carlssonee98ac52007-10-15 02:50:23 +0000830 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
833 const char *ThisTokBuf = &TokenBuf[0];
834 // Get the spelling of the token, which eliminates trigraphs, etc. We know
835 // that ThisTokBuf points to a buffer that is big enough for the whole token
836 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000837 bool StringInvalid = false;
838 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf,
Sean Hunt6cf75022010-08-30 17:47:05 +0000839 &StringInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000840 if (StringInvalid) {
841 hadError = 1;
842 continue;
843 }
844
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000846 bool wide = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 // Skip L marker for wide strings.
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000850 if (ThisTokBuf[0] == 'L') {
851 wide = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
856 ++ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Anders Carlssonee98ac52007-10-15 02:50:23 +0000858 // Check if this is a pascal string
859 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
860 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Anders Carlssonee98ac52007-10-15 02:50:23 +0000862 // If the \p sequence is found in the first token, we have a pascal string
863 // Otherwise, if we already have a pascal string, ignore the first \p
864 if (i == 0) {
865 ++ThisTokBuf;
866 Pascal = true;
867 } else if (Pascal)
868 ThisTokBuf += 2;
869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 while (ThisTokBuf != ThisTokEnd) {
872 // Is this a span of non-escape characters?
873 if (ThisTokBuf[0] != '\\') {
874 const char *InStart = ThisTokBuf;
875 do {
876 ++ThisTokBuf;
877 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 // Copy the character span over.
880 unsigned Len = ThisTokBuf-InStart;
881 if (!AnyWide) {
882 memcpy(ResultPtr, InStart, Len);
883 ResultPtr += Len;
884 } else {
885 // Note: our internal rep of wide char tokens is always little-endian.
886 for (; Len; --Len, ++InStart) {
887 *ResultPtr++ = InStart[0];
888 // Add zeros at the end.
889 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000890 *ResultPtr++ = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
892 }
893 continue;
894 }
Steve Naroff4e93b342009-04-01 11:09:15 +0000895 // Is this a Universal Character Name escape?
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000896 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Mike Stump1eb44332009-09-09 15:08:12 +0000897 ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000898 hadError, StringToks[i].getLocation(), PP, wide,
899 Complain);
Steve Naroff4e93b342009-04-01 11:09:15 +0000900 continue;
901 }
902 // Otherwise, this is a non-UCN escape character. Process it.
903 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
904 StringToks[i].getLocation(),
Chris Lattner37dd3ec2010-06-15 18:06:43 +0000905 AnyWide, PP, Complain);
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Steve Naroff4e93b342009-04-01 11:09:15 +0000907 // Note: our internal rep of wide char tokens is always little-endian.
908 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Steve Naroff4e93b342009-04-01 11:09:15 +0000910 if (AnyWide) {
911 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
912 *ResultPtr++ = ResultChar >> i*8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 }
914 }
915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000917 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +0000918 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Fariborz Jahanian64a80342010-05-28 19:40:48 +0000919 if (AnyWide)
920 ResultBuf[0] /= wchar_tByteWidth;
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000921
922 // Verify that pascal strings aren't too large.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000923 if (GetStringLength() > 256 && Complain) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000924 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
925 << SourceRange(StringToks[0].getLocation(),
926 StringToks[NumStringToks-1].getLocation());
Eli Friedman57d7dde2009-04-01 03:17:08 +0000927 hadError = 1;
928 return;
929 }
Douglas Gregor427c4922010-07-20 14:33:20 +0000930 } else if (Complain) {
931 // Complain if this string literal has too many characters.
932 unsigned MaxChars = PP.getLangOptions().CPlusPlus? 65536
933 : PP.getLangOptions().C99 ? 4095
934 : 509;
935
936 if (GetNumStringChars() > MaxChars)
937 PP.Diag(StringToks[0].getLocation(), diag::ext_string_too_long)
938 << GetNumStringChars() << MaxChars
939 << (PP.getLangOptions().CPlusPlus? 2
940 : PP.getLangOptions().C99 ? 1
941 : 0)
942 << SourceRange(StringToks[0].getLocation(),
943 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000944 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000945}
Chris Lattner719e6152009-02-18 19:21:10 +0000946
947
948/// getOffsetOfStringByte - This function returns the offset of the
949/// specified byte of the string data represented by Token. This handles
950/// advancing over escape sequences in the string.
951unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
952 unsigned ByteNo,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000953 Preprocessor &PP,
954 bool Complain) {
Chris Lattner719e6152009-02-18 19:21:10 +0000955 // Get the spelling of the token.
956 llvm::SmallString<16> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +0000957 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregor50f6af72010-03-16 05:20:39 +0000959 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +0000960 const char *SpellingPtr = &SpellingBuffer[0];
Douglas Gregor50f6af72010-03-16 05:20:39 +0000961 unsigned TokLen = PP.getSpelling(Tok, SpellingPtr, &StringInvalid);
962 if (StringInvalid) {
963 return 0;
964 }
Chris Lattner719e6152009-02-18 19:21:10 +0000965
966 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
967
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattner719e6152009-02-18 19:21:10 +0000969 const char *SpellingStart = SpellingPtr;
970 const char *SpellingEnd = SpellingPtr+TokLen;
971
972 // Skip over the leading quote.
973 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
974 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner719e6152009-02-18 19:21:10 +0000976 // Skip over bytes until we find the offset we're looking for.
977 while (ByteNo) {
978 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattner719e6152009-02-18 19:21:10 +0000980 // Step over non-escapes simply.
981 if (*SpellingPtr != '\\') {
982 ++SpellingPtr;
983 --ByteNo;
984 continue;
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattner719e6152009-02-18 19:21:10 +0000987 // Otherwise, this is an escape character. Advance over it.
988 bool HadError = false;
989 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000990 Tok.getLocation(), false, PP, Complain);
Chris Lattner719e6152009-02-18 19:21:10 +0000991 assert(!HadError && "This method isn't valid on erroneous strings");
992 --ByteNo;
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattner719e6152009-02-18 19:21:10 +0000995 return SpellingPtr-SpellingStart;
996}