blob: 5cd54975055d161c58963e582558d533bdb2cb57 [file] [log] [blame]
Chris Lattner2f5add62007-04-05 06:57:15 +00001//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
Steve Naroff09ef4742007-03-09 23:16:33 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Steve Naroff09ef4742007-03-09 23:16:33 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner2f5add62007-04-05 06:57:15 +000010// This file implements the NumericLiteralParser, CharLiteralParser, and
11// StringLiteralParser interfaces.
Steve Naroff09ef4742007-03-09 23:16:33 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/LiteralSupport.h"
16#include "clang/Lex/Preprocessor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerbb1b44f2007-07-16 06:55:01 +000018#include "clang/Basic/TargetInfo.h"
Erick Tryzelaarb9073112009-08-16 23:36:28 +000019#include "llvm/ADT/StringRef.h"
Steve Naroff4f88b312007-03-13 22:37:02 +000020#include "llvm/ADT/StringExtras.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000021using namespace clang;
22
Chris Lattner2f5add62007-04-05 06:57:15 +000023/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
24/// not valid.
25static int HexDigitValue(char C) {
26 if (C >= '0' && C <= '9') return C-'0';
27 if (C >= 'a' && C <= 'f') return C-'a'+10;
28 if (C >= 'A' && C <= 'F') return C-'A'+10;
29 return -1;
30}
31
32/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
33/// either a character or a string literal.
34static unsigned ProcessCharEscape(const char *&ThisTokBuf,
35 const char *ThisTokEnd, bool &HadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +000036 SourceLocation Loc, bool IsWide,
37 Preprocessor &PP) {
Chris Lattner2f5add62007-04-05 06:57:15 +000038 // Skip the '\' char.
39 ++ThisTokBuf;
40
41 // We know that this character can't be off the end of the buffer, because
42 // that would have been \", which would not have been the end of string.
43 unsigned ResultChar = *ThisTokBuf++;
44 switch (ResultChar) {
45 // These map to themselves.
46 case '\\': case '\'': case '"': case '?': break;
Mike Stump11289f42009-09-09 15:08:12 +000047
Chris Lattner2f5add62007-04-05 06:57:15 +000048 // These have fixed mappings.
49 case 'a':
50 // TODO: K&R: the meaning of '\\a' is different in traditional C
51 ResultChar = 7;
52 break;
53 case 'b':
54 ResultChar = 8;
55 break;
56 case 'e':
Chris Lattnere05c4df2008-11-18 21:48:13 +000057 PP.Diag(Loc, diag::ext_nonstandard_escape) << "e";
Chris Lattner2f5add62007-04-05 06:57:15 +000058 ResultChar = 27;
59 break;
Eli Friedman28a00aa2009-06-10 01:32:39 +000060 case 'E':
61 PP.Diag(Loc, diag::ext_nonstandard_escape) << "E";
62 ResultChar = 27;
63 break;
Chris Lattner2f5add62007-04-05 06:57:15 +000064 case 'f':
65 ResultChar = 12;
66 break;
67 case 'n':
68 ResultChar = 10;
69 break;
70 case 'r':
71 ResultChar = 13;
72 break;
73 case 't':
74 ResultChar = 9;
75 break;
76 case 'v':
77 ResultChar = 11;
78 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +000079 case 'x': { // Hex escape.
80 ResultChar = 0;
81 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner2f5add62007-04-05 06:57:15 +000082 PP.Diag(Loc, diag::err_hex_escape_no_digits);
83 HadError = 1;
Chris Lattner2f5add62007-04-05 06:57:15 +000084 break;
85 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner812eda82007-05-20 05:17:04 +000087 // Hex escapes are a maximal series of hex digits.
Chris Lattnerc10adde2007-05-20 05:00:58 +000088 bool Overflow = false;
89 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
90 int CharVal = HexDigitValue(ThisTokBuf[0]);
91 if (CharVal == -1) break;
Chris Lattner59f09b62008-09-30 20:45:40 +000092 // About to shift out a digit?
93 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Chris Lattnerc10adde2007-05-20 05:00:58 +000094 ResultChar <<= 4;
95 ResultChar |= CharVal;
96 }
97
98 // See if any bits will be truncated when evaluated as a character.
Alisdair Meredithed28f6e2009-07-14 08:10:06 +000099 unsigned CharWidth = IsWide
100 ? PP.getTargetInfo().getWCharWidth()
101 : PP.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattnerc10adde2007-05-20 05:00:58 +0000103 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
104 Overflow = true;
105 ResultChar &= ~0U >> (32-CharWidth);
106 }
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerc10adde2007-05-20 05:00:58 +0000108 // Check for overflow.
109 if (Overflow) // Too many digits to fit in
110 PP.Diag(Loc, diag::warn_hex_escape_too_large);
Chris Lattner2f5add62007-04-05 06:57:15 +0000111 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000112 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000113 case '0': case '1': case '2': case '3':
Chris Lattner812eda82007-05-20 05:17:04 +0000114 case '4': case '5': case '6': case '7': {
Chris Lattner2f5add62007-04-05 06:57:15 +0000115 // Octal escapes.
Chris Lattner3f4b6e32007-06-09 06:20:47 +0000116 --ThisTokBuf;
Chris Lattner812eda82007-05-20 05:17:04 +0000117 ResultChar = 0;
118
119 // Octal escapes are a series of octal digits with maximum length 3.
120 // "\0123" is a two digit sequence equal to "\012" "3".
121 unsigned NumDigits = 0;
122 do {
123 ResultChar <<= 3;
124 ResultChar |= *ThisTokBuf++ - '0';
125 ++NumDigits;
126 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
127 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump11289f42009-09-09 15:08:12 +0000128
Chris Lattner812eda82007-05-20 05:17:04 +0000129 // Check for overflow. Reject '\777', but not L'\777'.
Alisdair Meredithed28f6e2009-07-14 08:10:06 +0000130 unsigned CharWidth = IsWide
131 ? PP.getTargetInfo().getWCharWidth()
132 : PP.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattner812eda82007-05-20 05:17:04 +0000134 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
135 PP.Diag(Loc, diag::warn_octal_escape_too_large);
136 ResultChar &= ~0U >> (32-CharWidth);
137 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000138 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000139 }
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattner2f5add62007-04-05 06:57:15 +0000141 // Otherwise, these are not valid escapes.
142 case '(': case '{': case '[': case '%':
143 // GCC accepts these as extensions. We warn about them as such though.
Eli Friedman5d72d412009-04-28 00:51:18 +0000144 PP.Diag(Loc, diag::ext_nonstandard_escape)
145 << std::string()+(char)ResultChar;
146 break;
Chris Lattner2f5add62007-04-05 06:57:15 +0000147 default:
Chris Lattner59acca52008-11-22 07:23:31 +0000148 if (isgraph(ThisTokBuf[0]))
Chris Lattnere05c4df2008-11-18 21:48:13 +0000149 PP.Diag(Loc, diag::ext_unknown_escape) << std::string()+(char)ResultChar;
Chris Lattner59acca52008-11-22 07:23:31 +0000150 else
Chris Lattnere05c4df2008-11-18 21:48:13 +0000151 PP.Diag(Loc, diag::ext_unknown_escape) << "x"+llvm::utohexstr(ResultChar);
Chris Lattner2f5add62007-04-05 06:57:15 +0000152 break;
153 }
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattner2f5add62007-04-05 06:57:15 +0000155 return ResultChar;
156}
157
Steve Naroff7b753d22009-03-30 23:46:03 +0000158/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
159/// convert the UTF32 to UTF8. This is a subroutine of StringLiteralParser.
160/// When we decide to implement UCN's for character constants and identifiers,
161/// we will likely rework our support for UCN's.
Mike Stump11289f42009-09-09 15:08:12 +0000162static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
163 char *&ResultBuf, bool &HadError,
164 SourceLocation Loc, bool IsWide, Preprocessor &PP)
Steve Narofff2a880c2009-03-31 10:29:45 +0000165{
Steve Naroff7b753d22009-03-30 23:46:03 +0000166 // FIXME: Add a warning - UCN's are only valid in C++ & C99.
Steve Narofff2a880c2009-03-31 10:29:45 +0000167 // FIXME: Handle wide strings.
Mike Stump11289f42009-09-09 15:08:12 +0000168
Steve Naroffc94adda2009-04-01 11:09:15 +0000169 // Save the beginning of the string (for error diagnostics).
170 const char *ThisTokBegin = ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000171
Steve Naroff7b753d22009-03-30 23:46:03 +0000172 // Skip the '\u' char's.
173 ThisTokBuf += 2;
Chris Lattner2f5add62007-04-05 06:57:15 +0000174
Steve Naroff7b753d22009-03-30 23:46:03 +0000175 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
176 PP.Diag(Loc, diag::err_ucn_escape_no_digits);
177 HadError = 1;
178 return;
179 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000180 typedef uint32_t UTF32;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Steve Naroff7b753d22009-03-30 23:46:03 +0000182 UTF32 UcnVal = 0;
183 unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
184 for (; ThisTokBuf != ThisTokEnd && UcnLen; ++ThisTokBuf, UcnLen--) {
185 int CharVal = HexDigitValue(ThisTokBuf[0]);
186 if (CharVal == -1) break;
187 UcnVal <<= 4;
188 UcnVal |= CharVal;
189 }
190 // If we didn't consume the proper number of digits, there is a problem.
191 if (UcnLen) {
Steve Naroffc94adda2009-04-01 11:09:15 +0000192 PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
193 diag::err_ucn_escape_incomplete);
Steve Naroff7b753d22009-03-30 23:46:03 +0000194 HadError = 1;
195 return;
196 }
Mike Stump11289f42009-09-09 15:08:12 +0000197 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff7b753d22009-03-30 23:46:03 +0000198 if ((UcnVal < 0xa0 &&
199 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump11289f42009-09-09 15:08:12 +0000200 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Narofff2a880c2009-03-31 10:29:45 +0000201 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Steve Naroff7b753d22009-03-30 23:46:03 +0000202 PP.Diag(Loc, diag::err_ucn_escape_invalid);
203 HadError = 1;
204 return;
205 }
206 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
207 // The conversion below was inspired by:
208 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump11289f42009-09-09 15:08:12 +0000209 // First, we determine how many bytes the result will require.
Steve Naroffc94adda2009-04-01 11:09:15 +0000210 typedef uint8_t UTF8;
Steve Naroff7b753d22009-03-30 23:46:03 +0000211
212 unsigned short bytesToWrite = 0;
213 if (UcnVal < (UTF32)0x80)
214 bytesToWrite = 1;
215 else if (UcnVal < (UTF32)0x800)
216 bytesToWrite = 2;
217 else if (UcnVal < (UTF32)0x10000)
218 bytesToWrite = 3;
219 else
220 bytesToWrite = 4;
Mike Stump11289f42009-09-09 15:08:12 +0000221
Steve Naroff7b753d22009-03-30 23:46:03 +0000222 const unsigned byteMask = 0xBF;
223 const unsigned byteMark = 0x80;
Mike Stump11289f42009-09-09 15:08:12 +0000224
Steve Naroff7b753d22009-03-30 23:46:03 +0000225 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Narofff2a880c2009-03-31 10:29:45 +0000226 // into the first byte, depending on how many bytes follow.
Mike Stump11289f42009-09-09 15:08:12 +0000227 static const UTF8 firstByteMark[5] = {
Steve Narofff2a880c2009-03-31 10:29:45 +0000228 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff7b753d22009-03-30 23:46:03 +0000229 };
230 // Finally, we write the bytes into ResultBuf.
231 ResultBuf += bytesToWrite;
232 switch (bytesToWrite) { // note: everything falls through.
233 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
234 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
235 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
236 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
237 }
238 // Update the buffer.
239 ResultBuf += bytesToWrite;
240}
Chris Lattner2f5add62007-04-05 06:57:15 +0000241
242
Steve Naroff09ef4742007-03-09 23:16:33 +0000243/// integer-constant: [C99 6.4.4.1]
244/// decimal-constant integer-suffix
245/// octal-constant integer-suffix
246/// hexadecimal-constant integer-suffix
Mike Stump11289f42009-09-09 15:08:12 +0000247/// decimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000248/// nonzero-digit
249/// decimal-constant digit
Mike Stump11289f42009-09-09 15:08:12 +0000250/// octal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000251/// 0
252/// octal-constant octal-digit
Mike Stump11289f42009-09-09 15:08:12 +0000253/// hexadecimal-constant:
Steve Naroff09ef4742007-03-09 23:16:33 +0000254/// hexadecimal-prefix hexadecimal-digit
255/// hexadecimal-constant hexadecimal-digit
256/// hexadecimal-prefix: one of
257/// 0x 0X
258/// integer-suffix:
259/// unsigned-suffix [long-suffix]
260/// unsigned-suffix [long-long-suffix]
261/// long-suffix [unsigned-suffix]
262/// long-long-suffix [unsigned-sufix]
263/// nonzero-digit:
264/// 1 2 3 4 5 6 7 8 9
265/// octal-digit:
266/// 0 1 2 3 4 5 6 7
267/// hexadecimal-digit:
268/// 0 1 2 3 4 5 6 7 8 9
269/// a b c d e f
270/// A B C D E F
271/// unsigned-suffix: one of
272/// u U
273/// long-suffix: one of
274/// l L
Mike Stump11289f42009-09-09 15:08:12 +0000275/// long-long-suffix: one of
Steve Naroff09ef4742007-03-09 23:16:33 +0000276/// ll LL
277///
278/// floating-constant: [C99 6.4.4.2]
279/// TODO: add rules...
280///
Steve Naroff09ef4742007-03-09 23:16:33 +0000281NumericLiteralParser::
282NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000283 SourceLocation TokLoc, Preprocessor &pp)
284 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner59f09b62008-09-30 20:45:40 +0000286 // This routine assumes that the range begin/end matches the regex for integer
287 // and FP constants (specifically, the 'pp-number' regex), and assumes that
288 // the byte at "*end" is both valid and not part of the regex. Because of
289 // this, it doesn't have to check for 'overscan' in various places.
290 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
291 "Lexer didn't maximally munch?");
Mike Stump11289f42009-09-09 15:08:12 +0000292
Steve Naroff09ef4742007-03-09 23:16:33 +0000293 s = DigitsBegin = begin;
294 saw_exponent = false;
295 saw_period = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000296 isLong = false;
297 isUnsigned = false;
298 isLongLong = false;
Chris Lattnered045422007-08-26 03:29:23 +0000299 isFloat = false;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000300 isImaginary = false;
Mike Stumpc99c0222009-10-08 22:55:36 +0000301 isMicrosoftInteger = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000302 hadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Steve Naroff09ef4742007-03-09 23:16:33 +0000304 if (*s == '0') { // parse radix
Chris Lattner6016a512008-06-30 06:39:54 +0000305 ParseNumberStartingWithZero(TokLoc);
306 if (hadError)
307 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000308 } else { // the first digit is non-zero
309 radix = 10;
310 s = SkipDigits(s);
311 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000312 // Done.
Christopher Lamb42e69f22007-11-29 06:06:27 +0000313 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattner59acca52008-11-22 07:23:31 +0000314 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
315 diag::err_invalid_decimal_digit) << std::string(s, s+1);
316 hadError = true;
Chris Lattner328fa5c2007-06-08 17:12:06 +0000317 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000318 } else if (*s == '.') {
319 s++;
320 saw_period = true;
321 s = SkipDigits(s);
Mike Stump11289f42009-09-09 15:08:12 +0000322 }
Chris Lattnerfb8b8f22008-09-29 23:12:31 +0000323 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner4885b972008-04-20 18:47:55 +0000324 const char *Exponent = s;
Steve Naroff09ef4742007-03-09 23:16:33 +0000325 s++;
326 saw_exponent = true;
327 if (*s == '+' || *s == '-') s++; // sign
328 const char *first_non_digit = SkipDigits(s);
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000329 if (first_non_digit != s) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000330 s = first_non_digit;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000331 } else {
Chris Lattner59acca52008-11-22 07:23:31 +0000332 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
333 diag::err_exponent_has_no_digits);
334 hadError = true;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000335 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000336 }
337 }
338 }
339
340 SuffixBegin = s;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Chris Lattnerf55ab182007-08-26 01:58:14 +0000342 // Parse the suffix. At this point we can classify whether we have an FP or
343 // integer constant.
344 bool isFPConstant = isFloatingLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattnerf55ab182007-08-26 01:58:14 +0000346 // Loop over all of the characters of the suffix. If we see something bad,
347 // we break out of the loop.
348 for (; s != ThisTokEnd; ++s) {
349 switch (*s) {
350 case 'f': // FP Suffix for "float"
351 case 'F':
352 if (!isFPConstant) break; // Error for integer constant.
Chris Lattnered045422007-08-26 03:29:23 +0000353 if (isFloat || isLong) break; // FF, LF invalid.
354 isFloat = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000355 continue; // Success.
356 case 'u':
357 case 'U':
358 if (isFPConstant) break; // Error for floating constant.
359 if (isUnsigned) break; // Cannot be repeated.
360 isUnsigned = true;
361 continue; // Success.
362 case 'l':
363 case 'L':
364 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattnered045422007-08-26 03:29:23 +0000365 if (isFloat) break; // LF invalid.
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattnerf55ab182007-08-26 01:58:14 +0000367 // Check for long long. The L's need to be adjacent and the same case.
368 if (s+1 != ThisTokEnd && s[1] == s[0]) {
369 if (isFPConstant) break; // long long invalid for floats.
370 isLongLong = true;
371 ++s; // Eat both of them.
372 } else {
Steve Naroff09ef4742007-03-09 23:16:33 +0000373 isLong = true;
Steve Naroff09ef4742007-03-09 23:16:33 +0000374 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000375 continue; // Success.
376 case 'i':
Steve Naroffa1f41452008-04-04 21:02:54 +0000377 if (PP.getLangOptions().Microsoft) {
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000378 if (isFPConstant || isUnsigned || isLong || isLongLong) break;
379
Steve Naroffa1f41452008-04-04 21:02:54 +0000380 // Allow i8, i16, i32, i64, and i128.
Mike Stumpc99c0222009-10-08 22:55:36 +0000381 if (s + 1 != ThisTokEnd) {
382 switch (s[1]) {
383 case '8':
384 s += 2; // i8 suffix
385 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000386 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000387 case '1':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000388 if (s + 2 == ThisTokEnd) break;
389 if (s[2] == '6') s += 3; // i16 suffix
390 else if (s[2] == '2') {
391 if (s + 3 == ThisTokEnd) break;
392 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000393 }
394 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000395 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000396 case '3':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000397 if (s + 2 == ThisTokEnd) break;
398 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000399 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000400 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000401 case '6':
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000402 if (s + 2 == ThisTokEnd) break;
403 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpc99c0222009-10-08 22:55:36 +0000404 isMicrosoftInteger = true;
Nuno Lopesbaa1bc42009-11-28 13:37:52 +0000405 break;
Mike Stumpc99c0222009-10-08 22:55:36 +0000406 default:
407 break;
408 }
409 break;
Steve Naroffa1f41452008-04-04 21:02:54 +0000410 }
Steve Naroffa1f41452008-04-04 21:02:54 +0000411 }
412 // fall through.
Chris Lattnerf55ab182007-08-26 01:58:14 +0000413 case 'I':
414 case 'j':
415 case 'J':
416 if (isImaginary) break; // Cannot be repeated.
417 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
418 diag::ext_imaginary_constant);
419 isImaginary = true;
420 continue; // Success.
Steve Naroff09ef4742007-03-09 23:16:33 +0000421 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000422 // If we reached here, there was an error.
423 break;
424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnerf55ab182007-08-26 01:58:14 +0000426 // Report an error if there are any.
427 if (s != ThisTokEnd) {
Chris Lattner59acca52008-11-22 07:23:31 +0000428 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
429 isFPConstant ? diag::err_invalid_suffix_float_constant :
430 diag::err_invalid_suffix_integer_constant)
431 << std::string(SuffixBegin, ThisTokEnd);
432 hadError = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000433 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000434 }
435}
436
Chris Lattner6016a512008-06-30 06:39:54 +0000437/// ParseNumberStartingWithZero - This method is called when the first character
438/// of the number is found to be a zero. This means it is either an octal
439/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump11289f42009-09-09 15:08:12 +0000440/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner6016a512008-06-30 06:39:54 +0000441/// radix etc.
442void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
443 assert(s[0] == '0' && "Invalid method call");
444 s++;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner6016a512008-06-30 06:39:54 +0000446 // Handle a hex number like 0x1234.
447 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
448 s++;
449 radix = 16;
450 DigitsBegin = s;
451 s = SkipHexDigits(s);
452 if (s == ThisTokEnd) {
453 // Done.
454 } else if (*s == '.') {
455 s++;
456 saw_period = true;
457 s = SkipHexDigits(s);
458 }
459 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump11289f42009-09-09 15:08:12 +0000460 // binary exponent is required.
Alexis Hunt91b78382010-01-10 23:37:56 +0000461 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner6016a512008-06-30 06:39:54 +0000462 const char *Exponent = s;
463 s++;
464 saw_exponent = true;
465 if (*s == '+' || *s == '-') s++; // sign
466 const char *first_non_digit = SkipDigits(s);
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000467 if (first_non_digit == s) {
Chris Lattner59acca52008-11-22 07:23:31 +0000468 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
469 diag::err_exponent_has_no_digits);
470 hadError = true;
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000471 return;
Chris Lattner6016a512008-06-30 06:39:54 +0000472 }
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000473 s = first_non_digit;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Alexis Hunt91b78382010-01-10 23:37:56 +0000475 // In C++0x, we cannot support hexadecmial floating literals because
476 // they conflict with user-defined literals, so we warn in previous
477 // versions of C++ by default.
478 if (PP.getLangOptions().CPlusPlus)
479 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
480 else if (!PP.getLangOptions().HexFloats)
Chris Lattner59acca52008-11-22 07:23:31 +0000481 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner6016a512008-06-30 06:39:54 +0000482 } else if (saw_period) {
Chris Lattner59acca52008-11-22 07:23:31 +0000483 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
484 diag::err_hexconstant_requires_exponent);
485 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000486 }
487 return;
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner6016a512008-06-30 06:39:54 +0000490 // Handle simple binary numbers 0b01010
491 if (*s == 'b' || *s == 'B') {
492 // 0b101010 is a GCC extension.
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000493 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner6016a512008-06-30 06:39:54 +0000494 ++s;
495 radix = 2;
496 DigitsBegin = s;
497 s = SkipBinaryDigits(s);
498 if (s == ThisTokEnd) {
499 // Done.
500 } else if (isxdigit(*s)) {
Chris Lattner59acca52008-11-22 07:23:31 +0000501 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
502 diag::err_invalid_binary_digit) << std::string(s, s+1);
503 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000504 }
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000505 // Other suffixes will be diagnosed by the caller.
Chris Lattner6016a512008-06-30 06:39:54 +0000506 return;
507 }
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner6016a512008-06-30 06:39:54 +0000509 // For now, the radix is set to 8. If we discover that we have a
510 // floating point constant, the radix will change to 10. Octal floating
Mike Stump11289f42009-09-09 15:08:12 +0000511 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner6016a512008-06-30 06:39:54 +0000512 radix = 8;
513 DigitsBegin = s;
514 s = SkipOctalDigits(s);
515 if (s == ThisTokEnd)
516 return; // Done, simple octal number like 01234
Mike Stump11289f42009-09-09 15:08:12 +0000517
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000518 // If we have some other non-octal digit that *is* a decimal digit, see if
519 // this is part of a floating point number like 094.123 or 09e1.
520 if (isdigit(*s)) {
521 const char *EndDecimal = SkipDigits(s);
522 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
523 s = EndDecimal;
524 radix = 10;
525 }
526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000528 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
529 // the code is using an incorrect base.
Chris Lattner6016a512008-06-30 06:39:54 +0000530 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattner59acca52008-11-22 07:23:31 +0000531 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
532 diag::err_invalid_octal_digit) << std::string(s, s+1);
533 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000534 return;
535 }
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner6016a512008-06-30 06:39:54 +0000537 if (*s == '.') {
538 s++;
539 radix = 10;
540 saw_period = true;
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000541 s = SkipDigits(s); // Skip suffix.
Chris Lattner6016a512008-06-30 06:39:54 +0000542 }
543 if (*s == 'e' || *s == 'E') { // exponent
544 const char *Exponent = s;
545 s++;
546 radix = 10;
547 saw_exponent = true;
548 if (*s == '+' || *s == '-') s++; // sign
549 const char *first_non_digit = SkipDigits(s);
550 if (first_non_digit != s) {
551 s = first_non_digit;
552 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000553 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattner59acca52008-11-22 07:23:31 +0000554 diag::err_exponent_has_no_digits);
555 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000556 return;
557 }
558 }
559}
560
561
Chris Lattner5b743d32007-04-04 05:52:58 +0000562/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000563/// matches Val's input width. If there is an overflow, set Val to the low bits
564/// of the result and return true. Otherwise, return false.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000565bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbarbe947082008-10-16 07:32:01 +0000566 // Fast path: Compute a conservative bound on the maximum number of
567 // bits per digit in this radix. If we can't possibly overflow a
568 // uint64 based on that bound then do the simple conversion to
569 // integer. This avoids the expensive overflow checking below, and
570 // handles the common cases that matter (small decimal integers and
571 // hex/octal values which don't overflow).
572 unsigned MaxBitsPerDigit = 1;
Mike Stump11289f42009-09-09 15:08:12 +0000573 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbarbe947082008-10-16 07:32:01 +0000574 MaxBitsPerDigit += 1;
575 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
576 uint64_t N = 0;
577 for (s = DigitsBegin; s != SuffixBegin; ++s)
578 N = N*radix + HexDigitValue(*s);
579
580 // This will truncate the value to Val's input width. Simply check
581 // for overflow by comparing.
582 Val = N;
583 return Val.getZExtValue() != N;
584 }
585
Chris Lattner5b743d32007-04-04 05:52:58 +0000586 Val = 0;
587 s = DigitsBegin;
588
Chris Lattner23b7eb62007-06-15 23:05:46 +0000589 llvm::APInt RadixVal(Val.getBitWidth(), radix);
590 llvm::APInt CharVal(Val.getBitWidth(), 0);
591 llvm::APInt OldVal = Val;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner871b4e12007-04-04 06:36:34 +0000593 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000594 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000595 unsigned C = HexDigitValue(*s++);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattner5b743d32007-04-04 05:52:58 +0000597 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000598 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner5b743d32007-04-04 05:52:58 +0000600 CharVal = C;
Mike Stump11289f42009-09-09 15:08:12 +0000601
Chris Lattner871b4e12007-04-04 06:36:34 +0000602 // Add the digit to the value in the appropriate radix. If adding in digits
603 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000604 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000605
606 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000607 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000608 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
609
Chris Lattner871b4e12007-04-04 06:36:34 +0000610 // Add value, did overflow occur on the value?
Daniel Dunbarb1f64422008-10-16 06:39:30 +0000611 // (a + b) ult b <=> overflow
Chris Lattner5b743d32007-04-04 05:52:58 +0000612 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000613 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000614 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000615 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000616}
617
John McCall53b93a02009-12-24 09:08:04 +0000618llvm::APFloat::opStatus
619NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000620 using llvm::APFloat;
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000621 using llvm::StringRef;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Erick Tryzelaarb9073112009-08-16 23:36:28 +0000623 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall53b93a02009-12-24 09:08:04 +0000624 return Result.convertFromString(StringRef(ThisTokBegin, n),
625 APFloat::rmNearestTiesToEven);
Steve Naroff97b9e912007-07-09 23:53:58 +0000626}
Chris Lattner5b743d32007-04-04 05:52:58 +0000627
Chris Lattner2f5add62007-04-05 06:57:15 +0000628
629CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
630 SourceLocation Loc, Preprocessor &PP) {
631 // At this point we know that the character matches the regex "L?'.*'".
632 HadError = false;
Mike Stump11289f42009-09-09 15:08:12 +0000633
Chris Lattner2f5add62007-04-05 06:57:15 +0000634 // Determine if this is a wide character.
635 IsWide = begin[0] == 'L';
636 if (IsWide) ++begin;
Mike Stump11289f42009-09-09 15:08:12 +0000637
Chris Lattner2f5add62007-04-05 06:57:15 +0000638 // Skip over the entry quote.
639 assert(begin[0] == '\'' && "Invalid token lexed");
640 ++begin;
641
Mike Stump11289f42009-09-09 15:08:12 +0000642 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000643 // upto 64-bits.
Chris Lattner2f5add62007-04-05 06:57:15 +0000644 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner37e05872008-03-05 18:54:05 +0000645 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Chris Lattner2f5add62007-04-05 06:57:15 +0000646 "Assumes char is 8 bits");
Chris Lattner8577f622009-04-28 21:51:46 +0000647 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
648 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
649 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
650 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
651 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000652
Mike Stump11289f42009-09-09 15:08:12 +0000653 // This is what we will use for overflow detection
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000654 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattner8577f622009-04-28 21:51:46 +0000656 unsigned NumCharsSoFar = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000657 while (begin[0] != '\'') {
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000658 uint64_t ResultChar;
Chris Lattner2f5add62007-04-05 06:57:15 +0000659 if (begin[0] != '\\') // If this is a normal character, consume it.
660 ResultChar = *begin++;
661 else // Otherwise, this is an escape character.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000662 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
Chris Lattner2f5add62007-04-05 06:57:15 +0000663
664 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
665 // implementation defined (C99 6.4.4.4p10).
Chris Lattner8577f622009-04-28 21:51:46 +0000666 if (NumCharsSoFar) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000667 if (IsWide) {
668 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000669 LitVal = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000670 } else {
671 // Narrow character literals act as though their value is concatenated
Chris Lattner8577f622009-04-28 21:51:46 +0000672 // in this implementation, but warn on overflow.
673 if (LitVal.countLeadingZeros() < 8)
Chris Lattner2f5add62007-04-05 06:57:15 +0000674 PP.Diag(Loc, diag::warn_char_constant_too_large);
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000675 LitVal <<= 8;
Chris Lattner2f5add62007-04-05 06:57:15 +0000676 }
677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000679 LitVal = LitVal + ResultChar;
Chris Lattner8577f622009-04-28 21:51:46 +0000680 ++NumCharsSoFar;
681 }
682
683 // If this is the second character being processed, do special handling.
684 if (NumCharsSoFar > 1) {
685 // Warn about discarding the top bits for multi-char wide-character
686 // constants (L'abcd').
687 if (IsWide)
688 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
689 else if (NumCharsSoFar != 4)
690 PP.Diag(Loc, diag::ext_multichar_character_literal);
691 else
692 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedmand8cec572009-06-01 05:25:02 +0000693 IsMultiChar = true;
Daniel Dunbara444cc22009-07-29 01:46:05 +0000694 } else
695 IsMultiChar = false;
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000696
697 // Transfer the value from APInt to uint64_t
698 Value = LitVal.getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000699
Chris Lattner2f5add62007-04-05 06:57:15 +0000700 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
701 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
702 // character constants are not sign extended in the this implementation:
703 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattner8577f622009-04-28 21:51:46 +0000704 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000705 PP.getLangOptions().CharIsSigned)
Chris Lattner2f5add62007-04-05 06:57:15 +0000706 Value = (signed char)Value;
707}
708
709
Steve Naroff4f88b312007-03-13 22:37:02 +0000710/// string-literal: [C99 6.4.5]
711/// " [s-char-sequence] "
712/// L" [s-char-sequence] "
713/// s-char-sequence:
714/// s-char
715/// s-char-sequence s-char
716/// s-char:
717/// any source character except the double quote ",
718/// backslash \, or newline character
719/// escape-character
720/// universal-character-name
721/// escape-character: [C99 6.4.4.4]
722/// \ escape-code
723/// universal-character-name
724/// escape-code:
725/// character-escape-code
726/// octal-escape-code
727/// hex-escape-code
728/// character-escape-code: one of
729/// n t b r f v a
730/// \ ' " ?
731/// octal-escape-code:
732/// octal-digit
733/// octal-digit octal-digit
734/// octal-digit octal-digit octal-digit
735/// hex-escape-code:
736/// x hex-digit
737/// hex-escape-code hex-digit
738/// universal-character-name:
739/// \u hex-quad
740/// \U hex-quad hex-quad
741/// hex-quad:
742/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000743///
Steve Naroff4f88b312007-03-13 22:37:02 +0000744StringLiteralParser::
Chris Lattner146762e2007-07-20 16:59:19 +0000745StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner8a24e582009-01-16 18:51:42 +0000746 Preprocessor &pp) : PP(pp) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000747 // Scan all of the string portions, remember the max individual token length,
748 // computing a bound on the concatenated string length, and see whether any
749 // piece is a wide-string. If any of the string portions is a wide-string
750 // literal, the result is a wide-string literal [C99 6.4.5p4].
751 MaxTokenLength = StringToks[0].getLength();
752 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000753 AnyWide = StringToks[0].is(tok::wide_string_literal);
Mike Stump11289f42009-09-09 15:08:12 +0000754
Steve Narofff1e53692007-03-23 22:27:02 +0000755 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000756
757 // Implement Translation Phase #6: concatenation of string literals
758 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000759 for (unsigned i = 1; i != NumStringToks; ++i) {
760 // The string could be shorter than this if it needs cleaning, but this is a
761 // reasonable bound, which is all we need.
762 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump11289f42009-09-09 15:08:12 +0000763
Steve Naroff4f88b312007-03-13 22:37:02 +0000764 // Remember maximum string piece length.
Mike Stump11289f42009-09-09 15:08:12 +0000765 if (StringToks[i].getLength() > MaxTokenLength)
Steve Naroff4f88b312007-03-13 22:37:02 +0000766 MaxTokenLength = StringToks[i].getLength();
Mike Stump11289f42009-09-09 15:08:12 +0000767
Steve Naroff4f88b312007-03-13 22:37:02 +0000768 // Remember if we see any wide strings.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000769 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Steve Naroff4f88b312007-03-13 22:37:02 +0000770 }
Chris Lattnerd42c29f2009-02-26 23:01:51 +0000771
Steve Naroff4f88b312007-03-13 22:37:02 +0000772 // Include space for the null terminator.
773 ++SizeBound;
Mike Stump11289f42009-09-09 15:08:12 +0000774
Steve Naroff4f88b312007-03-13 22:37:02 +0000775 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump11289f42009-09-09 15:08:12 +0000776
Steve Naroff4f88b312007-03-13 22:37:02 +0000777 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
778 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
779 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000780 if (AnyWide) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000781 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Chris Lattner2f5add62007-04-05 06:57:15 +0000782 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
783 wchar_tByteWidth /= 8;
784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Steve Naroff4f88b312007-03-13 22:37:02 +0000786 // The output buffer size needs to be large enough to hold wide characters.
787 // This is a worst-case assumption which basically corresponds to L"" "long".
788 if (AnyWide)
789 SizeBound *= wchar_tByteWidth;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Steve Naroff4f88b312007-03-13 22:37:02 +0000791 // Size the temporary buffer to hold the result string data.
792 ResultBuf.resize(SizeBound);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Steve Naroff4f88b312007-03-13 22:37:02 +0000794 // Likewise, but for each string piece.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000795 llvm::SmallString<512> TokenBuf;
Steve Naroff4f88b312007-03-13 22:37:02 +0000796 TokenBuf.resize(MaxTokenLength);
Mike Stump11289f42009-09-09 15:08:12 +0000797
Steve Naroff4f88b312007-03-13 22:37:02 +0000798 // Loop over all the strings, getting their spelling, and expanding them to
799 // wide strings as appropriate.
800 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump11289f42009-09-09 15:08:12 +0000801
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000802 Pascal = false;
Mike Stump11289f42009-09-09 15:08:12 +0000803
Steve Naroff4f88b312007-03-13 22:37:02 +0000804 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
805 const char *ThisTokBuf = &TokenBuf[0];
806 // Get the spelling of the token, which eliminates trigraphs, etc. We know
807 // that ThisTokBuf points to a buffer that is big enough for the whole token
808 // and 'spelled' tokens can only shrink.
809 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
810 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Mike Stump11289f42009-09-09 15:08:12 +0000811
Steve Naroff4f88b312007-03-13 22:37:02 +0000812 // TODO: Input character set mapping support.
Mike Stump11289f42009-09-09 15:08:12 +0000813
Steve Naroff4f88b312007-03-13 22:37:02 +0000814 // Skip L marker for wide strings.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000815 bool ThisIsWide = false;
816 if (ThisTokBuf[0] == 'L') {
817 ++ThisTokBuf;
818 ThisIsWide = true;
819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Steve Naroff4f88b312007-03-13 22:37:02 +0000821 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
822 ++ThisTokBuf;
Mike Stump11289f42009-09-09 15:08:12 +0000823
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000824 // Check if this is a pascal string
825 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
826 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump11289f42009-09-09 15:08:12 +0000827
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000828 // If the \p sequence is found in the first token, we have a pascal string
829 // Otherwise, if we already have a pascal string, ignore the first \p
830 if (i == 0) {
831 ++ThisTokBuf;
832 Pascal = true;
833 } else if (Pascal)
834 ThisTokBuf += 2;
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Steve Naroff4f88b312007-03-13 22:37:02 +0000837 while (ThisTokBuf != ThisTokEnd) {
838 // Is this a span of non-escape characters?
839 if (ThisTokBuf[0] != '\\') {
840 const char *InStart = ThisTokBuf;
841 do {
842 ++ThisTokBuf;
843 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump11289f42009-09-09 15:08:12 +0000844
Steve Naroff4f88b312007-03-13 22:37:02 +0000845 // Copy the character span over.
846 unsigned Len = ThisTokBuf-InStart;
847 if (!AnyWide) {
848 memcpy(ResultPtr, InStart, Len);
849 ResultPtr += Len;
850 } else {
851 // Note: our internal rep of wide char tokens is always little-endian.
852 for (; Len; --Len, ++InStart) {
853 *ResultPtr++ = InStart[0];
854 // Add zeros at the end.
855 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff7b753d22009-03-30 23:46:03 +0000856 *ResultPtr++ = 0;
Steve Naroff4f88b312007-03-13 22:37:02 +0000857 }
858 }
859 continue;
860 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000861 // Is this a Universal Character Name escape?
Steve Naroff7b753d22009-03-30 23:46:03 +0000862 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Mike Stump11289f42009-09-09 15:08:12 +0000863 ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Steve Narofff2a880c2009-03-31 10:29:45 +0000864 hadError, StringToks[i].getLocation(), ThisIsWide, PP);
Steve Naroffc94adda2009-04-01 11:09:15 +0000865 continue;
866 }
867 // Otherwise, this is a non-UCN escape character. Process it.
868 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
869 StringToks[i].getLocation(),
870 ThisIsWide, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000871
Steve Naroffc94adda2009-04-01 11:09:15 +0000872 // Note: our internal rep of wide char tokens is always little-endian.
873 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump11289f42009-09-09 15:08:12 +0000874
Steve Naroffc94adda2009-04-01 11:09:15 +0000875 if (AnyWide) {
876 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
877 *ResultPtr++ = ResultChar >> i*8;
Steve Naroff4f88b312007-03-13 22:37:02 +0000878 }
879 }
880 }
Mike Stump11289f42009-09-09 15:08:12 +0000881
Chris Lattner8a24e582009-01-16 18:51:42 +0000882 if (Pascal) {
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000883 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Chris Lattner8a24e582009-01-16 18:51:42 +0000884
885 // Verify that pascal strings aren't too large.
Eli Friedman1c3fb222009-04-01 03:17:08 +0000886 if (GetStringLength() > 256) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000887 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
888 << SourceRange(StringToks[0].getLocation(),
889 StringToks[NumStringToks-1].getLocation());
Eli Friedman1c3fb222009-04-01 03:17:08 +0000890 hadError = 1;
891 return;
892 }
Chris Lattner8a24e582009-01-16 18:51:42 +0000893 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000894}
Chris Lattnerddb71912009-02-18 19:21:10 +0000895
896
897/// getOffsetOfStringByte - This function returns the offset of the
898/// specified byte of the string data represented by Token. This handles
899/// advancing over escape sequences in the string.
900unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
901 unsigned ByteNo,
902 Preprocessor &PP) {
903 // Get the spelling of the token.
904 llvm::SmallString<16> SpellingBuffer;
905 SpellingBuffer.resize(Tok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattnerddb71912009-02-18 19:21:10 +0000907 const char *SpellingPtr = &SpellingBuffer[0];
908 unsigned TokLen = PP.getSpelling(Tok, SpellingPtr);
909
910 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
911
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattnerddb71912009-02-18 19:21:10 +0000913 const char *SpellingStart = SpellingPtr;
914 const char *SpellingEnd = SpellingPtr+TokLen;
915
916 // Skip over the leading quote.
917 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
918 ++SpellingPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattnerddb71912009-02-18 19:21:10 +0000920 // Skip over bytes until we find the offset we're looking for.
921 while (ByteNo) {
922 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattnerddb71912009-02-18 19:21:10 +0000924 // Step over non-escapes simply.
925 if (*SpellingPtr != '\\') {
926 ++SpellingPtr;
927 --ByteNo;
928 continue;
929 }
Mike Stump11289f42009-09-09 15:08:12 +0000930
Chris Lattnerddb71912009-02-18 19:21:10 +0000931 // Otherwise, this is an escape character. Advance over it.
932 bool HadError = false;
933 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
934 Tok.getLocation(), false, PP);
935 assert(!HadError && "This method isn't valid on erroneous strings");
936 --ByteNo;
937 }
Mike Stump11289f42009-09-09 15:08:12 +0000938
Chris Lattnerddb71912009-02-18 19:21:10 +0000939 return SpellingPtr-SpellingStart;
940}