blob: b73f236641c01d45ccff77ca8edc9e4fd2fcfa43 [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,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000172 SourceLocation Loc, bool IsWide, Preprocessor &PP,
173 bool Complain)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000174{
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);
194 for (; ThisTokBuf != ThisTokEnd && UcnLen; ++ThisTokBuf, UcnLen--) {
195 int CharVal = HexDigitValue(ThisTokBuf[0]);
196 if (CharVal == -1) break;
197 UcnVal <<= 4;
198 UcnVal |= CharVal;
199 }
200 // If we didn't consume the proper number of digits, there is a problem.
201 if (UcnLen) {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000202 if (Complain)
203 PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
204 diag::err_ucn_escape_incomplete);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000205 HadError = 1;
206 return;
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000209 if ((UcnVal < 0xa0 &&
210 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump1eb44332009-09-09 15:08:12 +0000211 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000212 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000213 if (Complain)
214 PP.Diag(Loc, diag::err_ucn_escape_invalid);
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000215 HadError = 1;
216 return;
217 }
218 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
219 // The conversion below was inspired by:
220 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000221 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000222 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000223
224 unsigned short bytesToWrite = 0;
225 if (UcnVal < (UTF32)0x80)
226 bytesToWrite = 1;
227 else if (UcnVal < (UTF32)0x800)
228 bytesToWrite = 2;
229 else if (UcnVal < (UTF32)0x10000)
230 bytesToWrite = 3;
231 else
232 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000234 const unsigned byteMask = 0xBF;
235 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000237 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000238 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000239 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000240 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000241 };
242 // Finally, we write the bytes into ResultBuf.
243 ResultBuf += bytesToWrite;
244 switch (bytesToWrite) { // note: everything falls through.
245 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
246 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
247 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
248 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
249 }
250 // Update the buffer.
251 ResultBuf += bytesToWrite;
252}
Reid Spencer5f016e22007-07-11 17:01:13 +0000253
254
255/// integer-constant: [C99 6.4.4.1]
256/// decimal-constant integer-suffix
257/// octal-constant integer-suffix
258/// hexadecimal-constant integer-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000259/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000260/// nonzero-digit
261/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000262/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000263/// 0
264/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000265/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000266/// hexadecimal-prefix hexadecimal-digit
267/// hexadecimal-constant hexadecimal-digit
268/// hexadecimal-prefix: one of
269/// 0x 0X
270/// integer-suffix:
271/// unsigned-suffix [long-suffix]
272/// unsigned-suffix [long-long-suffix]
273/// long-suffix [unsigned-suffix]
274/// long-long-suffix [unsigned-sufix]
275/// nonzero-digit:
276/// 1 2 3 4 5 6 7 8 9
277/// octal-digit:
278/// 0 1 2 3 4 5 6 7
279/// hexadecimal-digit:
280/// 0 1 2 3 4 5 6 7 8 9
281/// a b c d e f
282/// A B C D E F
283/// unsigned-suffix: one of
284/// u U
285/// long-suffix: one of
286/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000287/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000288/// ll LL
289///
290/// floating-constant: [C99 6.4.4.2]
291/// TODO: add rules...
292///
Reid Spencer5f016e22007-07-11 17:01:13 +0000293NumericLiteralParser::
294NumericLiteralParser(const char *begin, const char *end,
295 SourceLocation TokLoc, Preprocessor &pp)
296 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000298 // This routine assumes that the range begin/end matches the regex for integer
299 // and FP constants (specifically, the 'pp-number' regex), and assumes that
300 // the byte at "*end" is both valid and not part of the regex. Because of
301 // this, it doesn't have to check for 'overscan' in various places.
302 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
303 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 s = DigitsBegin = begin;
306 saw_exponent = false;
307 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 isLong = false;
309 isUnsigned = false;
310 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000311 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000312 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000313 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000317 ParseNumberStartingWithZero(TokLoc);
318 if (hadError)
319 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 } else { // the first digit is non-zero
321 radix = 10;
322 s = SkipDigits(s);
323 if (s == ThisTokEnd) {
324 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000325 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000326 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
327 diag::err_invalid_decimal_digit) << std::string(s, s+1);
328 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 return;
330 } else if (*s == '.') {
331 s++;
332 saw_period = true;
333 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000334 }
Chris Lattner4411f462008-09-29 23:12:31 +0000335 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000336 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 s++;
338 saw_exponent = true;
339 if (*s == '+' || *s == '-') s++; // sign
340 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000341 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000343 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000344 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
345 diag::err_exponent_has_no_digits);
346 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000347 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 }
349 }
350 }
351
352 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner506b8de2007-08-26 01:58:14 +0000354 // Parse the suffix. At this point we can classify whether we have an FP or
355 // integer constant.
356 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattner506b8de2007-08-26 01:58:14 +0000358 // Loop over all of the characters of the suffix. If we see something bad,
359 // we break out of the loop.
360 for (; s != ThisTokEnd; ++s) {
361 switch (*s) {
362 case 'f': // FP Suffix for "float"
363 case 'F':
364 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000365 if (isFloat || isLong) break; // FF, LF invalid.
366 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000367 continue; // Success.
368 case 'u':
369 case 'U':
370 if (isFPConstant) break; // Error for floating constant.
371 if (isUnsigned) break; // Cannot be repeated.
372 isUnsigned = true;
373 continue; // Success.
374 case 'l':
375 case 'L':
376 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000377 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattner506b8de2007-08-26 01:58:14 +0000379 // Check for long long. The L's need to be adjacent and the same case.
380 if (s+1 != ThisTokEnd && s[1] == s[0]) {
381 if (isFPConstant) break; // long long invalid for floats.
382 isLongLong = true;
383 ++s; // Eat both of them.
384 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000387 continue; // Success.
388 case 'i':
Steve Naroff0c29b222008-04-04 21:02:54 +0000389 if (PP.getLangOptions().Microsoft) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000390 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000391
Steve Naroff0c29b222008-04-04 21:02:54 +0000392 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000393 if (s + 1 != ThisTokEnd) {
394 switch (s[1]) {
395 case '8':
396 s += 2; // i8 suffix
397 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000398 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000399 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000400 if (s + 2 == ThisTokEnd) break;
401 if (s[2] == '6') s += 3; // i16 suffix
402 else if (s[2] == '2') {
403 if (s + 3 == ThisTokEnd) break;
404 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000405 }
406 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000407 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000408 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000409 if (s + 2 == ThisTokEnd) break;
410 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000411 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000412 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000413 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000414 if (s + 2 == ThisTokEnd) break;
415 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000416 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000417 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000418 default:
419 break;
420 }
421 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000422 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000423 }
424 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000425 case 'I':
426 case 'j':
427 case 'J':
428 if (isImaginary) break; // Cannot be repeated.
429 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
430 diag::ext_imaginary_constant);
431 isImaginary = true;
432 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000434 // If we reached here, there was an error.
435 break;
436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner506b8de2007-08-26 01:58:14 +0000438 // Report an error if there are any.
439 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000440 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
441 isFPConstant ? diag::err_invalid_suffix_float_constant :
442 diag::err_invalid_suffix_integer_constant)
443 << std::string(SuffixBegin, ThisTokEnd);
444 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000445 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 }
447}
448
Chris Lattner368328c2008-06-30 06:39:54 +0000449/// ParseNumberStartingWithZero - This method is called when the first character
450/// of the number is found to be a zero. This means it is either an octal
451/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000452/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000453/// radix etc.
454void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
455 assert(s[0] == '0' && "Invalid method call");
456 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner368328c2008-06-30 06:39:54 +0000458 // Handle a hex number like 0x1234.
459 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
460 s++;
461 radix = 16;
462 DigitsBegin = s;
463 s = SkipHexDigits(s);
464 if (s == ThisTokEnd) {
465 // Done.
466 } else if (*s == '.') {
467 s++;
468 saw_period = true;
469 s = SkipHexDigits(s);
470 }
471 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000472 // binary exponent is required.
Sean Hunt8c723402010-01-10 23:37:56 +0000473 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner368328c2008-06-30 06:39:54 +0000474 const char *Exponent = s;
475 s++;
476 saw_exponent = true;
477 if (*s == '+' || *s == '-') s++; // sign
478 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000479 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000480 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
481 diag::err_exponent_has_no_digits);
482 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000483 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000484 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000485 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Sean Hunt8c723402010-01-10 23:37:56 +0000487 // In C++0x, we cannot support hexadecmial floating literals because
488 // they conflict with user-defined literals, so we warn in previous
489 // versions of C++ by default.
490 if (PP.getLangOptions().CPlusPlus)
491 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
492 else if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000493 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000494 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000495 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
496 diag::err_hexconstant_requires_exponent);
497 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000498 }
499 return;
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner368328c2008-06-30 06:39:54 +0000502 // Handle simple binary numbers 0b01010
503 if (*s == 'b' || *s == 'B') {
504 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000505 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000506 ++s;
507 radix = 2;
508 DigitsBegin = s;
509 s = SkipBinaryDigits(s);
510 if (s == ThisTokEnd) {
511 // Done.
512 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000513 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
514 diag::err_invalid_binary_digit) << std::string(s, s+1);
515 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000516 }
Chris Lattner413d3552008-06-30 06:44:49 +0000517 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000518 return;
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner368328c2008-06-30 06:39:54 +0000521 // For now, the radix is set to 8. If we discover that we have a
522 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000523 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000524 radix = 8;
525 DigitsBegin = s;
526 s = SkipOctalDigits(s);
527 if (s == ThisTokEnd)
528 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattner413d3552008-06-30 06:44:49 +0000530 // If we have some other non-octal digit that *is* a decimal digit, see if
531 // this is part of a floating point number like 094.123 or 09e1.
532 if (isdigit(*s)) {
533 const char *EndDecimal = SkipDigits(s);
534 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
535 s = EndDecimal;
536 radix = 10;
537 }
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner413d3552008-06-30 06:44:49 +0000540 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
541 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000542 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000543 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
544 diag::err_invalid_octal_digit) << std::string(s, s+1);
545 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000546 return;
547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner368328c2008-06-30 06:39:54 +0000549 if (*s == '.') {
550 s++;
551 radix = 10;
552 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000553 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000554 }
555 if (*s == 'e' || *s == 'E') { // exponent
556 const char *Exponent = s;
557 s++;
558 radix = 10;
559 saw_exponent = true;
560 if (*s == '+' || *s == '-') s++; // sign
561 const char *first_non_digit = SkipDigits(s);
562 if (first_non_digit != s) {
563 s = first_non_digit;
564 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000565 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000566 diag::err_exponent_has_no_digits);
567 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000568 return;
569 }
570 }
571}
572
573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574/// GetIntegerValue - Convert this numeric literal value to an APInt that
575/// matches Val's input width. If there is an overflow, set Val to the low bits
576/// of the result and return true. Otherwise, return false.
577bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000578 // Fast path: Compute a conservative bound on the maximum number of
579 // bits per digit in this radix. If we can't possibly overflow a
580 // uint64 based on that bound then do the simple conversion to
581 // integer. This avoids the expensive overflow checking below, and
582 // handles the common cases that matter (small decimal integers and
583 // hex/octal values which don't overflow).
584 unsigned MaxBitsPerDigit = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000585 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbara179be32008-10-16 07:32:01 +0000586 MaxBitsPerDigit += 1;
587 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
588 uint64_t N = 0;
589 for (s = DigitsBegin; s != SuffixBegin; ++s)
590 N = N*radix + HexDigitValue(*s);
591
592 // This will truncate the value to Val's input width. Simply check
593 // for overflow by comparing.
594 Val = N;
595 return Val.getZExtValue() != N;
596 }
597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 Val = 0;
599 s = DigitsBegin;
600
601 llvm::APInt RadixVal(Val.getBitWidth(), radix);
602 llvm::APInt CharVal(Val.getBitWidth(), 0);
603 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 bool OverflowOccurred = false;
606 while (s < SuffixBegin) {
607 unsigned C = HexDigitValue(*s++);
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 // If this letter is out of bound for this radix, reject it.
610 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 // Add the digit to the value in the appropriate radix. If adding in digits
615 // made the value smaller, then this overflowed.
616 OldVal = Val;
617
618 // Multiply by radix, did overflow occur on the multiply?
619 Val *= RadixVal;
620 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000623 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 OverflowOccurred |= Val.ult(CharVal);
626 }
627 return OverflowOccurred;
628}
629
John McCall94c939d2009-12-24 09:08:04 +0000630llvm::APFloat::opStatus
631NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000632 using llvm::APFloat;
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000633 using llvm::StringRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000635 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000636 return Result.convertFromString(StringRef(ThisTokBegin, n),
637 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000638}
639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640
641CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
642 SourceLocation Loc, Preprocessor &PP) {
643 // At this point we know that the character matches the regex "L?'.*'".
644 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 // Determine if this is a wide character.
647 IsWide = begin[0] == 'L';
648 if (IsWide) ++begin;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 // Skip over the entry quote.
651 assert(begin[0] == '\'' && "Invalid token lexed");
652 ++begin;
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000655 // upto 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000657 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000659 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
660 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
661 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
662 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
663 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000664
Mike Stump1eb44332009-09-09 15:08:12 +0000665 // This is what we will use for overflow detection
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000666 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Chris Lattnere3ad8812009-04-28 21:51:46 +0000668 unsigned NumCharsSoFar = 0;
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000669 bool Warned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 while (begin[0] != '\'') {
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000671 uint64_t ResultChar;
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 if (begin[0] != '\\') // If this is a normal character, consume it.
673 ResultChar = *begin++;
674 else // Otherwise, this is an escape character.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000675 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP,
676 /*Complain=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000677
678 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
679 // implementation defined (C99 6.4.4.4p10).
Chris Lattnere3ad8812009-04-28 21:51:46 +0000680 if (NumCharsSoFar) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 if (IsWide) {
682 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000683 LitVal = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 } else {
685 // Narrow character literals act as though their value is concatenated
Chris Lattnere3ad8812009-04-28 21:51:46 +0000686 // in this implementation, but warn on overflow.
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000687 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000689 Warned = true;
690 }
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000691 LitVal <<= 8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 }
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000695 LitVal = LitVal + ResultChar;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000696 ++NumCharsSoFar;
697 }
698
699 // If this is the second character being processed, do special handling.
700 if (NumCharsSoFar > 1) {
701 // Warn about discarding the top bits for multi-char wide-character
702 // constants (L'abcd').
703 if (IsWide)
704 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
705 else if (NumCharsSoFar != 4)
706 PP.Diag(Loc, diag::ext_multichar_character_literal);
707 else
708 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +0000709 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +0000710 } else
711 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000712
713 // Transfer the value from APInt to uint64_t
714 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
717 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
718 // character constants are not sign extended in the this implementation:
719 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattnere3ad8812009-04-28 21:51:46 +0000720 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman15b91762009-06-05 07:05:05 +0000721 PP.getLangOptions().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 Value = (signed char)Value;
723}
724
725
726/// string-literal: [C99 6.4.5]
727/// " [s-char-sequence] "
728/// L" [s-char-sequence] "
729/// s-char-sequence:
730/// s-char
731/// s-char-sequence s-char
732/// s-char:
733/// any source character except the double quote ",
734/// backslash \, or newline character
735/// escape-character
736/// universal-character-name
737/// escape-character: [C99 6.4.4.4]
738/// \ escape-code
739/// universal-character-name
740/// escape-code:
741/// character-escape-code
742/// octal-escape-code
743/// hex-escape-code
744/// character-escape-code: one of
745/// n t b r f v a
746/// \ ' " ?
747/// octal-escape-code:
748/// octal-digit
749/// octal-digit octal-digit
750/// octal-digit octal-digit octal-digit
751/// hex-escape-code:
752/// x hex-digit
753/// hex-escape-code hex-digit
754/// universal-character-name:
755/// \u hex-quad
756/// \U hex-quad hex-quad
757/// hex-quad:
758/// hex-digit hex-digit hex-digit hex-digit
759///
760StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000761StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000762 Preprocessor &pp, bool Complain) : PP(pp) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 // Scan all of the string portions, remember the max individual token length,
764 // computing a bound on the concatenated string length, and see whether any
765 // piece is a wide-string. If any of the string portions is a wide-string
766 // literal, the result is a wide-string literal [C99 6.4.5p4].
767 MaxTokenLength = StringToks[0].getLength();
768 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000769 AnyWide = StringToks[0].is(tok::wide_string_literal);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 hadError = false;
772
773 // Implement Translation Phase #6: concatenation of string literals
774 /// (C99 5.1.1.2p1). The common case is only one string fragment.
775 for (unsigned i = 1; i != NumStringToks; ++i) {
776 // The string could be shorter than this if it needs cleaning, but this is a
777 // reasonable bound, which is all we need.
778 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 // Remember maximum string piece length.
Mike Stump1eb44332009-09-09 15:08:12 +0000781 if (StringToks[i].getLength() > MaxTokenLength)
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // Remember if we see any wide strings.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000785 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +0000787
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 // Include space for the null terminator.
789 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
794 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
795 wchar_tByteWidth = ~0U;
796 if (AnyWide) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000797 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
799 wchar_tByteWidth /= 8;
800 }
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 // The output buffer size needs to be large enough to hold wide characters.
803 // This is a worst-case assumption which basically corresponds to L"" "long".
804 if (AnyWide)
805 SizeBound *= wchar_tByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // Size the temporary buffer to hold the result string data.
808 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 // Likewise, but for each string piece.
811 llvm::SmallString<512> TokenBuf;
812 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 // Loop over all the strings, getting their spelling, and expanding them to
815 // wide strings as appropriate.
816 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Anders Carlssonee98ac52007-10-15 02:50:23 +0000818 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
821 const char *ThisTokBuf = &TokenBuf[0];
822 // Get the spelling of the token, which eliminates trigraphs, etc. We know
823 // that ThisTokBuf points to a buffer that is big enough for the whole token
824 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000825 bool StringInvalid = false;
826 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf,
827 &StringInvalid);
828 if (StringInvalid) {
829 hadError = 1;
830 continue;
831 }
832
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 // Skip L marker for wide strings.
838 bool ThisIsWide = false;
839 if (ThisTokBuf[0] == 'L') {
840 ++ThisTokBuf;
841 ThisIsWide = true;
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
845 ++ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Anders Carlssonee98ac52007-10-15 02:50:23 +0000847 // Check if this is a pascal string
848 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
849 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Anders Carlssonee98ac52007-10-15 02:50:23 +0000851 // If the \p sequence is found in the first token, we have a pascal string
852 // Otherwise, if we already have a pascal string, ignore the first \p
853 if (i == 0) {
854 ++ThisTokBuf;
855 Pascal = true;
856 } else if (Pascal)
857 ThisTokBuf += 2;
858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 while (ThisTokBuf != ThisTokEnd) {
861 // Is this a span of non-escape characters?
862 if (ThisTokBuf[0] != '\\') {
863 const char *InStart = ThisTokBuf;
864 do {
865 ++ThisTokBuf;
866 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 // Copy the character span over.
869 unsigned Len = ThisTokBuf-InStart;
870 if (!AnyWide) {
871 memcpy(ResultPtr, InStart, Len);
872 ResultPtr += Len;
873 } else {
874 // Note: our internal rep of wide char tokens is always little-endian.
875 for (; Len; --Len, ++InStart) {
876 *ResultPtr++ = InStart[0];
877 // Add zeros at the end.
878 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000879 *ResultPtr++ = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 }
881 }
882 continue;
883 }
Steve Naroff4e93b342009-04-01 11:09:15 +0000884 // Is this a Universal Character Name escape?
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000885 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Mike Stump1eb44332009-09-09 15:08:12 +0000886 ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000887 hadError, StringToks[i].getLocation(), ThisIsWide, PP,
888 Complain);
Steve Naroff4e93b342009-04-01 11:09:15 +0000889 continue;
890 }
891 // Otherwise, this is a non-UCN escape character. Process it.
892 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
893 StringToks[i].getLocation(),
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000894 ThisIsWide, PP, Complain);
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Steve Naroff4e93b342009-04-01 11:09:15 +0000896 // Note: our internal rep of wide char tokens is always little-endian.
897 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Steve Naroff4e93b342009-04-01 11:09:15 +0000899 if (AnyWide) {
900 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
901 *ResultPtr++ = ResultChar >> i*8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 }
903 }
904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000906 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +0000907 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000908
909 // Verify that pascal strings aren't too large.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000910 if (GetStringLength() > 256 && Complain) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000911 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
912 << SourceRange(StringToks[0].getLocation(),
913 StringToks[NumStringToks-1].getLocation());
Eli Friedman57d7dde2009-04-01 03:17:08 +0000914 hadError = 1;
915 return;
916 }
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000917 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000918}
Chris Lattner719e6152009-02-18 19:21:10 +0000919
920
921/// getOffsetOfStringByte - This function returns the offset of the
922/// specified byte of the string data represented by Token. This handles
923/// advancing over escape sequences in the string.
924unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
925 unsigned ByteNo,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000926 Preprocessor &PP,
927 bool Complain) {
Chris Lattner719e6152009-02-18 19:21:10 +0000928 // Get the spelling of the token.
929 llvm::SmallString<16> SpellingBuffer;
930 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregor50f6af72010-03-16 05:20:39 +0000932 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +0000933 const char *SpellingPtr = &SpellingBuffer[0];
Douglas Gregor50f6af72010-03-16 05:20:39 +0000934 unsigned TokLen = PP.getSpelling(Tok, SpellingPtr, &StringInvalid);
935 if (StringInvalid) {
936 return 0;
937 }
Chris Lattner719e6152009-02-18 19:21:10 +0000938
939 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
940
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattner719e6152009-02-18 19:21:10 +0000942 const char *SpellingStart = SpellingPtr;
943 const char *SpellingEnd = SpellingPtr+TokLen;
944
945 // Skip over the leading quote.
946 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
947 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattner719e6152009-02-18 19:21:10 +0000949 // Skip over bytes until we find the offset we're looking for.
950 while (ByteNo) {
951 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner719e6152009-02-18 19:21:10 +0000953 // Step over non-escapes simply.
954 if (*SpellingPtr != '\\') {
955 ++SpellingPtr;
956 --ByteNo;
957 continue;
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Chris Lattner719e6152009-02-18 19:21:10 +0000960 // Otherwise, this is an escape character. Advance over it.
961 bool HadError = false;
962 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000963 Tok.getLocation(), false, PP, Complain);
Chris Lattner719e6152009-02-18 19:21:10 +0000964 assert(!HadError && "This method isn't valid on erroneous strings");
965 --ByteNo;
966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner719e6152009-02-18 19:21:10 +0000968 return SpellingPtr-SpellingStart;
969}