blob: ebc66271dbd36409ad278592a84b8ba9f585ff87 [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//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/Diagnostic.h"
Chris Lattner136f93a2007-07-16 06:55:01 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
21using namespace clang;
22
23/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
24/// not valid.
25static int HexDigitValue(char C) {
26 if (C >= '0' && C <= '9') return C-'0';
27 if (C >= 'a' && C <= 'f') return C-'a'+10;
28 if (C >= 'A' && C <= 'F') return C-'A'+10;
29 return -1;
30}
31
32/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
33/// either a character or a string literal.
34static unsigned ProcessCharEscape(const char *&ThisTokBuf,
35 const char *ThisTokEnd, bool &HadError,
36 SourceLocation Loc, bool IsWide,
37 Preprocessor &PP) {
38 // 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;
47
48 // 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':
57 PP.Diag(Loc, diag::ext_nonstandard_escape, "e");
58 ResultChar = 27;
59 break;
60 case 'f':
61 ResultChar = 12;
62 break;
63 case 'n':
64 ResultChar = 10;
65 break;
66 case 'r':
67 ResultChar = 13;
68 break;
69 case 't':
70 ResultChar = 9;
71 break;
72 case 'v':
73 ResultChar = 11;
74 break;
75
76 //case 'u': case 'U': // FIXME: UCNs.
77 case 'x': { // Hex escape.
78 ResultChar = 0;
79 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
80 PP.Diag(Loc, diag::err_hex_escape_no_digits);
81 HadError = 1;
82 break;
83 }
84
85 // Hex escapes are a maximal series of hex digits.
86 bool Overflow = false;
87 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
88 int CharVal = HexDigitValue(ThisTokBuf[0]);
89 if (CharVal == -1) break;
Chris Lattnerb8128142007-09-03 18:28:41 +000090 Overflow |= (ResultChar & 0xF0000000) ? true : false; // About to shift out a digit?
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.
96 unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc)
97 : PP.getTargetInfo().getCharWidth(Loc);
98 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'.
125 unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc)
126 : PP.getTargetInfo().getCharWidth(Loc);
127 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) {
138 PP.Diag(Loc, diag::ext_nonstandard_escape,
139 std::string()+(char)ResultChar);
140 break;
141 }
142 // FALL THROUGH.
143 default:
144 if (isgraph(ThisTokBuf[0])) {
145 PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar);
146 } else {
147 PP.Diag(Loc, diag::ext_unknown_escape, "x"+llvm::utohexstr(ResultChar));
148 }
149 break;
150 }
151
152 return ResultChar;
153}
154
155
156
157
158/// integer-constant: [C99 6.4.4.1]
159/// decimal-constant integer-suffix
160/// octal-constant integer-suffix
161/// hexadecimal-constant integer-suffix
162/// decimal-constant:
163/// nonzero-digit
164/// decimal-constant digit
165/// octal-constant:
166/// 0
167/// octal-constant octal-digit
168/// hexadecimal-constant:
169/// hexadecimal-prefix hexadecimal-digit
170/// hexadecimal-constant hexadecimal-digit
171/// hexadecimal-prefix: one of
172/// 0x 0X
173/// integer-suffix:
174/// unsigned-suffix [long-suffix]
175/// unsigned-suffix [long-long-suffix]
176/// long-suffix [unsigned-suffix]
177/// long-long-suffix [unsigned-sufix]
178/// nonzero-digit:
179/// 1 2 3 4 5 6 7 8 9
180/// octal-digit:
181/// 0 1 2 3 4 5 6 7
182/// hexadecimal-digit:
183/// 0 1 2 3 4 5 6 7 8 9
184/// a b c d e f
185/// A B C D E F
186/// unsigned-suffix: one of
187/// u U
188/// long-suffix: one of
189/// l L
190/// long-long-suffix: one of
191/// ll LL
192///
193/// floating-constant: [C99 6.4.4.2]
194/// TODO: add rules...
195///
196
197NumericLiteralParser::
198NumericLiteralParser(const char *begin, const char *end,
199 SourceLocation TokLoc, Preprocessor &pp)
200 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
201 s = DigitsBegin = begin;
202 saw_exponent = false;
203 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 isLong = false;
205 isUnsigned = false;
206 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000207 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000208 isImaginary = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 hadError = false;
210
211 if (*s == '0') { // parse radix
212 s++;
213 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
214 s++;
215 radix = 16;
216 DigitsBegin = s;
217 s = SkipHexDigits(s);
218 if (s == ThisTokEnd) {
219 // Done.
220 } else if (*s == '.') {
221 s++;
222 saw_period = true;
223 s = SkipHexDigits(s);
224 }
225 // A binary exponent can appear with or with a '.'. If dotted, the
226 // binary exponent is required.
Chris Lattner921e9ff2007-11-14 16:14:50 +0000227 if ((*s == 'p' || *s == 'P') && PP.getLangOptions().HexFloats) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 s++;
229 saw_exponent = true;
230 if (*s == '+' || *s == '-') s++; // sign
231 const char *first_non_digit = SkipDigits(s);
232 if (first_non_digit == s) {
233 Diag(TokLoc, diag::err_exponent_has_no_digits);
234 return;
235 } else {
236 s = first_non_digit;
237 }
238 } else if (saw_period) {
239 Diag(TokLoc, diag::err_hexconstant_requires_exponent);
240 return;
241 }
242 } else if (*s == 'b' || *s == 'B') {
243 // 0b101010 is a GCC extension.
244 ++s;
245 radix = 2;
246 DigitsBegin = s;
247 s = SkipBinaryDigits(s);
248 if (s == ThisTokEnd) {
249 // Done.
250 } else if (isxdigit(*s)) {
251 Diag(TokLoc, diag::err_invalid_binary_digit, std::string(s, s+1));
252 return;
253 }
254 PP.Diag(TokLoc, diag::ext_binary_literal);
255 } else {
256 // For now, the radix is set to 8. If we discover that we have a
257 // floating point constant, the radix will change to 10. Octal floating
258 // point constants are not permitted (only decimal and hexadecimal).
259 radix = 8;
260 DigitsBegin = s;
261 s = SkipOctalDigits(s);
262 if (s == ThisTokEnd) {
263 // Done.
264 } else if (isxdigit(*s)) {
Chris Lattner136f93a2007-07-16 06:55:01 +0000265 TokLoc = PP.AdvanceToTokenCharacter(TokLoc, s-begin);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1));
267 return;
268 } else if (*s == '.') {
269 s++;
270 radix = 10;
271 saw_period = true;
272 s = SkipDigits(s);
273 }
274 if (*s == 'e' || *s == 'E') { // exponent
275 s++;
276 radix = 10;
277 saw_exponent = true;
278 if (*s == '+' || *s == '-') s++; // sign
279 const char *first_non_digit = SkipDigits(s);
280 if (first_non_digit == s) {
281 Diag(TokLoc, diag::err_exponent_has_no_digits);
282 return;
283 } else {
284 s = first_non_digit;
285 }
286 }
287 }
288 } else { // the first digit is non-zero
289 radix = 10;
290 s = SkipDigits(s);
291 if (s == ThisTokEnd) {
292 // Done.
293 } else if (isxdigit(*s)) {
294 Diag(TokLoc, diag::err_invalid_decimal_digit, std::string(s, s+1));
295 return;
296 } else if (*s == '.') {
297 s++;
298 saw_period = true;
299 s = SkipDigits(s);
300 }
301 if (*s == 'e' || *s == 'E') { // exponent
302 s++;
303 saw_exponent = true;
304 if (*s == '+' || *s == '-') s++; // sign
305 const char *first_non_digit = SkipDigits(s);
306 if (first_non_digit == s) {
307 Diag(TokLoc, diag::err_exponent_has_no_digits);
308 return;
309 } else {
310 s = first_non_digit;
311 }
312 }
313 }
314
315 SuffixBegin = s;
Chris Lattner506b8de2007-08-26 01:58:14 +0000316
317 // Parse the suffix. At this point we can classify whether we have an FP or
318 // integer constant.
319 bool isFPConstant = isFloatingLiteral();
320
321 // Loop over all of the characters of the suffix. If we see something bad,
322 // we break out of the loop.
323 for (; s != ThisTokEnd; ++s) {
324 switch (*s) {
325 case 'f': // FP Suffix for "float"
326 case 'F':
327 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000328 if (isFloat || isLong) break; // FF, LF invalid.
329 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000330 continue; // Success.
331 case 'u':
332 case 'U':
333 if (isFPConstant) break; // Error for floating constant.
334 if (isUnsigned) break; // Cannot be repeated.
335 isUnsigned = true;
336 continue; // Success.
337 case 'l':
338 case 'L':
339 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000340 if (isFloat) break; // LF invalid.
Chris Lattner506b8de2007-08-26 01:58:14 +0000341
342 // Check for long long. The L's need to be adjacent and the same case.
343 if (s+1 != ThisTokEnd && s[1] == s[0]) {
344 if (isFPConstant) break; // long long invalid for floats.
345 isLongLong = true;
346 ++s; // Eat both of them.
347 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000350 continue; // Success.
351 case 'i':
352 case 'I':
353 case 'j':
354 case 'J':
355 if (isImaginary) break; // Cannot be repeated.
356 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
357 diag::ext_imaginary_constant);
358 isImaginary = true;
359 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000361 // If we reached here, there was an error.
362 break;
363 }
364
365 // Report an error if there are any.
366 if (s != ThisTokEnd) {
367 TokLoc = PP.AdvanceToTokenCharacter(TokLoc, s-begin);
368 Diag(TokLoc, isFPConstant ? diag::err_invalid_suffix_float_constant :
369 diag::err_invalid_suffix_integer_constant,
370 std::string(SuffixBegin, ThisTokEnd));
371 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 }
373}
374
375/// GetIntegerValue - Convert this numeric literal value to an APInt that
376/// matches Val's input width. If there is an overflow, set Val to the low bits
377/// of the result and return true. Otherwise, return false.
378bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
379 Val = 0;
380 s = DigitsBegin;
381
382 llvm::APInt RadixVal(Val.getBitWidth(), radix);
383 llvm::APInt CharVal(Val.getBitWidth(), 0);
384 llvm::APInt OldVal = Val;
385
386 bool OverflowOccurred = false;
387 while (s < SuffixBegin) {
388 unsigned C = HexDigitValue(*s++);
389
390 // If this letter is out of bound for this radix, reject it.
391 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
392
393 CharVal = C;
394
395 // Add the digit to the value in the appropriate radix. If adding in digits
396 // made the value smaller, then this overflowed.
397 OldVal = Val;
398
399 // Multiply by radix, did overflow occur on the multiply?
400 Val *= RadixVal;
401 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
402
403 OldVal = Val;
404 // Add value, did overflow occur on the value?
405 Val += CharVal;
406 OverflowOccurred |= Val.ult(OldVal);
407 OverflowOccurred |= Val.ult(CharVal);
408 }
409 return OverflowOccurred;
410}
411
Chris Lattner525a0502007-09-22 18:29:59 +0000412llvm::APFloat NumericLiteralParser::
Ted Kremenek427d5af2007-11-26 23:12:30 +0000413GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) {
414 using llvm::APFloat;
415
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 char floatChars[256];
417 strncpy(floatChars, ThisTokBegin, ThisTokEnd-ThisTokBegin);
418 floatChars[ThisTokEnd-ThisTokBegin] = '\0';
Ted Kremenek427d5af2007-11-26 23:12:30 +0000419
420#if 1
421 APFloat V (Format, APFloat::fcZero, false);
422
423 APFloat::opStatus status;
424 status = V.convertFromString(floatChars,APFloat::rmTowardZero);
425
426 if (isExact)
427 *isExact = status == APFloat::opOK;
428
429 return V;
Chris Lattner1c9bdae2007-09-22 18:38:30 +0000430#else
431 // FIXME: this is horrible!
Ted Kremenek427d5af2007-11-26 23:12:30 +0000432 APFloat V(strtod(floatChars, 0));
433 V.convert(Format, APFloat::rmTowardZero);
Chris Lattner1c9bdae2007-09-22 18:38:30 +0000434 return V;
435#endif
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
437
438void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID,
439 const std::string &M) {
440 PP.Diag(Loc, DiagID, M);
441 hadError = true;
442}
443
444
445CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
446 SourceLocation Loc, Preprocessor &PP) {
447 // At this point we know that the character matches the regex "L?'.*'".
448 HadError = false;
449 Value = 0;
450
451 // Determine if this is a wide character.
452 IsWide = begin[0] == 'L';
453 if (IsWide) ++begin;
454
455 // Skip over the entry quote.
456 assert(begin[0] == '\'' && "Invalid token lexed");
457 ++begin;
458
459 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
460 // size of "value".
461 assert(PP.getTargetInfo().getIntWidth(Loc) == 32 &&
462 "Assumes sizeof(int) == 4 for now");
463 // FIXME: This assumes that wchar_t is 32-bits for now.
464 assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 &&
465 "Assumes sizeof(wchar_t) == 4 for now");
466 // FIXME: This extensively assumes that 'char' is 8-bits.
467 assert(PP.getTargetInfo().getCharWidth(Loc) == 8 &&
468 "Assumes char is 8 bits");
469
470 bool isFirstChar = true;
471 bool isMultiChar = false;
472 while (begin[0] != '\'') {
473 unsigned ResultChar;
474 if (begin[0] != '\\') // If this is a normal character, consume it.
475 ResultChar = *begin++;
476 else // Otherwise, this is an escape character.
477 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
478
479 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
480 // implementation defined (C99 6.4.4.4p10).
481 if (!isFirstChar) {
482 // If this is the second character being processed, do special handling.
483 if (!isMultiChar) {
484 isMultiChar = true;
485
486 // Warn about discarding the top bits for multi-char wide-character
487 // constants (L'abcd').
488 if (IsWide)
489 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
490 }
491
492 if (IsWide) {
493 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
494 Value = 0;
495 } else {
496 // Narrow character literals act as though their value is concatenated
497 // in this implementation.
498 if (((Value << 8) >> 8) != Value)
499 PP.Diag(Loc, diag::warn_char_constant_too_large);
500 Value <<= 8;
501 }
502 }
503
504 Value += ResultChar;
505 isFirstChar = false;
506 }
507
508 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
509 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
510 // character constants are not sign extended in the this implementation:
511 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
512 if (!IsWide && !isMultiChar && (Value & 128) &&
513 PP.getTargetInfo().isCharSigned(Loc))
514 Value = (signed char)Value;
515}
516
517
518/// string-literal: [C99 6.4.5]
519/// " [s-char-sequence] "
520/// L" [s-char-sequence] "
521/// s-char-sequence:
522/// s-char
523/// s-char-sequence s-char
524/// s-char:
525/// any source character except the double quote ",
526/// backslash \, or newline character
527/// escape-character
528/// universal-character-name
529/// escape-character: [C99 6.4.4.4]
530/// \ escape-code
531/// universal-character-name
532/// escape-code:
533/// character-escape-code
534/// octal-escape-code
535/// hex-escape-code
536/// character-escape-code: one of
537/// n t b r f v a
538/// \ ' " ?
539/// octal-escape-code:
540/// octal-digit
541/// octal-digit octal-digit
542/// octal-digit octal-digit octal-digit
543/// hex-escape-code:
544/// x hex-digit
545/// hex-escape-code hex-digit
546/// universal-character-name:
547/// \u hex-quad
548/// \U hex-quad hex-quad
549/// hex-quad:
550/// hex-digit hex-digit hex-digit hex-digit
551///
552StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000553StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 Preprocessor &pp, TargetInfo &t)
555 : PP(pp), Target(t) {
556 // Scan all of the string portions, remember the max individual token length,
557 // computing a bound on the concatenated string length, and see whether any
558 // piece is a wide-string. If any of the string portions is a wide-string
559 // literal, the result is a wide-string literal [C99 6.4.5p4].
560 MaxTokenLength = StringToks[0].getLength();
561 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000562 AnyWide = StringToks[0].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
564 hadError = false;
565
566 // Implement Translation Phase #6: concatenation of string literals
567 /// (C99 5.1.1.2p1). The common case is only one string fragment.
568 for (unsigned i = 1; i != NumStringToks; ++i) {
569 // The string could be shorter than this if it needs cleaning, but this is a
570 // reasonable bound, which is all we need.
571 SizeBound += StringToks[i].getLength()-2; // -2 for "".
572
573 // Remember maximum string piece length.
574 if (StringToks[i].getLength() > MaxTokenLength)
575 MaxTokenLength = StringToks[i].getLength();
576
577 // Remember if we see any wide strings.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000578 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 }
580
581
582 // Include space for the null terminator.
583 ++SizeBound;
584
585 // TODO: K&R warning: "traditional C rejects string constant concatenation"
586
587 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
588 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
589 wchar_tByteWidth = ~0U;
590 if (AnyWide) {
591 wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation());
592 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
593 wchar_tByteWidth /= 8;
594 }
595
596 // The output buffer size needs to be large enough to hold wide characters.
597 // This is a worst-case assumption which basically corresponds to L"" "long".
598 if (AnyWide)
599 SizeBound *= wchar_tByteWidth;
600
601 // Size the temporary buffer to hold the result string data.
602 ResultBuf.resize(SizeBound);
603
604 // Likewise, but for each string piece.
605 llvm::SmallString<512> TokenBuf;
606 TokenBuf.resize(MaxTokenLength);
607
608 // Loop over all the strings, getting their spelling, and expanding them to
609 // wide strings as appropriate.
610 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
611
Anders Carlssonee98ac52007-10-15 02:50:23 +0000612 Pascal = false;
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
615 const char *ThisTokBuf = &TokenBuf[0];
616 // Get the spelling of the token, which eliminates trigraphs, etc. We know
617 // that ThisTokBuf points to a buffer that is big enough for the whole token
618 // and 'spelled' tokens can only shrink.
619 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
620 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
621
622 // TODO: Input character set mapping support.
623
624 // Skip L marker for wide strings.
625 bool ThisIsWide = false;
626 if (ThisTokBuf[0] == 'L') {
627 ++ThisTokBuf;
628 ThisIsWide = true;
629 }
630
631 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
632 ++ThisTokBuf;
633
Anders Carlssonee98ac52007-10-15 02:50:23 +0000634 // Check if this is a pascal string
635 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
636 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
637
638 // If the \p sequence is found in the first token, we have a pascal string
639 // Otherwise, if we already have a pascal string, ignore the first \p
640 if (i == 0) {
641 ++ThisTokBuf;
642 Pascal = true;
643 } else if (Pascal)
644 ThisTokBuf += 2;
645 }
646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 while (ThisTokBuf != ThisTokEnd) {
648 // Is this a span of non-escape characters?
649 if (ThisTokBuf[0] != '\\') {
650 const char *InStart = ThisTokBuf;
651 do {
652 ++ThisTokBuf;
653 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
654
655 // Copy the character span over.
656 unsigned Len = ThisTokBuf-InStart;
657 if (!AnyWide) {
658 memcpy(ResultPtr, InStart, Len);
659 ResultPtr += Len;
660 } else {
661 // Note: our internal rep of wide char tokens is always little-endian.
662 for (; Len; --Len, ++InStart) {
663 *ResultPtr++ = InStart[0];
664 // Add zeros at the end.
665 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
666 *ResultPtr++ = 0;
667 }
668 }
669 continue;
670 }
671
672 // Otherwise, this is an escape character. Process it.
673 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
674 StringToks[i].getLocation(),
675 ThisIsWide, PP);
676
677 // Note: our internal rep of wide char tokens is always little-endian.
678 *ResultPtr++ = ResultChar & 0xFF;
679
680 if (AnyWide) {
681 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
682 *ResultPtr++ = ResultChar >> i*8;
683 }
684 }
685 }
686
687 // Add zero terminator.
688 *ResultPtr = 0;
689 if (AnyWide) {
690 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
691 *ResultPtr++ = 0;
692 }
Anders Carlssonee98ac52007-10-15 02:50:23 +0000693
694 if (Pascal)
695 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000696}