blob: 9815f9b91e7b9015f620250c77c2453ba3fe04ba [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/StringExtras.h"
20using namespace clang;
21
22/// 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,
35 SourceLocation Loc, bool IsWide,
36 Preprocessor &PP) {
37 // 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 Lattner204b2fe2008-11-18 21:48:13 +000056 PP.Diag(Loc, diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +000057 ResultChar = 27;
58 break;
59 case 'f':
60 ResultChar = 12;
61 break;
62 case 'n':
63 ResultChar = 10;
64 break;
65 case 'r':
66 ResultChar = 13;
67 break;
68 case 't':
69 ResultChar = 9;
70 break;
71 case 'v':
72 ResultChar = 11;
73 break;
74
75 //case 'u': case 'U': // FIXME: UCNs.
76 case 'x': { // Hex escape.
77 ResultChar = 0;
78 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
79 PP.Diag(Loc, diag::err_hex_escape_no_digits);
80 HadError = 1;
81 break;
82 }
83
84 // Hex escapes are a maximal series of hex digits.
85 bool Overflow = false;
86 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
87 int CharVal = HexDigitValue(ThisTokBuf[0]);
88 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +000089 // About to shift out a digit?
90 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ResultChar <<= 4;
92 ResultChar |= CharVal;
93 }
94
95 // See if any bits will be truncated when evaluated as a character.
Chris Lattner98be4942008-03-05 18:54:05 +000096 unsigned CharWidth = PP.getTargetInfo().getCharWidth(IsWide);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000097
Reid Spencer5f016e22007-07-11 17:01:13 +000098 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
99 Overflow = true;
100 ResultChar &= ~0U >> (32-CharWidth);
101 }
102
103 // Check for overflow.
104 if (Overflow) // Too many digits to fit in
105 PP.Diag(Loc, diag::warn_hex_escape_too_large);
106 break;
107 }
108 case '0': case '1': case '2': case '3':
109 case '4': case '5': case '6': case '7': {
110 // Octal escapes.
111 --ThisTokBuf;
112 ResultChar = 0;
113
114 // Octal escapes are a series of octal digits with maximum length 3.
115 // "\0123" is a two digit sequence equal to "\012" "3".
116 unsigned NumDigits = 0;
117 do {
118 ResultChar <<= 3;
119 ResultChar |= *ThisTokBuf++ - '0';
120 ++NumDigits;
121 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
122 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
123
124 // Check for overflow. Reject '\777', but not L'\777'.
Chris Lattner98be4942008-03-05 18:54:05 +0000125 unsigned CharWidth = PP.getTargetInfo().getCharWidth(IsWide);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
128 PP.Diag(Loc, diag::warn_octal_escape_too_large);
129 ResultChar &= ~0U >> (32-CharWidth);
130 }
131 break;
132 }
133
134 // Otherwise, these are not valid escapes.
135 case '(': case '{': case '[': case '%':
136 // GCC accepts these as extensions. We warn about them as such though.
137 if (!PP.getLangOptions().NoExtensions) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000138 PP.Diag(Loc, diag::ext_nonstandard_escape)
139 << std::string()+(char)ResultChar;
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 break;
141 }
142 // FALL THROUGH.
143 default:
Chris Lattnerac92d822008-11-22 07:23:31 +0000144 if (isgraph(ThisTokBuf[0]))
Chris Lattner204b2fe2008-11-18 21:48:13 +0000145 PP.Diag(Loc, diag::ext_unknown_escape) << std::string()+(char)ResultChar;
Chris Lattnerac92d822008-11-22 07:23:31 +0000146 else
Chris Lattner204b2fe2008-11-18 21:48:13 +0000147 PP.Diag(Loc, diag::ext_unknown_escape) << "x"+llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 break;
149 }
150
151 return ResultChar;
152}
153
154
155
156
157/// integer-constant: [C99 6.4.4.1]
158/// decimal-constant integer-suffix
159/// octal-constant integer-suffix
160/// hexadecimal-constant integer-suffix
161/// decimal-constant:
162/// nonzero-digit
163/// decimal-constant digit
164/// octal-constant:
165/// 0
166/// octal-constant octal-digit
167/// hexadecimal-constant:
168/// hexadecimal-prefix hexadecimal-digit
169/// hexadecimal-constant hexadecimal-digit
170/// hexadecimal-prefix: one of
171/// 0x 0X
172/// integer-suffix:
173/// unsigned-suffix [long-suffix]
174/// unsigned-suffix [long-long-suffix]
175/// long-suffix [unsigned-suffix]
176/// long-long-suffix [unsigned-sufix]
177/// nonzero-digit:
178/// 1 2 3 4 5 6 7 8 9
179/// octal-digit:
180/// 0 1 2 3 4 5 6 7
181/// hexadecimal-digit:
182/// 0 1 2 3 4 5 6 7 8 9
183/// a b c d e f
184/// A B C D E F
185/// unsigned-suffix: one of
186/// u U
187/// long-suffix: one of
188/// l L
189/// long-long-suffix: one of
190/// ll LL
191///
192/// floating-constant: [C99 6.4.4.2]
193/// TODO: add rules...
194///
Reid Spencer5f016e22007-07-11 17:01:13 +0000195NumericLiteralParser::
196NumericLiteralParser(const char *begin, const char *end,
197 SourceLocation TokLoc, Preprocessor &pp)
198 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000199
200 // This routine assumes that the range begin/end matches the regex for integer
201 // and FP constants (specifically, the 'pp-number' regex), and assumes that
202 // the byte at "*end" is both valid and not part of the regex. Because of
203 // this, it doesn't have to check for 'overscan' in various places.
204 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
205 "Lexer didn't maximally munch?");
206
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 s = DigitsBegin = begin;
208 saw_exponent = false;
209 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 isLong = false;
211 isUnsigned = false;
212 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000213 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000214 isImaginary = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 hadError = false;
216
217 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000218 ParseNumberStartingWithZero(TokLoc);
219 if (hadError)
220 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 } else { // the first digit is non-zero
222 radix = 10;
223 s = SkipDigits(s);
224 if (s == ThisTokEnd) {
225 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000226 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000227 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
228 diag::err_invalid_decimal_digit) << std::string(s, s+1);
229 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 return;
231 } else if (*s == '.') {
232 s++;
233 saw_period = true;
234 s = SkipDigits(s);
235 }
Chris Lattner4411f462008-09-29 23:12:31 +0000236 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000237 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 s++;
239 saw_exponent = true;
240 if (*s == '+' || *s == '-') s++; // sign
241 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000242 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000244 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000245 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
246 diag::err_exponent_has_no_digits);
247 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000248 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 }
250 }
251 }
252
253 SuffixBegin = s;
Chris Lattner506b8de2007-08-26 01:58:14 +0000254
255 // Parse the suffix. At this point we can classify whether we have an FP or
256 // integer constant.
257 bool isFPConstant = isFloatingLiteral();
258
259 // Loop over all of the characters of the suffix. If we see something bad,
260 // we break out of the loop.
261 for (; s != ThisTokEnd; ++s) {
262 switch (*s) {
263 case 'f': // FP Suffix for "float"
264 case 'F':
265 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000266 if (isFloat || isLong) break; // FF, LF invalid.
267 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000268 continue; // Success.
269 case 'u':
270 case 'U':
271 if (isFPConstant) break; // Error for floating constant.
272 if (isUnsigned) break; // Cannot be repeated.
273 isUnsigned = true;
274 continue; // Success.
275 case 'l':
276 case 'L':
277 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000278 if (isFloat) break; // LF invalid.
Chris Lattner506b8de2007-08-26 01:58:14 +0000279
280 // Check for long long. The L's need to be adjacent and the same case.
281 if (s+1 != ThisTokEnd && s[1] == s[0]) {
282 if (isFPConstant) break; // long long invalid for floats.
283 isLongLong = true;
284 ++s; // Eat both of them.
285 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000288 continue; // Success.
289 case 'i':
Steve Naroff0c29b222008-04-04 21:02:54 +0000290 if (PP.getLangOptions().Microsoft) {
291 // Allow i8, i16, i32, i64, and i128.
292 if (++s == ThisTokEnd) break;
293 switch (*s) {
294 case '8':
295 s++; // i8 suffix
296 break;
297 case '1':
298 if (++s == ThisTokEnd) break;
299 if (*s == '6') s++; // i16 suffix
300 else if (*s == '2') {
301 if (++s == ThisTokEnd) break;
302 if (*s == '8') s++; // i128 suffix
303 }
304 break;
305 case '3':
306 if (++s == ThisTokEnd) break;
307 if (*s == '2') s++; // i32 suffix
308 break;
309 case '6':
310 if (++s == ThisTokEnd) break;
311 if (*s == '4') s++; // i64 suffix
312 break;
313 default:
314 break;
315 }
316 break;
317 }
318 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000319 case 'I':
320 case 'j':
321 case 'J':
322 if (isImaginary) break; // Cannot be repeated.
323 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
324 diag::ext_imaginary_constant);
325 isImaginary = true;
326 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000328 // If we reached here, there was an error.
329 break;
330 }
331
332 // Report an error if there are any.
333 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000334 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
335 isFPConstant ? diag::err_invalid_suffix_float_constant :
336 diag::err_invalid_suffix_integer_constant)
337 << std::string(SuffixBegin, ThisTokEnd);
338 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000339 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 }
341}
342
Chris Lattner368328c2008-06-30 06:39:54 +0000343/// ParseNumberStartingWithZero - This method is called when the first character
344/// of the number is found to be a zero. This means it is either an octal
345/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
346/// a floating point number (01239.123e4). Eat the prefix, determining the
347/// radix etc.
348void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
349 assert(s[0] == '0' && "Invalid method call");
350 s++;
351
352 // Handle a hex number like 0x1234.
353 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
354 s++;
355 radix = 16;
356 DigitsBegin = s;
357 s = SkipHexDigits(s);
358 if (s == ThisTokEnd) {
359 // Done.
360 } else if (*s == '.') {
361 s++;
362 saw_period = true;
363 s = SkipHexDigits(s);
364 }
365 // A binary exponent can appear with or with a '.'. If dotted, the
366 // binary exponent is required.
Chris Lattner6ea62382008-07-25 18:18:34 +0000367 if (*s == 'p' || *s == 'P') {
Chris Lattner368328c2008-06-30 06:39:54 +0000368 const char *Exponent = s;
369 s++;
370 saw_exponent = true;
371 if (*s == '+' || *s == '-') s++; // sign
372 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000373 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000374 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
375 diag::err_exponent_has_no_digits);
376 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000377 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000378 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000379 s = first_non_digit;
380
Chris Lattner49842122008-11-22 07:39:03 +0000381 if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000382 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000383 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000384 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
385 diag::err_hexconstant_requires_exponent);
386 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000387 }
388 return;
389 }
390
391 // Handle simple binary numbers 0b01010
392 if (*s == 'b' || *s == 'B') {
393 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000394 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000395 ++s;
396 radix = 2;
397 DigitsBegin = s;
398 s = SkipBinaryDigits(s);
399 if (s == ThisTokEnd) {
400 // Done.
401 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000402 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
403 diag::err_invalid_binary_digit) << std::string(s, s+1);
404 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000405 }
Chris Lattner413d3552008-06-30 06:44:49 +0000406 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000407 return;
408 }
409
410 // For now, the radix is set to 8. If we discover that we have a
411 // floating point constant, the radix will change to 10. Octal floating
412 // point constants are not permitted (only decimal and hexadecimal).
413 radix = 8;
414 DigitsBegin = s;
415 s = SkipOctalDigits(s);
416 if (s == ThisTokEnd)
417 return; // Done, simple octal number like 01234
418
Chris Lattner413d3552008-06-30 06:44:49 +0000419 // If we have some other non-octal digit that *is* a decimal digit, see if
420 // this is part of a floating point number like 094.123 or 09e1.
421 if (isdigit(*s)) {
422 const char *EndDecimal = SkipDigits(s);
423 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
424 s = EndDecimal;
425 radix = 10;
426 }
427 }
428
429 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
430 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000431 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000432 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
433 diag::err_invalid_octal_digit) << std::string(s, s+1);
434 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000435 return;
436 }
437
438 if (*s == '.') {
439 s++;
440 radix = 10;
441 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000442 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000443 }
444 if (*s == 'e' || *s == 'E') { // exponent
445 const char *Exponent = s;
446 s++;
447 radix = 10;
448 saw_exponent = true;
449 if (*s == '+' || *s == '-') s++; // sign
450 const char *first_non_digit = SkipDigits(s);
451 if (first_non_digit != s) {
452 s = first_non_digit;
453 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000454 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
455 diag::err_exponent_has_no_digits);
456 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000457 return;
458 }
459 }
460}
461
462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463/// GetIntegerValue - Convert this numeric literal value to an APInt that
464/// matches Val's input width. If there is an overflow, set Val to the low bits
465/// of the result and return true. Otherwise, return false.
466bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000467 // Fast path: Compute a conservative bound on the maximum number of
468 // bits per digit in this radix. If we can't possibly overflow a
469 // uint64 based on that bound then do the simple conversion to
470 // integer. This avoids the expensive overflow checking below, and
471 // handles the common cases that matter (small decimal integers and
472 // hex/octal values which don't overflow).
473 unsigned MaxBitsPerDigit = 1;
474 while ((1U << MaxBitsPerDigit) < radix)
475 MaxBitsPerDigit += 1;
476 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
477 uint64_t N = 0;
478 for (s = DigitsBegin; s != SuffixBegin; ++s)
479 N = N*radix + HexDigitValue(*s);
480
481 // This will truncate the value to Val's input width. Simply check
482 // for overflow by comparing.
483 Val = N;
484 return Val.getZExtValue() != N;
485 }
486
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 Val = 0;
488 s = DigitsBegin;
489
490 llvm::APInt RadixVal(Val.getBitWidth(), radix);
491 llvm::APInt CharVal(Val.getBitWidth(), 0);
492 llvm::APInt OldVal = Val;
493
494 bool OverflowOccurred = false;
495 while (s < SuffixBegin) {
496 unsigned C = HexDigitValue(*s++);
497
498 // If this letter is out of bound for this radix, reject it.
499 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
500
501 CharVal = C;
502
503 // Add the digit to the value in the appropriate radix. If adding in digits
504 // made the value smaller, then this overflowed.
505 OldVal = Val;
506
507 // Multiply by radix, did overflow occur on the multiply?
508 Val *= RadixVal;
509 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
510
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000512 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 OverflowOccurred |= Val.ult(CharVal);
515 }
516 return OverflowOccurred;
517}
518
Chris Lattner525a0502007-09-22 18:29:59 +0000519llvm::APFloat NumericLiteralParser::
Ted Kremenek427d5af2007-11-26 23:12:30 +0000520GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) {
521 using llvm::APFloat;
522
Ted Kremenek32e61bf2007-11-29 00:54:29 +0000523 llvm::SmallVector<char,256> floatChars;
524 for (unsigned i = 0, n = ThisTokEnd-ThisTokBegin; i != n; ++i)
525 floatChars.push_back(ThisTokBegin[i]);
526
527 floatChars.push_back('\0');
528
Ted Kremenek427d5af2007-11-26 23:12:30 +0000529 APFloat V (Format, APFloat::fcZero, false);
Ted Kremenek427d5af2007-11-26 23:12:30 +0000530 APFloat::opStatus status;
Ted Kremenek32e61bf2007-11-29 00:54:29 +0000531
532 status = V.convertFromString(&floatChars[0],APFloat::rmNearestTiesToEven);
Ted Kremenek427d5af2007-11-26 23:12:30 +0000533
534 if (isExact)
535 *isExact = status == APFloat::opOK;
536
537 return V;
Reid Spencer5f016e22007-07-11 17:01:13 +0000538}
539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540
541CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
542 SourceLocation Loc, Preprocessor &PP) {
543 // At this point we know that the character matches the regex "L?'.*'".
544 HadError = false;
545 Value = 0;
546
547 // Determine if this is a wide character.
548 IsWide = begin[0] == 'L';
549 if (IsWide) ++begin;
550
551 // Skip over the entry quote.
552 assert(begin[0] == '\'' && "Invalid token lexed");
553 ++begin;
554
555 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
556 // size of "value".
Chris Lattner98be4942008-03-05 18:54:05 +0000557 assert(PP.getTargetInfo().getIntWidth() == 32 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 "Assumes sizeof(int) == 4 for now");
559 // FIXME: This assumes that wchar_t is 32-bits for now.
Chris Lattner98be4942008-03-05 18:54:05 +0000560 assert(PP.getTargetInfo().getWCharWidth() == 32 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 "Assumes sizeof(wchar_t) == 4 for now");
562 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000563 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 "Assumes char is 8 bits");
565
566 bool isFirstChar = true;
567 bool isMultiChar = false;
568 while (begin[0] != '\'') {
569 unsigned ResultChar;
570 if (begin[0] != '\\') // If this is a normal character, consume it.
571 ResultChar = *begin++;
572 else // Otherwise, this is an escape character.
573 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
574
575 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
576 // implementation defined (C99 6.4.4.4p10).
577 if (!isFirstChar) {
578 // If this is the second character being processed, do special handling.
579 if (!isMultiChar) {
580 isMultiChar = true;
581
582 // Warn about discarding the top bits for multi-char wide-character
583 // constants (L'abcd').
584 if (IsWide)
585 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
586 }
587
588 if (IsWide) {
589 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
590 Value = 0;
591 } else {
592 // Narrow character literals act as though their value is concatenated
593 // in this implementation.
594 if (((Value << 8) >> 8) != Value)
595 PP.Diag(Loc, diag::warn_char_constant_too_large);
596 Value <<= 8;
597 }
598 }
599
600 Value += ResultChar;
601 isFirstChar = false;
602 }
603
604 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
605 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
606 // character constants are not sign extended in the this implementation:
607 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
608 if (!IsWide && !isMultiChar && (Value & 128) &&
Chris Lattner98be4942008-03-05 18:54:05 +0000609 PP.getTargetInfo().isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 Value = (signed char)Value;
611}
612
613
614/// string-literal: [C99 6.4.5]
615/// " [s-char-sequence] "
616/// L" [s-char-sequence] "
617/// s-char-sequence:
618/// s-char
619/// s-char-sequence s-char
620/// s-char:
621/// any source character except the double quote ",
622/// backslash \, or newline character
623/// escape-character
624/// universal-character-name
625/// escape-character: [C99 6.4.4.4]
626/// \ escape-code
627/// universal-character-name
628/// escape-code:
629/// character-escape-code
630/// octal-escape-code
631/// hex-escape-code
632/// character-escape-code: one of
633/// n t b r f v a
634/// \ ' " ?
635/// octal-escape-code:
636/// octal-digit
637/// octal-digit octal-digit
638/// octal-digit octal-digit octal-digit
639/// hex-escape-code:
640/// x hex-digit
641/// hex-escape-code hex-digit
642/// universal-character-name:
643/// \u hex-quad
644/// \U hex-quad hex-quad
645/// hex-quad:
646/// hex-digit hex-digit hex-digit hex-digit
647///
648StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000649StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000650 Preprocessor &pp) : PP(pp) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 // Scan all of the string portions, remember the max individual token length,
652 // computing a bound on the concatenated string length, and see whether any
653 // piece is a wide-string. If any of the string portions is a wide-string
654 // literal, the result is a wide-string literal [C99 6.4.5p4].
655 MaxTokenLength = StringToks[0].getLength();
656 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000657 AnyWide = StringToks[0].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658
659 hadError = false;
660
661 // Implement Translation Phase #6: concatenation of string literals
662 /// (C99 5.1.1.2p1). The common case is only one string fragment.
663 for (unsigned i = 1; i != NumStringToks; ++i) {
664 // The string could be shorter than this if it needs cleaning, but this is a
665 // reasonable bound, which is all we need.
666 SizeBound += StringToks[i].getLength()-2; // -2 for "".
667
668 // Remember maximum string piece length.
669 if (StringToks[i].getLength() > MaxTokenLength)
670 MaxTokenLength = StringToks[i].getLength();
671
672 // Remember if we see any wide strings.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000673 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 }
675
676
677 // Include space for the null terminator.
678 ++SizeBound;
679
680 // TODO: K&R warning: "traditional C rejects string constant concatenation"
681
682 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
683 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
684 wchar_tByteWidth = ~0U;
685 if (AnyWide) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000686 wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
688 wchar_tByteWidth /= 8;
689 }
690
691 // The output buffer size needs to be large enough to hold wide characters.
692 // This is a worst-case assumption which basically corresponds to L"" "long".
693 if (AnyWide)
694 SizeBound *= wchar_tByteWidth;
695
696 // Size the temporary buffer to hold the result string data.
697 ResultBuf.resize(SizeBound);
698
699 // Likewise, but for each string piece.
700 llvm::SmallString<512> TokenBuf;
701 TokenBuf.resize(MaxTokenLength);
702
703 // Loop over all the strings, getting their spelling, and expanding them to
704 // wide strings as appropriate.
705 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
706
Anders Carlssonee98ac52007-10-15 02:50:23 +0000707 Pascal = false;
708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
710 const char *ThisTokBuf = &TokenBuf[0];
711 // Get the spelling of the token, which eliminates trigraphs, etc. We know
712 // that ThisTokBuf points to a buffer that is big enough for the whole token
713 // and 'spelled' tokens can only shrink.
714 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
715 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
716
717 // TODO: Input character set mapping support.
718
719 // Skip L marker for wide strings.
720 bool ThisIsWide = false;
721 if (ThisTokBuf[0] == 'L') {
722 ++ThisTokBuf;
723 ThisIsWide = true;
724 }
725
726 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
727 ++ThisTokBuf;
728
Anders Carlssonee98ac52007-10-15 02:50:23 +0000729 // Check if this is a pascal string
730 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
731 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
732
733 // If the \p sequence is found in the first token, we have a pascal string
734 // Otherwise, if we already have a pascal string, ignore the first \p
735 if (i == 0) {
736 ++ThisTokBuf;
737 Pascal = true;
738 } else if (Pascal)
739 ThisTokBuf += 2;
740 }
741
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 while (ThisTokBuf != ThisTokEnd) {
743 // Is this a span of non-escape characters?
744 if (ThisTokBuf[0] != '\\') {
745 const char *InStart = ThisTokBuf;
746 do {
747 ++ThisTokBuf;
748 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
749
750 // Copy the character span over.
751 unsigned Len = ThisTokBuf-InStart;
752 if (!AnyWide) {
753 memcpy(ResultPtr, InStart, Len);
754 ResultPtr += Len;
755 } else {
756 // Note: our internal rep of wide char tokens is always little-endian.
757 for (; Len; --Len, ++InStart) {
758 *ResultPtr++ = InStart[0];
759 // Add zeros at the end.
760 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
761 *ResultPtr++ = 0;
762 }
763 }
764 continue;
765 }
766
767 // Otherwise, this is an escape character. Process it.
768 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
769 StringToks[i].getLocation(),
770 ThisIsWide, PP);
771
772 // Note: our internal rep of wide char tokens is always little-endian.
773 *ResultPtr++ = ResultChar & 0xFF;
774
775 if (AnyWide) {
776 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
777 *ResultPtr++ = ResultChar >> i*8;
778 }
779 }
780 }
781
782 // Add zero terminator.
783 *ResultPtr = 0;
784 if (AnyWide) {
785 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
786 *ResultPtr++ = 0;
787 }
Anders Carlssonee98ac52007-10-15 02:50:23 +0000788
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000789 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +0000790 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000791
792 // Verify that pascal strings aren't too large.
793 if (GetStringLength() > 256)
794 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
795 << SourceRange(StringToks[0].getLocation(),
796 StringToks[NumStringToks-1].getLocation());
797 hadError = 1;
798 return;
799 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000800}
Chris Lattner719e6152009-02-18 19:21:10 +0000801
802
803/// getOffsetOfStringByte - This function returns the offset of the
804/// specified byte of the string data represented by Token. This handles
805/// advancing over escape sequences in the string.
806unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
807 unsigned ByteNo,
808 Preprocessor &PP) {
809 // Get the spelling of the token.
810 llvm::SmallString<16> SpellingBuffer;
811 SpellingBuffer.resize(Tok.getLength());
812
813 const char *SpellingPtr = &SpellingBuffer[0];
814 unsigned TokLen = PP.getSpelling(Tok, SpellingPtr);
815
816 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
817
818
819 const char *SpellingStart = SpellingPtr;
820 const char *SpellingEnd = SpellingPtr+TokLen;
821
822 // Skip over the leading quote.
823 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
824 ++SpellingPtr;
825
826 // Skip over bytes until we find the offset we're looking for.
827 while (ByteNo) {
828 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
829
830 // Step over non-escapes simply.
831 if (*SpellingPtr != '\\') {
832 ++SpellingPtr;
833 --ByteNo;
834 continue;
835 }
836
837 // Otherwise, this is an escape character. Advance over it.
838 bool HadError = false;
839 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
840 Tok.getLocation(), false, PP);
841 assert(!HadError && "This method isn't valid on erroneous strings");
842 --ByteNo;
843 }
844
845 return SpellingPtr-SpellingStart;
846}