blob: f8b92584575718c491e39e474b38e5c5921575e5 [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"
Steve Naroff4f88b312007-03-13 22:37:02 +000019#include "llvm/ADT/StringExtras.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000020using namespace clang;
21
Chris Lattner2f5add62007-04-05 06:57:15 +000022/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
23/// not valid.
24static int HexDigitValue(char C) {
25 if (C >= '0' && C <= '9') return C-'0';
26 if (C >= 'a' && C <= 'f') return C-'a'+10;
27 if (C >= 'A' && C <= 'F') return C-'A'+10;
28 return -1;
29}
30
31/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
32/// either a character or a string literal.
33static unsigned ProcessCharEscape(const char *&ThisTokBuf,
34 const char *ThisTokEnd, bool &HadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +000035 SourceLocation Loc, bool IsWide,
36 Preprocessor &PP) {
Chris Lattner2f5add62007-04-05 06:57:15 +000037 // Skip the '\' char.
38 ++ThisTokBuf;
39
40 // We know that this character can't be off the end of the buffer, because
41 // that would have been \", which would not have been the end of string.
42 unsigned ResultChar = *ThisTokBuf++;
43 switch (ResultChar) {
44 // These map to themselves.
45 case '\\': case '\'': case '"': case '?': break;
46
47 // These have fixed mappings.
48 case 'a':
49 // TODO: K&R: the meaning of '\\a' is different in traditional C
50 ResultChar = 7;
51 break;
52 case 'b':
53 ResultChar = 8;
54 break;
55 case 'e':
Chris Lattnere05c4df2008-11-18 21:48:13 +000056 PP.Diag(Loc, diag::ext_nonstandard_escape) << "e";
Chris Lattner2f5add62007-04-05 06:57:15 +000057 ResultChar = 27;
58 break;
Eli Friedman28a00aa2009-06-10 01:32:39 +000059 case 'E':
60 PP.Diag(Loc, diag::ext_nonstandard_escape) << "E";
61 ResultChar = 27;
62 break;
Chris Lattner2f5add62007-04-05 06:57:15 +000063 case 'f':
64 ResultChar = 12;
65 break;
66 case 'n':
67 ResultChar = 10;
68 break;
69 case 'r':
70 ResultChar = 13;
71 break;
72 case 't':
73 ResultChar = 9;
74 break;
75 case 'v':
76 ResultChar = 11;
77 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +000078 case 'x': { // Hex escape.
79 ResultChar = 0;
80 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner2f5add62007-04-05 06:57:15 +000081 PP.Diag(Loc, diag::err_hex_escape_no_digits);
82 HadError = 1;
Chris Lattner2f5add62007-04-05 06:57:15 +000083 break;
84 }
Chris Lattner2f5add62007-04-05 06:57:15 +000085
Chris Lattner812eda82007-05-20 05:17:04 +000086 // Hex escapes are a maximal series of hex digits.
Chris Lattnerc10adde2007-05-20 05:00:58 +000087 bool Overflow = false;
88 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
89 int CharVal = HexDigitValue(ThisTokBuf[0]);
90 if (CharVal == -1) break;
Chris Lattner59f09b62008-09-30 20:45:40 +000091 // About to shift out a digit?
92 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Chris Lattnerc10adde2007-05-20 05:00:58 +000093 ResultChar <<= 4;
94 ResultChar |= CharVal;
95 }
96
97 // See if any bits will be truncated when evaluated as a character.
Alisdair Meredithed28f6e2009-07-14 08:10:06 +000098 unsigned CharWidth = IsWide
99 ? PP.getTargetInfo().getWCharWidth()
100 : PP.getTargetInfo().getCharWidth();
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000101
Chris Lattnerc10adde2007-05-20 05:00:58 +0000102 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
103 Overflow = true;
104 ResultChar &= ~0U >> (32-CharWidth);
105 }
106
107 // Check for overflow.
108 if (Overflow) // Too many digits to fit in
109 PP.Diag(Loc, diag::warn_hex_escape_too_large);
Chris Lattner2f5add62007-04-05 06:57:15 +0000110 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000111 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000112 case '0': case '1': case '2': case '3':
Chris Lattner812eda82007-05-20 05:17:04 +0000113 case '4': case '5': case '6': case '7': {
Chris Lattner2f5add62007-04-05 06:57:15 +0000114 // Octal escapes.
Chris Lattner3f4b6e32007-06-09 06:20:47 +0000115 --ThisTokBuf;
Chris Lattner812eda82007-05-20 05:17:04 +0000116 ResultChar = 0;
117
118 // Octal escapes are a series of octal digits with maximum length 3.
119 // "\0123" is a two digit sequence equal to "\012" "3".
120 unsigned NumDigits = 0;
121 do {
122 ResultChar <<= 3;
123 ResultChar |= *ThisTokBuf++ - '0';
124 ++NumDigits;
125 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
126 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
127
128 // Check for overflow. Reject '\777', but not L'\777'.
Alisdair Meredithed28f6e2009-07-14 08:10:06 +0000129 unsigned CharWidth = IsWide
130 ? PP.getTargetInfo().getWCharWidth()
131 : PP.getTargetInfo().getCharWidth();
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000132
Chris Lattner812eda82007-05-20 05:17:04 +0000133 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
134 PP.Diag(Loc, diag::warn_octal_escape_too_large);
135 ResultChar &= ~0U >> (32-CharWidth);
136 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000137 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000138 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000139
140 // Otherwise, these are not valid escapes.
141 case '(': case '{': case '[': case '%':
142 // GCC accepts these as extensions. We warn about them as such though.
Eli Friedman5d72d412009-04-28 00:51:18 +0000143 PP.Diag(Loc, diag::ext_nonstandard_escape)
144 << std::string()+(char)ResultChar;
145 break;
Chris Lattner2f5add62007-04-05 06:57:15 +0000146 default:
Chris Lattner59acca52008-11-22 07:23:31 +0000147 if (isgraph(ThisTokBuf[0]))
Chris Lattnere05c4df2008-11-18 21:48:13 +0000148 PP.Diag(Loc, diag::ext_unknown_escape) << std::string()+(char)ResultChar;
Chris Lattner59acca52008-11-22 07:23:31 +0000149 else
Chris Lattnere05c4df2008-11-18 21:48:13 +0000150 PP.Diag(Loc, diag::ext_unknown_escape) << "x"+llvm::utohexstr(ResultChar);
Chris Lattner2f5add62007-04-05 06:57:15 +0000151 break;
152 }
153
154 return ResultChar;
155}
156
Steve Naroff7b753d22009-03-30 23:46:03 +0000157/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
158/// convert the UTF32 to UTF8. This is a subroutine of StringLiteralParser.
159/// When we decide to implement UCN's for character constants and identifiers,
160/// we will likely rework our support for UCN's.
161static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
Steve Narofff2a880c2009-03-31 10:29:45 +0000162 char *&ResultBuf, bool &HadError,
163 SourceLocation Loc, bool IsWide, Preprocessor &PP)
164{
Steve Naroff7b753d22009-03-30 23:46:03 +0000165 // FIXME: Add a warning - UCN's are only valid in C++ & C99.
Steve Narofff2a880c2009-03-31 10:29:45 +0000166 // FIXME: Handle wide strings.
Steve Naroff7b753d22009-03-30 23:46:03 +0000167
Steve Naroffc94adda2009-04-01 11:09:15 +0000168 // Save the beginning of the string (for error diagnostics).
169 const char *ThisTokBegin = ThisTokBuf;
170
Steve Naroff7b753d22009-03-30 23:46:03 +0000171 // Skip the '\u' char's.
172 ThisTokBuf += 2;
Chris Lattner2f5add62007-04-05 06:57:15 +0000173
Steve Naroff7b753d22009-03-30 23:46:03 +0000174 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
175 PP.Diag(Loc, diag::err_ucn_escape_no_digits);
176 HadError = 1;
177 return;
178 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000179 typedef uint32_t UTF32;
Steve Naroff7b753d22009-03-30 23:46:03 +0000180
181 UTF32 UcnVal = 0;
182 unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
183 for (; ThisTokBuf != ThisTokEnd && UcnLen; ++ThisTokBuf, UcnLen--) {
184 int CharVal = HexDigitValue(ThisTokBuf[0]);
185 if (CharVal == -1) break;
186 UcnVal <<= 4;
187 UcnVal |= CharVal;
188 }
189 // If we didn't consume the proper number of digits, there is a problem.
190 if (UcnLen) {
Steve Naroffc94adda2009-04-01 11:09:15 +0000191 PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
192 diag::err_ucn_escape_incomplete);
Steve Naroff7b753d22009-03-30 23:46:03 +0000193 HadError = 1;
194 return;
195 }
Steve Narofff2a880c2009-03-31 10:29:45 +0000196 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff7b753d22009-03-30 23:46:03 +0000197 if ((UcnVal < 0xa0 &&
198 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Steve Narofff2a880c2009-03-31 10:29:45 +0000199 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
200 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Steve Naroff7b753d22009-03-30 23:46:03 +0000201 PP.Diag(Loc, diag::err_ucn_escape_invalid);
202 HadError = 1;
203 return;
204 }
205 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
206 // The conversion below was inspired by:
207 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
208 // First, we determine how many bytes the result will require.
Steve Naroffc94adda2009-04-01 11:09:15 +0000209 typedef uint8_t UTF8;
Steve Naroff7b753d22009-03-30 23:46:03 +0000210
211 unsigned short bytesToWrite = 0;
212 if (UcnVal < (UTF32)0x80)
213 bytesToWrite = 1;
214 else if (UcnVal < (UTF32)0x800)
215 bytesToWrite = 2;
216 else if (UcnVal < (UTF32)0x10000)
217 bytesToWrite = 3;
218 else
219 bytesToWrite = 4;
220
Steve Naroff7b753d22009-03-30 23:46:03 +0000221 const unsigned byteMask = 0xBF;
222 const unsigned byteMark = 0x80;
223
224 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Narofff2a880c2009-03-31 10:29:45 +0000225 // into the first byte, depending on how many bytes follow.
226 static const UTF8 firstByteMark[5] = {
227 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff7b753d22009-03-30 23:46:03 +0000228 };
229 // Finally, we write the bytes into ResultBuf.
230 ResultBuf += bytesToWrite;
231 switch (bytesToWrite) { // note: everything falls through.
232 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
233 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
234 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
235 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
236 }
237 // Update the buffer.
238 ResultBuf += bytesToWrite;
239}
Chris Lattner2f5add62007-04-05 06:57:15 +0000240
241
Steve Naroff09ef4742007-03-09 23:16:33 +0000242/// integer-constant: [C99 6.4.4.1]
243/// decimal-constant integer-suffix
244/// octal-constant integer-suffix
245/// hexadecimal-constant integer-suffix
246/// decimal-constant:
247/// nonzero-digit
248/// decimal-constant digit
249/// octal-constant:
250/// 0
251/// octal-constant octal-digit
252/// hexadecimal-constant:
253/// hexadecimal-prefix hexadecimal-digit
254/// hexadecimal-constant hexadecimal-digit
255/// hexadecimal-prefix: one of
256/// 0x 0X
257/// integer-suffix:
258/// unsigned-suffix [long-suffix]
259/// unsigned-suffix [long-long-suffix]
260/// long-suffix [unsigned-suffix]
261/// long-long-suffix [unsigned-sufix]
262/// nonzero-digit:
263/// 1 2 3 4 5 6 7 8 9
264/// octal-digit:
265/// 0 1 2 3 4 5 6 7
266/// hexadecimal-digit:
267/// 0 1 2 3 4 5 6 7 8 9
268/// a b c d e f
269/// A B C D E F
270/// unsigned-suffix: one of
271/// u U
272/// long-suffix: one of
273/// l L
274/// long-long-suffix: one of
275/// ll LL
276///
277/// floating-constant: [C99 6.4.4.2]
278/// TODO: add rules...
279///
Steve Naroff09ef4742007-03-09 23:16:33 +0000280NumericLiteralParser::
281NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000282 SourceLocation TokLoc, Preprocessor &pp)
283 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Chris Lattner59f09b62008-09-30 20:45:40 +0000284
285 // This routine assumes that the range begin/end matches the regex for integer
286 // and FP constants (specifically, the 'pp-number' regex), and assumes that
287 // the byte at "*end" is both valid and not part of the regex. Because of
288 // this, it doesn't have to check for 'overscan' in various places.
289 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
290 "Lexer didn't maximally munch?");
291
Steve Naroff09ef4742007-03-09 23:16:33 +0000292 s = DigitsBegin = begin;
293 saw_exponent = false;
294 saw_period = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000295 isLong = false;
296 isUnsigned = false;
297 isLongLong = false;
Chris Lattnered045422007-08-26 03:29:23 +0000298 isFloat = false;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000299 isImaginary = false;
Steve Naroff09ef4742007-03-09 23:16:33 +0000300 hadError = false;
301
302 if (*s == '0') { // parse radix
Chris Lattner6016a512008-06-30 06:39:54 +0000303 ParseNumberStartingWithZero(TokLoc);
304 if (hadError)
305 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000306 } else { // the first digit is non-zero
307 radix = 10;
308 s = SkipDigits(s);
309 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000310 // Done.
Christopher Lamb42e69f22007-11-29 06:06:27 +0000311 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattner59acca52008-11-22 07:23:31 +0000312 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
313 diag::err_invalid_decimal_digit) << std::string(s, s+1);
314 hadError = true;
Chris Lattner328fa5c2007-06-08 17:12:06 +0000315 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000316 } else if (*s == '.') {
317 s++;
318 saw_period = true;
319 s = SkipDigits(s);
320 }
Chris Lattnerfb8b8f22008-09-29 23:12:31 +0000321 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner4885b972008-04-20 18:47:55 +0000322 const char *Exponent = s;
Steve Naroff09ef4742007-03-09 23:16:33 +0000323 s++;
324 saw_exponent = true;
325 if (*s == '+' || *s == '-') s++; // sign
326 const char *first_non_digit = SkipDigits(s);
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000327 if (first_non_digit != s) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000328 s = first_non_digit;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000329 } else {
Chris Lattner59acca52008-11-22 07:23:31 +0000330 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
331 diag::err_exponent_has_no_digits);
332 hadError = true;
Chris Lattner48a9b9b2008-04-20 18:41:46 +0000333 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000334 }
335 }
336 }
337
338 SuffixBegin = s;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000339
340 // Parse the suffix. At this point we can classify whether we have an FP or
341 // integer constant.
342 bool isFPConstant = isFloatingLiteral();
343
344 // Loop over all of the characters of the suffix. If we see something bad,
345 // we break out of the loop.
346 for (; s != ThisTokEnd; ++s) {
347 switch (*s) {
348 case 'f': // FP Suffix for "float"
349 case 'F':
350 if (!isFPConstant) break; // Error for integer constant.
Chris Lattnered045422007-08-26 03:29:23 +0000351 if (isFloat || isLong) break; // FF, LF invalid.
352 isFloat = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000353 continue; // Success.
354 case 'u':
355 case 'U':
356 if (isFPConstant) break; // Error for floating constant.
357 if (isUnsigned) break; // Cannot be repeated.
358 isUnsigned = true;
359 continue; // Success.
360 case 'l':
361 case 'L':
362 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattnered045422007-08-26 03:29:23 +0000363 if (isFloat) break; // LF invalid.
Chris Lattnerf55ab182007-08-26 01:58:14 +0000364
365 // Check for long long. The L's need to be adjacent and the same case.
366 if (s+1 != ThisTokEnd && s[1] == s[0]) {
367 if (isFPConstant) break; // long long invalid for floats.
368 isLongLong = true;
369 ++s; // Eat both of them.
370 } else {
Steve Naroff09ef4742007-03-09 23:16:33 +0000371 isLong = true;
Steve Naroff09ef4742007-03-09 23:16:33 +0000372 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000373 continue; // Success.
374 case 'i':
Steve Naroffa1f41452008-04-04 21:02:54 +0000375 if (PP.getLangOptions().Microsoft) {
376 // Allow i8, i16, i32, i64, and i128.
377 if (++s == ThisTokEnd) break;
378 switch (*s) {
379 case '8':
380 s++; // i8 suffix
381 break;
382 case '1':
383 if (++s == ThisTokEnd) break;
384 if (*s == '6') s++; // i16 suffix
385 else if (*s == '2') {
386 if (++s == ThisTokEnd) break;
387 if (*s == '8') s++; // i128 suffix
388 }
389 break;
390 case '3':
391 if (++s == ThisTokEnd) break;
392 if (*s == '2') s++; // i32 suffix
393 break;
394 case '6':
395 if (++s == ThisTokEnd) break;
396 if (*s == '4') s++; // i64 suffix
397 break;
398 default:
399 break;
400 }
401 break;
402 }
403 // fall through.
Chris Lattnerf55ab182007-08-26 01:58:14 +0000404 case 'I':
405 case 'j':
406 case 'J':
407 if (isImaginary) break; // Cannot be repeated.
408 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
409 diag::ext_imaginary_constant);
410 isImaginary = true;
411 continue; // Success.
Steve Naroff09ef4742007-03-09 23:16:33 +0000412 }
Chris Lattnerf55ab182007-08-26 01:58:14 +0000413 // If we reached here, there was an error.
414 break;
415 }
416
417 // Report an error if there are any.
418 if (s != ThisTokEnd) {
Chris Lattner59acca52008-11-22 07:23:31 +0000419 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
420 isFPConstant ? diag::err_invalid_suffix_float_constant :
421 diag::err_invalid_suffix_integer_constant)
422 << std::string(SuffixBegin, ThisTokEnd);
423 hadError = true;
Chris Lattnerf55ab182007-08-26 01:58:14 +0000424 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000425 }
426}
427
Chris Lattner6016a512008-06-30 06:39:54 +0000428/// ParseNumberStartingWithZero - This method is called when the first character
429/// of the number is found to be a zero. This means it is either an octal
430/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
431/// a floating point number (01239.123e4). Eat the prefix, determining the
432/// radix etc.
433void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
434 assert(s[0] == '0' && "Invalid method call");
435 s++;
436
437 // Handle a hex number like 0x1234.
438 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
439 s++;
440 radix = 16;
441 DigitsBegin = s;
442 s = SkipHexDigits(s);
443 if (s == ThisTokEnd) {
444 // Done.
445 } else if (*s == '.') {
446 s++;
447 saw_period = true;
448 s = SkipHexDigits(s);
449 }
450 // A binary exponent can appear with or with a '.'. If dotted, the
451 // binary exponent is required.
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000452 if (*s == 'p' || *s == 'P') {
Chris Lattner6016a512008-06-30 06:39:54 +0000453 const char *Exponent = s;
454 s++;
455 saw_exponent = true;
456 if (*s == '+' || *s == '-') s++; // sign
457 const char *first_non_digit = SkipDigits(s);
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000458 if (first_non_digit == s) {
Chris Lattner59acca52008-11-22 07:23:31 +0000459 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
460 diag::err_exponent_has_no_digits);
461 hadError = true;
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000462 return;
Chris Lattner6016a512008-06-30 06:39:54 +0000463 }
Chris Lattnerc94ad4a2008-07-25 18:18:34 +0000464 s = first_non_digit;
465
Chris Lattnerf3cb3942008-11-22 07:39:03 +0000466 if (!PP.getLangOptions().HexFloats)
Chris Lattner59acca52008-11-22 07:23:31 +0000467 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner6016a512008-06-30 06:39:54 +0000468 } else if (saw_period) {
Chris Lattner59acca52008-11-22 07:23:31 +0000469 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
470 diag::err_hexconstant_requires_exponent);
471 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000472 }
473 return;
474 }
475
476 // Handle simple binary numbers 0b01010
477 if (*s == 'b' || *s == 'B') {
478 // 0b101010 is a GCC extension.
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000479 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner6016a512008-06-30 06:39:54 +0000480 ++s;
481 radix = 2;
482 DigitsBegin = s;
483 s = SkipBinaryDigits(s);
484 if (s == ThisTokEnd) {
485 // Done.
486 } else if (isxdigit(*s)) {
Chris Lattner59acca52008-11-22 07:23:31 +0000487 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
488 diag::err_invalid_binary_digit) << std::string(s, s+1);
489 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000490 }
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000491 // Other suffixes will be diagnosed by the caller.
Chris Lattner6016a512008-06-30 06:39:54 +0000492 return;
493 }
494
495 // For now, the radix is set to 8. If we discover that we have a
496 // floating point constant, the radix will change to 10. Octal floating
497 // point constants are not permitted (only decimal and hexadecimal).
498 radix = 8;
499 DigitsBegin = s;
500 s = SkipOctalDigits(s);
501 if (s == ThisTokEnd)
502 return; // Done, simple octal number like 01234
503
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000504 // If we have some other non-octal digit that *is* a decimal digit, see if
505 // this is part of a floating point number like 094.123 or 09e1.
506 if (isdigit(*s)) {
507 const char *EndDecimal = SkipDigits(s);
508 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
509 s = EndDecimal;
510 radix = 10;
511 }
512 }
513
514 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
515 // the code is using an incorrect base.
Chris Lattner6016a512008-06-30 06:39:54 +0000516 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattner59acca52008-11-22 07:23:31 +0000517 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
518 diag::err_invalid_octal_digit) << std::string(s, s+1);
519 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000520 return;
521 }
522
523 if (*s == '.') {
524 s++;
525 radix = 10;
526 saw_period = true;
Chris Lattnerd68c04f2008-06-30 06:44:49 +0000527 s = SkipDigits(s); // Skip suffix.
Chris Lattner6016a512008-06-30 06:39:54 +0000528 }
529 if (*s == 'e' || *s == 'E') { // exponent
530 const char *Exponent = s;
531 s++;
532 radix = 10;
533 saw_exponent = true;
534 if (*s == '+' || *s == '-') s++; // sign
535 const char *first_non_digit = SkipDigits(s);
536 if (first_non_digit != s) {
537 s = first_non_digit;
538 } else {
Chris Lattner59acca52008-11-22 07:23:31 +0000539 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
540 diag::err_exponent_has_no_digits);
541 hadError = true;
Chris Lattner6016a512008-06-30 06:39:54 +0000542 return;
543 }
544 }
545}
546
547
Chris Lattner5b743d32007-04-04 05:52:58 +0000548/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000549/// matches Val's input width. If there is an overflow, set Val to the low bits
550/// of the result and return true. Otherwise, return false.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000551bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbarbe947082008-10-16 07:32:01 +0000552 // Fast path: Compute a conservative bound on the maximum number of
553 // bits per digit in this radix. If we can't possibly overflow a
554 // uint64 based on that bound then do the simple conversion to
555 // integer. This avoids the expensive overflow checking below, and
556 // handles the common cases that matter (small decimal integers and
557 // hex/octal values which don't overflow).
558 unsigned MaxBitsPerDigit = 1;
559 while ((1U << MaxBitsPerDigit) < radix)
560 MaxBitsPerDigit += 1;
561 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
562 uint64_t N = 0;
563 for (s = DigitsBegin; s != SuffixBegin; ++s)
564 N = N*radix + HexDigitValue(*s);
565
566 // This will truncate the value to Val's input width. Simply check
567 // for overflow by comparing.
568 Val = N;
569 return Val.getZExtValue() != N;
570 }
571
Chris Lattner5b743d32007-04-04 05:52:58 +0000572 Val = 0;
573 s = DigitsBegin;
574
Chris Lattner23b7eb62007-06-15 23:05:46 +0000575 llvm::APInt RadixVal(Val.getBitWidth(), radix);
576 llvm::APInt CharVal(Val.getBitWidth(), 0);
577 llvm::APInt OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000578
579 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000580 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000581 unsigned C = HexDigitValue(*s++);
Chris Lattner5b743d32007-04-04 05:52:58 +0000582
583 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000584 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Chris Lattner5b743d32007-04-04 05:52:58 +0000585
586 CharVal = C;
587
Chris Lattner871b4e12007-04-04 06:36:34 +0000588 // Add the digit to the value in the appropriate radix. If adding in digits
589 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000590 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000591
592 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000593 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000594 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
595
Chris Lattner871b4e12007-04-04 06:36:34 +0000596 // Add value, did overflow occur on the value?
Daniel Dunbarb1f64422008-10-16 06:39:30 +0000597 // (a + b) ult b <=> overflow
Chris Lattner5b743d32007-04-04 05:52:58 +0000598 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000599 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000600 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000601 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000602}
603
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000604llvm::APFloat NumericLiteralParser::
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000605GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) {
606 using llvm::APFloat;
607
Ted Kremenek9924ca22007-11-29 00:54:29 +0000608 llvm::SmallVector<char,256> floatChars;
609 for (unsigned i = 0, n = ThisTokEnd-ThisTokBegin; i != n; ++i)
610 floatChars.push_back(ThisTokBegin[i]);
611
612 floatChars.push_back('\0');
613
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000614 APFloat V (Format, APFloat::fcZero, false);
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000615 APFloat::opStatus status;
Ted Kremenek9924ca22007-11-29 00:54:29 +0000616
617 status = V.convertFromString(&floatChars[0],APFloat::rmNearestTiesToEven);
Ted Kremenekfbb08bc2007-11-26 23:12:30 +0000618
619 if (isExact)
620 *isExact = status == APFloat::opOK;
621
622 return V;
Steve Naroff97b9e912007-07-09 23:53:58 +0000623}
Chris Lattner5b743d32007-04-04 05:52:58 +0000624
Chris Lattner2f5add62007-04-05 06:57:15 +0000625
626CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
627 SourceLocation Loc, Preprocessor &PP) {
628 // At this point we know that the character matches the regex "L?'.*'".
629 HadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000630
631 // Determine if this is a wide character.
632 IsWide = begin[0] == 'L';
633 if (IsWide) ++begin;
634
635 // Skip over the entry quote.
636 assert(begin[0] == '\'' && "Invalid token lexed");
637 ++begin;
638
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000639 // FIXME: The "Value" is an uint64_t so we can handle char literals of
640 // upto 64-bits.
Chris Lattner2f5add62007-04-05 06:57:15 +0000641 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner37e05872008-03-05 18:54:05 +0000642 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Chris Lattner2f5add62007-04-05 06:57:15 +0000643 "Assumes char is 8 bits");
Chris Lattner8577f622009-04-28 21:51:46 +0000644 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
645 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
646 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
647 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
648 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000649
650 // This is what we will use for overflow detection
651 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Chris Lattner2f5add62007-04-05 06:57:15 +0000652
Chris Lattner8577f622009-04-28 21:51:46 +0000653 unsigned NumCharsSoFar = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000654 while (begin[0] != '\'') {
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000655 uint64_t ResultChar;
Chris Lattner2f5add62007-04-05 06:57:15 +0000656 if (begin[0] != '\\') // If this is a normal character, consume it.
657 ResultChar = *begin++;
658 else // Otherwise, this is an escape character.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000659 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
Chris Lattner2f5add62007-04-05 06:57:15 +0000660
661 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
662 // implementation defined (C99 6.4.4.4p10).
Chris Lattner8577f622009-04-28 21:51:46 +0000663 if (NumCharsSoFar) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000664 if (IsWide) {
665 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000666 LitVal = 0;
Chris Lattner2f5add62007-04-05 06:57:15 +0000667 } else {
668 // Narrow character literals act as though their value is concatenated
Chris Lattner8577f622009-04-28 21:51:46 +0000669 // in this implementation, but warn on overflow.
670 if (LitVal.countLeadingZeros() < 8)
Chris Lattner2f5add62007-04-05 06:57:15 +0000671 PP.Diag(Loc, diag::warn_char_constant_too_large);
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000672 LitVal <<= 8;
Chris Lattner2f5add62007-04-05 06:57:15 +0000673 }
674 }
675
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000676 LitVal = LitVal + ResultChar;
Chris Lattner8577f622009-04-28 21:51:46 +0000677 ++NumCharsSoFar;
678 }
679
680 // If this is the second character being processed, do special handling.
681 if (NumCharsSoFar > 1) {
682 // Warn about discarding the top bits for multi-char wide-character
683 // constants (L'abcd').
684 if (IsWide)
685 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
686 else if (NumCharsSoFar != 4)
687 PP.Diag(Loc, diag::ext_multichar_character_literal);
688 else
689 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedmand8cec572009-06-01 05:25:02 +0000690 IsMultiChar = true;
Chris Lattner2f5add62007-04-05 06:57:15 +0000691 }
Sanjiv Guptaf09cb952009-04-21 02:21:29 +0000692
693 // Transfer the value from APInt to uint64_t
694 Value = LitVal.getZExtValue();
Chris Lattner2f5add62007-04-05 06:57:15 +0000695
696 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
697 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
698 // character constants are not sign extended in the this implementation:
699 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattner8577f622009-04-28 21:51:46 +0000700 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000701 PP.getLangOptions().CharIsSigned)
Chris Lattner2f5add62007-04-05 06:57:15 +0000702 Value = (signed char)Value;
703}
704
705
Steve Naroff4f88b312007-03-13 22:37:02 +0000706/// string-literal: [C99 6.4.5]
707/// " [s-char-sequence] "
708/// L" [s-char-sequence] "
709/// s-char-sequence:
710/// s-char
711/// s-char-sequence s-char
712/// s-char:
713/// any source character except the double quote ",
714/// backslash \, or newline character
715/// escape-character
716/// universal-character-name
717/// escape-character: [C99 6.4.4.4]
718/// \ escape-code
719/// universal-character-name
720/// escape-code:
721/// character-escape-code
722/// octal-escape-code
723/// hex-escape-code
724/// character-escape-code: one of
725/// n t b r f v a
726/// \ ' " ?
727/// octal-escape-code:
728/// octal-digit
729/// octal-digit octal-digit
730/// octal-digit octal-digit octal-digit
731/// hex-escape-code:
732/// x hex-digit
733/// hex-escape-code hex-digit
734/// universal-character-name:
735/// \u hex-quad
736/// \U hex-quad hex-quad
737/// hex-quad:
738/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000739///
Steve Naroff4f88b312007-03-13 22:37:02 +0000740StringLiteralParser::
Chris Lattner146762e2007-07-20 16:59:19 +0000741StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner8a24e582009-01-16 18:51:42 +0000742 Preprocessor &pp) : PP(pp) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000743 // Scan all of the string portions, remember the max individual token length,
744 // computing a bound on the concatenated string length, and see whether any
745 // piece is a wide-string. If any of the string portions is a wide-string
746 // literal, the result is a wide-string literal [C99 6.4.5p4].
747 MaxTokenLength = StringToks[0].getLength();
748 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000749 AnyWide = StringToks[0].is(tok::wide_string_literal);
Steve Naroff4f88b312007-03-13 22:37:02 +0000750
Steve Narofff1e53692007-03-23 22:27:02 +0000751 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000752
753 // Implement Translation Phase #6: concatenation of string literals
754 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000755 for (unsigned i = 1; i != NumStringToks; ++i) {
756 // The string could be shorter than this if it needs cleaning, but this is a
757 // reasonable bound, which is all we need.
758 SizeBound += StringToks[i].getLength()-2; // -2 for "".
759
760 // Remember maximum string piece length.
761 if (StringToks[i].getLength() > MaxTokenLength)
762 MaxTokenLength = StringToks[i].getLength();
763
764 // Remember if we see any wide strings.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000765 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Steve Naroff4f88b312007-03-13 22:37:02 +0000766 }
Chris Lattnerd42c29f2009-02-26 23:01:51 +0000767
Steve Naroff4f88b312007-03-13 22:37:02 +0000768 // Include space for the null terminator.
769 ++SizeBound;
770
771 // TODO: K&R warning: "traditional C rejects string constant concatenation"
772
773 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
774 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
775 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000776 if (AnyWide) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000777 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Chris Lattner2f5add62007-04-05 06:57:15 +0000778 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
779 wchar_tByteWidth /= 8;
780 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000781
782 // The output buffer size needs to be large enough to hold wide characters.
783 // This is a worst-case assumption which basically corresponds to L"" "long".
784 if (AnyWide)
785 SizeBound *= wchar_tByteWidth;
786
787 // Size the temporary buffer to hold the result string data.
788 ResultBuf.resize(SizeBound);
789
790 // Likewise, but for each string piece.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000791 llvm::SmallString<512> TokenBuf;
Steve Naroff4f88b312007-03-13 22:37:02 +0000792 TokenBuf.resize(MaxTokenLength);
793
794 // Loop over all the strings, getting their spelling, and expanding them to
795 // wide strings as appropriate.
796 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
797
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000798 Pascal = false;
799
Steve Naroff4f88b312007-03-13 22:37:02 +0000800 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
801 const char *ThisTokBuf = &TokenBuf[0];
802 // Get the spelling of the token, which eliminates trigraphs, etc. We know
803 // that ThisTokBuf points to a buffer that is big enough for the whole token
804 // and 'spelled' tokens can only shrink.
805 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
806 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
807
808 // TODO: Input character set mapping support.
809
810 // Skip L marker for wide strings.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000811 bool ThisIsWide = false;
812 if (ThisTokBuf[0] == 'L') {
813 ++ThisTokBuf;
814 ThisIsWide = true;
815 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000816
817 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
818 ++ThisTokBuf;
819
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000820 // Check if this is a pascal string
821 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
822 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
823
824 // If the \p sequence is found in the first token, we have a pascal string
825 // Otherwise, if we already have a pascal string, ignore the first \p
826 if (i == 0) {
827 ++ThisTokBuf;
828 Pascal = true;
829 } else if (Pascal)
830 ThisTokBuf += 2;
831 }
832
Steve Naroff4f88b312007-03-13 22:37:02 +0000833 while (ThisTokBuf != ThisTokEnd) {
834 // Is this a span of non-escape characters?
835 if (ThisTokBuf[0] != '\\') {
836 const char *InStart = ThisTokBuf;
837 do {
838 ++ThisTokBuf;
839 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
840
841 // Copy the character span over.
842 unsigned Len = ThisTokBuf-InStart;
843 if (!AnyWide) {
844 memcpy(ResultPtr, InStart, Len);
845 ResultPtr += Len;
846 } else {
847 // Note: our internal rep of wide char tokens is always little-endian.
848 for (; Len; --Len, ++InStart) {
849 *ResultPtr++ = InStart[0];
850 // Add zeros at the end.
851 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff7b753d22009-03-30 23:46:03 +0000852 *ResultPtr++ = 0;
Steve Naroff4f88b312007-03-13 22:37:02 +0000853 }
854 }
855 continue;
856 }
Steve Naroffc94adda2009-04-01 11:09:15 +0000857 // Is this a Universal Character Name escape?
Steve Naroff7b753d22009-03-30 23:46:03 +0000858 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
859 ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Steve Narofff2a880c2009-03-31 10:29:45 +0000860 hadError, StringToks[i].getLocation(), ThisIsWide, PP);
Steve Naroffc94adda2009-04-01 11:09:15 +0000861 continue;
862 }
863 // Otherwise, this is a non-UCN escape character. Process it.
864 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
865 StringToks[i].getLocation(),
866 ThisIsWide, PP);
867
868 // Note: our internal rep of wide char tokens is always little-endian.
869 *ResultPtr++ = ResultChar & 0xFF;
870
871 if (AnyWide) {
872 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
873 *ResultPtr++ = ResultChar >> i*8;
Steve Naroff4f88b312007-03-13 22:37:02 +0000874 }
875 }
876 }
877
Chris Lattner8a24e582009-01-16 18:51:42 +0000878 if (Pascal) {
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000879 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Chris Lattner8a24e582009-01-16 18:51:42 +0000880
881 // Verify that pascal strings aren't too large.
Eli Friedman1c3fb222009-04-01 03:17:08 +0000882 if (GetStringLength() > 256) {
Chris Lattner8a24e582009-01-16 18:51:42 +0000883 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
884 << SourceRange(StringToks[0].getLocation(),
885 StringToks[NumStringToks-1].getLocation());
Eli Friedman1c3fb222009-04-01 03:17:08 +0000886 hadError = 1;
887 return;
888 }
Chris Lattner8a24e582009-01-16 18:51:42 +0000889 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000890}
Chris Lattnerddb71912009-02-18 19:21:10 +0000891
892
893/// getOffsetOfStringByte - This function returns the offset of the
894/// specified byte of the string data represented by Token. This handles
895/// advancing over escape sequences in the string.
896unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
897 unsigned ByteNo,
898 Preprocessor &PP) {
899 // Get the spelling of the token.
900 llvm::SmallString<16> SpellingBuffer;
901 SpellingBuffer.resize(Tok.getLength());
902
903 const char *SpellingPtr = &SpellingBuffer[0];
904 unsigned TokLen = PP.getSpelling(Tok, SpellingPtr);
905
906 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
907
908
909 const char *SpellingStart = SpellingPtr;
910 const char *SpellingEnd = SpellingPtr+TokLen;
911
912 // Skip over the leading quote.
913 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
914 ++SpellingPtr;
915
916 // Skip over bytes until we find the offset we're looking for.
917 while (ByteNo) {
918 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
919
920 // Step over non-escapes simply.
921 if (*SpellingPtr != '\\') {
922 ++SpellingPtr;
923 --ByteNo;
924 continue;
925 }
926
927 // Otherwise, this is an escape character. Advance over it.
928 bool HadError = false;
929 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
930 Tok.getLocation(), false, PP);
931 assert(!HadError && "This method isn't valid on erroneous strings");
932 --ByteNo;
933 }
934
935 return SpellingPtr-SpellingStart;
936}