blob: 3ac9837efc1fff73641f5153d2d94653db333237 [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//
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//
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"
17#include "clang/Basic/TargetInfo.h"
18#include "clang/Basic/Diagnostic.h"
Chris Lattner5b743d32007-04-04 05:52:58 +000019#include "llvm/ADT/APInt.h"
Steve Naroff4f88b312007-03-13 22:37:02 +000020#include "llvm/ADT/StringExtras.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000021using namespace llvm;
22using namespace clang;
23
Chris Lattner2f5add62007-04-05 06:57:15 +000024/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
25/// not valid.
26static int HexDigitValue(char C) {
27 if (C >= '0' && C <= '9') return C-'0';
28 if (C >= 'a' && C <= 'f') return C-'a'+10;
29 if (C >= 'A' && C <= 'F') return C-'A'+10;
30 return -1;
31}
32
33/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
34/// either a character or a string literal.
35static unsigned ProcessCharEscape(const char *&ThisTokBuf,
36 const char *ThisTokEnd, bool &HadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +000037 SourceLocation Loc, bool IsWide,
38 Preprocessor &PP) {
Chris Lattner2f5add62007-04-05 06:57:15 +000039 // Skip the '\' char.
40 ++ThisTokBuf;
41
42 // We know that this character can't be off the end of the buffer, because
43 // that would have been \", which would not have been the end of string.
44 unsigned ResultChar = *ThisTokBuf++;
45 switch (ResultChar) {
46 // These map to themselves.
47 case '\\': case '\'': case '"': case '?': break;
48
49 // These have fixed mappings.
50 case 'a':
51 // TODO: K&R: the meaning of '\\a' is different in traditional C
52 ResultChar = 7;
53 break;
54 case 'b':
55 ResultChar = 8;
56 break;
57 case 'e':
58 PP.Diag(Loc, diag::ext_nonstandard_escape, "e");
59 ResultChar = 27;
60 break;
61 case 'f':
62 ResultChar = 12;
63 break;
64 case 'n':
65 ResultChar = 10;
66 break;
67 case 'r':
68 ResultChar = 13;
69 break;
70 case 't':
71 ResultChar = 9;
72 break;
73 case 'v':
74 ResultChar = 11;
75 break;
76
77 //case 'u': case 'U': // FIXME: UCNs.
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;
91 Overflow |= ResultChar & 0xF0000000; // About to shift out a digit?
92 ResultChar <<= 4;
93 ResultChar |= CharVal;
94 }
95
96 // See if any bits will be truncated when evaluated as a character.
97 unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc)
98 : PP.getTargetInfo().getCharWidth(Loc);
99 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
100 Overflow = true;
101 ResultChar &= ~0U >> (32-CharWidth);
102 }
103
104 // Check for overflow.
105 if (Overflow) // Too many digits to fit in
106 PP.Diag(Loc, diag::warn_hex_escape_too_large);
Chris Lattner2f5add62007-04-05 06:57:15 +0000107 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000108 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000109 case '0': case '1': case '2': case '3':
Chris Lattner812eda82007-05-20 05:17:04 +0000110 case '4': case '5': case '6': case '7': {
Chris Lattner2f5add62007-04-05 06:57:15 +0000111 // Octal escapes.
Chris Lattner812eda82007-05-20 05:17:04 +0000112 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 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000131 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000132 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000133
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"+utohexstr(ResultChar));
148 }
149 break;
150 }
151
152 return ResultChar;
153}
154
155
156
157
Steve Naroff09ef4742007-03-09 23:16:33 +0000158/// 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,
Chris Lattner2f5add62007-04-05 06:57:15 +0000199 SourceLocation TokLoc, Preprocessor &pp)
200 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000201 s = DigitsBegin = begin;
202 saw_exponent = false;
203 saw_period = false;
204 saw_float_suffix = false;
205 isLong = false;
206 isUnsigned = false;
207 isLongLong = false;
208 hadError = false;
209
210 if (*s == '0') { // parse radix
211 s++;
212 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
213 s++;
214 radix = 16;
215 DigitsBegin = s;
216 s = SkipHexDigits(s);
217 if (s == ThisTokEnd) {
218 } else if (*s == '.') {
219 s++;
220 saw_period = true;
221 s = SkipHexDigits(s);
222 }
223 // A binary exponent can appear with or with a '.'. If dotted, the
224 // binary exponent is required.
225 if (*s == 'p' || *s == 'P') {
226 s++;
227 saw_exponent = true;
228 if (*s == '+' || *s == '-') s++; // sign
229 const char *first_non_digit = SkipDigits(s);
230 if (first_non_digit == s) {
231 Diag(TokLoc, diag::err_exponent_has_no_digits);
232 return;
233 } else {
234 s = first_non_digit;
235 }
236 } else if (saw_period) {
237 Diag(TokLoc, diag::err_hexconstant_requires_exponent);
238 return;
239 }
240 } else {
241 // For now, the radix is set to 8. If we discover that we have a
242 // floating point constant, the radix will change to 10. Octal floating
243 // point constants are not permitted (only decimal and hexadecimal).
244 radix = 8;
245 DigitsBegin = s;
246 s = SkipOctalDigits(s);
247 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000248 // Done.
249 } else if (isxdigit(*s)) {
250 Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1));
251 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000252 } else if (*s == '.') {
253 s++;
254 radix = 10;
255 saw_period = true;
256 s = SkipDigits(s);
257 }
258 if (*s == 'e' || *s == 'E') { // exponent
259 s++;
260 radix = 10;
261 saw_exponent = true;
262 if (*s == '+' || *s == '-') s++; // sign
263 const char *first_non_digit = SkipDigits(s);
264 if (first_non_digit == s) {
265 Diag(TokLoc, diag::err_exponent_has_no_digits);
266 return;
267 } else {
268 s = first_non_digit;
269 }
270 }
271 }
272 } else { // the first digit is non-zero
273 radix = 10;
274 s = SkipDigits(s);
275 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000276 // Done.
277 } else if (isxdigit(*s)) {
278 Diag(TokLoc, diag::err_invalid_decimal_digit, std::string(s, s+1));
279 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000280 } else if (*s == '.') {
281 s++;
282 saw_period = true;
283 s = SkipDigits(s);
284 }
285 if (*s == 'e' || *s == 'E') { // exponent
286 s++;
287 saw_exponent = true;
288 if (*s == '+' || *s == '-') s++; // sign
289 const char *first_non_digit = SkipDigits(s);
290 if (first_non_digit == s) {
291 Diag(TokLoc, diag::err_exponent_has_no_digits);
292 return;
293 } else {
294 s = first_non_digit;
295 }
296 }
297 }
298
299 SuffixBegin = s;
300
301 if (saw_period || saw_exponent) {
302 if (s < ThisTokEnd) { // parse size suffix (float, long double)
303 if (*s == 'f' || *s == 'F') {
304 saw_float_suffix = true;
305 s++;
306 } else if (*s == 'l' || *s == 'L') {
307 isLong = true;
308 s++;
309 }
310 if (s != ThisTokEnd) {
311 Diag(TokLoc, diag::err_invalid_suffix_float_constant,
312 std::string(SuffixBegin, ThisTokEnd));
313 return;
314 }
315 }
316 } else {
317 if (s < ThisTokEnd) {
318 // parse int suffix - they can appear in any order ("ul", "lu", "llu").
319 if (*s == 'u' || *s == 'U') {
320 s++;
321 isUnsigned = true; // unsigned
322
323 if ((s < ThisTokEnd) && (*s == 'l' || *s == 'L')) {
324 s++;
325 // handle "long long" type - l's need to be adjacent and same case.
326 if ((s < ThisTokEnd) && (*s == *(s-1))) {
327 isLongLong = true; // unsigned long long
328 s++;
329 } else {
330 isLong = true; // unsigned long
331 }
332 }
333 } else if (*s == 'l' || *s == 'L') {
334 s++;
335 // handle "long long" types - l's need to be adjacent and same case.
336 if ((s < ThisTokEnd) && (*s == *(s-1))) {
337 s++;
338 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
339 isUnsigned = true; // unsigned long long
340 s++;
341 } else {
342 isLongLong = true; // long long
343 }
344 } else { // handle "long" types
345 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
346 isUnsigned = true; // unsigned long
347 s++;
348 } else {
349 isLong = true; // long
350 }
351 }
352 }
353 if (s != ThisTokEnd) {
354 Diag(TokLoc, diag::err_invalid_suffix_integer_constant,
355 std::string(SuffixBegin, ThisTokEnd));
356 return;
357 }
358 }
359 }
360}
361
Chris Lattner5b743d32007-04-04 05:52:58 +0000362/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000363/// matches Val's input width. If there is an overflow, set Val to the low bits
364/// of the result and return true. Otherwise, return false.
Chris Lattner5b743d32007-04-04 05:52:58 +0000365bool NumericLiteralParser::GetIntegerValue(APInt &Val) {
366 Val = 0;
367 s = DigitsBegin;
368
Chris Lattner5b743d32007-04-04 05:52:58 +0000369 APInt RadixVal(Val.getBitWidth(), radix);
370 APInt CharVal(Val.getBitWidth(), 0);
371 APInt OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000372
373 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000374 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000375 unsigned C = HexDigitValue(*s++);
Chris Lattner5b743d32007-04-04 05:52:58 +0000376
377 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000378 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Chris Lattner5b743d32007-04-04 05:52:58 +0000379
380 CharVal = C;
381
Chris Lattner871b4e12007-04-04 06:36:34 +0000382 // Add the digit to the value in the appropriate radix. If adding in digits
383 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000384 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000385
386 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000387 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000388 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
389
390 OldVal = Val;
391 // Add value, did overflow occur on the value?
Chris Lattner5b743d32007-04-04 05:52:58 +0000392 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000393 OverflowOccurred |= Val.ult(OldVal);
394 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000395 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000396 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000397}
398
399
Steve Narofff2fb89e2007-03-13 20:29:44 +0000400void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID,
401 const std::string &M) {
402 PP.Diag(Loc, DiagID, M);
403 hadError = true;
404}
Steve Naroff4f88b312007-03-13 22:37:02 +0000405
Chris Lattner2f5add62007-04-05 06:57:15 +0000406
407CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
408 SourceLocation Loc, Preprocessor &PP) {
409 // At this point we know that the character matches the regex "L?'.*'".
410 HadError = false;
411 Value = 0;
412
413 // Determine if this is a wide character.
414 IsWide = begin[0] == 'L';
415 if (IsWide) ++begin;
416
417 // Skip over the entry quote.
418 assert(begin[0] == '\'' && "Invalid token lexed");
419 ++begin;
420
421 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
422 // size of "value".
423 assert(PP.getTargetInfo().getIntWidth(Loc) == 32 &&
424 "Assumes sizeof(int) == 4 for now");
425 // FIXME: This assumes that wchar_t is 32-bits for now.
426 assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 &&
427 "Assumes sizeof(wchar_t) == 4 for now");
428 // FIXME: This extensively assumes that 'char' is 8-bits.
429 assert(PP.getTargetInfo().getCharWidth(Loc) == 8 &&
430 "Assumes char is 8 bits");
431
432 bool isFirstChar = true;
433 bool isMultiChar = false;
434 while (begin[0] != '\'') {
435 unsigned ResultChar;
436 if (begin[0] != '\\') // If this is a normal character, consume it.
437 ResultChar = *begin++;
438 else // Otherwise, this is an escape character.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000439 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
Chris Lattner2f5add62007-04-05 06:57:15 +0000440
441 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
442 // implementation defined (C99 6.4.4.4p10).
443 if (!isFirstChar) {
444 // If this is the second character being processed, do special handling.
445 if (!isMultiChar) {
446 isMultiChar = true;
447
448 // Warn about discarding the top bits for multi-char wide-character
449 // constants (L'abcd').
450 if (IsWide)
451 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
452 }
453
454 if (IsWide) {
455 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
456 Value = 0;
457 } else {
458 // Narrow character literals act as though their value is concatenated
459 // in this implementation.
460 if (((Value << 8) >> 8) != Value)
461 PP.Diag(Loc, diag::warn_char_constant_too_large);
462 Value <<= 8;
463 }
464 }
465
466 Value += ResultChar;
467 isFirstChar = false;
468 }
469
470 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
471 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
472 // character constants are not sign extended in the this implementation:
473 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
474 if (!IsWide && !isMultiChar && (Value & 128) &&
475 PP.getTargetInfo().isCharSigned(Loc))
476 Value = (signed char)Value;
477}
478
479
Steve Naroff4f88b312007-03-13 22:37:02 +0000480/// string-literal: [C99 6.4.5]
481/// " [s-char-sequence] "
482/// L" [s-char-sequence] "
483/// s-char-sequence:
484/// s-char
485/// s-char-sequence s-char
486/// s-char:
487/// any source character except the double quote ",
488/// backslash \, or newline character
489/// escape-character
490/// universal-character-name
491/// escape-character: [C99 6.4.4.4]
492/// \ escape-code
493/// universal-character-name
494/// escape-code:
495/// character-escape-code
496/// octal-escape-code
497/// hex-escape-code
498/// character-escape-code: one of
499/// n t b r f v a
500/// \ ' " ?
501/// octal-escape-code:
502/// octal-digit
503/// octal-digit octal-digit
504/// octal-digit octal-digit octal-digit
505/// hex-escape-code:
506/// x hex-digit
507/// hex-escape-code hex-digit
508/// universal-character-name:
509/// \u hex-quad
510/// \U hex-quad hex-quad
511/// hex-quad:
512/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000513///
Steve Naroff4f88b312007-03-13 22:37:02 +0000514StringLiteralParser::
515StringLiteralParser(const LexerToken *StringToks, unsigned NumStringToks,
Chris Lattner2f5add62007-04-05 06:57:15 +0000516 Preprocessor &pp, TargetInfo &t)
517 : PP(pp), Target(t) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000518 // Scan all of the string portions, remember the max individual token length,
519 // computing a bound on the concatenated string length, and see whether any
520 // piece is a wide-string. If any of the string portions is a wide-string
521 // literal, the result is a wide-string literal [C99 6.4.5p4].
522 MaxTokenLength = StringToks[0].getLength();
523 SizeBound = StringToks[0].getLength()-2; // -2 for "".
524 AnyWide = StringToks[0].getKind() == tok::wide_string_literal;
525
Steve Narofff1e53692007-03-23 22:27:02 +0000526 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000527
528 // Implement Translation Phase #6: concatenation of string literals
529 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000530 for (unsigned i = 1; i != NumStringToks; ++i) {
531 // The string could be shorter than this if it needs cleaning, but this is a
532 // reasonable bound, which is all we need.
533 SizeBound += StringToks[i].getLength()-2; // -2 for "".
534
535 // Remember maximum string piece length.
536 if (StringToks[i].getLength() > MaxTokenLength)
537 MaxTokenLength = StringToks[i].getLength();
538
539 // Remember if we see any wide strings.
540 AnyWide |= StringToks[i].getKind() == tok::wide_string_literal;
541 }
542
543
544 // Include space for the null terminator.
545 ++SizeBound;
546
547 // TODO: K&R warning: "traditional C rejects string constant concatenation"
548
549 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
550 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
551 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000552 if (AnyWide) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000553 wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation());
Chris Lattner2f5add62007-04-05 06:57:15 +0000554 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
555 wchar_tByteWidth /= 8;
556 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000557
558 // The output buffer size needs to be large enough to hold wide characters.
559 // This is a worst-case assumption which basically corresponds to L"" "long".
560 if (AnyWide)
561 SizeBound *= wchar_tByteWidth;
562
563 // Size the temporary buffer to hold the result string data.
564 ResultBuf.resize(SizeBound);
565
566 // Likewise, but for each string piece.
567 SmallString<512> TokenBuf;
568 TokenBuf.resize(MaxTokenLength);
569
570 // Loop over all the strings, getting their spelling, and expanding them to
571 // wide strings as appropriate.
572 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
573
574 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
575 const char *ThisTokBuf = &TokenBuf[0];
576 // Get the spelling of the token, which eliminates trigraphs, etc. We know
577 // that ThisTokBuf points to a buffer that is big enough for the whole token
578 // and 'spelled' tokens can only shrink.
579 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
580 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
581
582 // TODO: Input character set mapping support.
583
584 // Skip L marker for wide strings.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000585 bool ThisIsWide = false;
586 if (ThisTokBuf[0] == 'L') {
587 ++ThisTokBuf;
588 ThisIsWide = true;
589 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000590
591 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
592 ++ThisTokBuf;
593
594 while (ThisTokBuf != ThisTokEnd) {
595 // Is this a span of non-escape characters?
596 if (ThisTokBuf[0] != '\\') {
597 const char *InStart = ThisTokBuf;
598 do {
599 ++ThisTokBuf;
600 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
601
602 // Copy the character span over.
603 unsigned Len = ThisTokBuf-InStart;
604 if (!AnyWide) {
605 memcpy(ResultPtr, InStart, Len);
606 ResultPtr += Len;
607 } else {
608 // Note: our internal rep of wide char tokens is always little-endian.
609 for (; Len; --Len, ++InStart) {
610 *ResultPtr++ = InStart[0];
611 // Add zeros at the end.
612 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
613 *ResultPtr++ = 0;
614 }
615 }
616 continue;
617 }
618
Chris Lattner2f5add62007-04-05 06:57:15 +0000619 // Otherwise, this is an escape character. Process it.
620 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +0000621 StringToks[i].getLocation(),
622 ThisIsWide, PP);
Steve Naroff4f88b312007-03-13 22:37:02 +0000623
624 // Note: our internal rep of wide char tokens is always little-endian.
625 *ResultPtr++ = ResultChar & 0xFF;
626
627 if (AnyWide) {
628 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
629 *ResultPtr++ = ResultChar >> i*8;
630 }
631 }
632 }
633
634 // Add zero terminator.
635 *ResultPtr = 0;
636 if (AnyWide) {
637 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
638 *ResultPtr++ = 0;
639 }
640}