blob: dd4ac3d524b31b9fbe7b6ab53dc6c9869746adec [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) {
Chris Lattnerde12ae22007-06-08 22:42:30 +0000218 // Done.
Steve Naroff09ef4742007-03-09 23:16:33 +0000219 } else if (*s == '.') {
220 s++;
221 saw_period = true;
222 s = SkipHexDigits(s);
223 }
224 // A binary exponent can appear with or with a '.'. If dotted, the
225 // binary exponent is required.
226 if (*s == 'p' || *s == 'P') {
227 s++;
228 saw_exponent = true;
229 if (*s == '+' || *s == '-') s++; // sign
230 const char *first_non_digit = SkipDigits(s);
231 if (first_non_digit == s) {
232 Diag(TokLoc, diag::err_exponent_has_no_digits);
233 return;
234 } else {
235 s = first_non_digit;
236 }
237 } else if (saw_period) {
238 Diag(TokLoc, diag::err_hexconstant_requires_exponent);
239 return;
240 }
Chris Lattnerde12ae22007-06-08 22:42:30 +0000241 } else if (*s == 'b' || *s == 'B') {
242 // 0b101010 is a GCC extension.
243 ++s;
244 radix = 2;
245 DigitsBegin = s;
246 s = SkipBinaryDigits(s);
247 if (s == ThisTokEnd) {
248 // Done.
249 } else if (isxdigit(*s)) {
250 Diag(TokLoc, diag::err_invalid_binary_digit, std::string(s, s+1));
251 return;
252 }
253 PP.Diag(TokLoc, diag::ext_binary_literal);
Steve Naroff09ef4742007-03-09 23:16:33 +0000254 } else {
255 // For now, the radix is set to 8. If we discover that we have a
256 // floating point constant, the radix will change to 10. Octal floating
257 // point constants are not permitted (only decimal and hexadecimal).
258 radix = 8;
259 DigitsBegin = s;
260 s = SkipOctalDigits(s);
261 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000262 // Done.
263 } else if (isxdigit(*s)) {
264 Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1));
265 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000266 } else if (*s == '.') {
267 s++;
268 radix = 10;
269 saw_period = true;
270 s = SkipDigits(s);
271 }
272 if (*s == 'e' || *s == 'E') { // exponent
273 s++;
274 radix = 10;
275 saw_exponent = true;
276 if (*s == '+' || *s == '-') s++; // sign
277 const char *first_non_digit = SkipDigits(s);
278 if (first_non_digit == s) {
279 Diag(TokLoc, diag::err_exponent_has_no_digits);
280 return;
281 } else {
282 s = first_non_digit;
283 }
284 }
285 }
286 } else { // the first digit is non-zero
287 radix = 10;
288 s = SkipDigits(s);
289 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000290 // Done.
291 } else if (isxdigit(*s)) {
292 Diag(TokLoc, diag::err_invalid_decimal_digit, std::string(s, s+1));
293 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000294 } else if (*s == '.') {
295 s++;
296 saw_period = true;
297 s = SkipDigits(s);
298 }
299 if (*s == 'e' || *s == 'E') { // exponent
300 s++;
301 saw_exponent = true;
302 if (*s == '+' || *s == '-') s++; // sign
303 const char *first_non_digit = SkipDigits(s);
304 if (first_non_digit == s) {
305 Diag(TokLoc, diag::err_exponent_has_no_digits);
306 return;
307 } else {
308 s = first_non_digit;
309 }
310 }
311 }
312
313 SuffixBegin = s;
314
315 if (saw_period || saw_exponent) {
316 if (s < ThisTokEnd) { // parse size suffix (float, long double)
317 if (*s == 'f' || *s == 'F') {
318 saw_float_suffix = true;
319 s++;
320 } else if (*s == 'l' || *s == 'L') {
321 isLong = true;
322 s++;
323 }
324 if (s != ThisTokEnd) {
325 Diag(TokLoc, diag::err_invalid_suffix_float_constant,
326 std::string(SuffixBegin, ThisTokEnd));
327 return;
328 }
329 }
330 } else {
331 if (s < ThisTokEnd) {
332 // parse int suffix - they can appear in any order ("ul", "lu", "llu").
333 if (*s == 'u' || *s == 'U') {
334 s++;
335 isUnsigned = true; // unsigned
336
337 if ((s < ThisTokEnd) && (*s == 'l' || *s == 'L')) {
338 s++;
339 // handle "long long" type - l's need to be adjacent and same case.
340 if ((s < ThisTokEnd) && (*s == *(s-1))) {
341 isLongLong = true; // unsigned long long
342 s++;
343 } else {
344 isLong = true; // unsigned long
345 }
346 }
347 } else if (*s == 'l' || *s == 'L') {
348 s++;
349 // handle "long long" types - l's need to be adjacent and same case.
350 if ((s < ThisTokEnd) && (*s == *(s-1))) {
351 s++;
352 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
353 isUnsigned = true; // unsigned long long
354 s++;
355 } else {
356 isLongLong = true; // long long
357 }
358 } else { // handle "long" types
359 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
360 isUnsigned = true; // unsigned long
361 s++;
362 } else {
363 isLong = true; // long
364 }
365 }
366 }
367 if (s != ThisTokEnd) {
368 Diag(TokLoc, diag::err_invalid_suffix_integer_constant,
369 std::string(SuffixBegin, ThisTokEnd));
370 return;
371 }
372 }
373 }
374}
375
Chris Lattner5b743d32007-04-04 05:52:58 +0000376/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000377/// matches Val's input width. If there is an overflow, set Val to the low bits
378/// of the result and return true. Otherwise, return false.
Chris Lattner5b743d32007-04-04 05:52:58 +0000379bool NumericLiteralParser::GetIntegerValue(APInt &Val) {
380 Val = 0;
381 s = DigitsBegin;
382
Chris Lattner5b743d32007-04-04 05:52:58 +0000383 APInt RadixVal(Val.getBitWidth(), radix);
384 APInt CharVal(Val.getBitWidth(), 0);
385 APInt OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000386
387 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000388 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000389 unsigned C = HexDigitValue(*s++);
Chris Lattner5b743d32007-04-04 05:52:58 +0000390
391 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000392 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Chris Lattner5b743d32007-04-04 05:52:58 +0000393
394 CharVal = C;
395
Chris Lattner871b4e12007-04-04 06:36:34 +0000396 // Add the digit to the value in the appropriate radix. If adding in digits
397 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000398 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000399
400 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000401 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000402 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
403
404 OldVal = Val;
405 // Add value, did overflow occur on the value?
Chris Lattner5b743d32007-04-04 05:52:58 +0000406 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000407 OverflowOccurred |= Val.ult(OldVal);
408 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000409 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000410 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000411}
412
413
Steve Narofff2fb89e2007-03-13 20:29:44 +0000414void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID,
415 const std::string &M) {
416 PP.Diag(Loc, DiagID, M);
417 hadError = true;
418}
Steve Naroff4f88b312007-03-13 22:37:02 +0000419
Chris Lattner2f5add62007-04-05 06:57:15 +0000420
421CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
422 SourceLocation Loc, Preprocessor &PP) {
423 // At this point we know that the character matches the regex "L?'.*'".
424 HadError = false;
425 Value = 0;
426
427 // Determine if this is a wide character.
428 IsWide = begin[0] == 'L';
429 if (IsWide) ++begin;
430
431 // Skip over the entry quote.
432 assert(begin[0] == '\'' && "Invalid token lexed");
433 ++begin;
434
435 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
436 // size of "value".
437 assert(PP.getTargetInfo().getIntWidth(Loc) == 32 &&
438 "Assumes sizeof(int) == 4 for now");
439 // FIXME: This assumes that wchar_t is 32-bits for now.
440 assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 &&
441 "Assumes sizeof(wchar_t) == 4 for now");
442 // FIXME: This extensively assumes that 'char' is 8-bits.
443 assert(PP.getTargetInfo().getCharWidth(Loc) == 8 &&
444 "Assumes char is 8 bits");
445
446 bool isFirstChar = true;
447 bool isMultiChar = false;
448 while (begin[0] != '\'') {
449 unsigned ResultChar;
450 if (begin[0] != '\\') // If this is a normal character, consume it.
451 ResultChar = *begin++;
452 else // Otherwise, this is an escape character.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000453 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
Chris Lattner2f5add62007-04-05 06:57:15 +0000454
455 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
456 // implementation defined (C99 6.4.4.4p10).
457 if (!isFirstChar) {
458 // If this is the second character being processed, do special handling.
459 if (!isMultiChar) {
460 isMultiChar = true;
461
462 // Warn about discarding the top bits for multi-char wide-character
463 // constants (L'abcd').
464 if (IsWide)
465 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
466 }
467
468 if (IsWide) {
469 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
470 Value = 0;
471 } else {
472 // Narrow character literals act as though their value is concatenated
473 // in this implementation.
474 if (((Value << 8) >> 8) != Value)
475 PP.Diag(Loc, diag::warn_char_constant_too_large);
476 Value <<= 8;
477 }
478 }
479
480 Value += ResultChar;
481 isFirstChar = false;
482 }
483
484 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
485 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
486 // character constants are not sign extended in the this implementation:
487 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
488 if (!IsWide && !isMultiChar && (Value & 128) &&
489 PP.getTargetInfo().isCharSigned(Loc))
490 Value = (signed char)Value;
491}
492
493
Steve Naroff4f88b312007-03-13 22:37:02 +0000494/// string-literal: [C99 6.4.5]
495/// " [s-char-sequence] "
496/// L" [s-char-sequence] "
497/// s-char-sequence:
498/// s-char
499/// s-char-sequence s-char
500/// s-char:
501/// any source character except the double quote ",
502/// backslash \, or newline character
503/// escape-character
504/// universal-character-name
505/// escape-character: [C99 6.4.4.4]
506/// \ escape-code
507/// universal-character-name
508/// escape-code:
509/// character-escape-code
510/// octal-escape-code
511/// hex-escape-code
512/// character-escape-code: one of
513/// n t b r f v a
514/// \ ' " ?
515/// octal-escape-code:
516/// octal-digit
517/// octal-digit octal-digit
518/// octal-digit octal-digit octal-digit
519/// hex-escape-code:
520/// x hex-digit
521/// hex-escape-code hex-digit
522/// universal-character-name:
523/// \u hex-quad
524/// \U hex-quad hex-quad
525/// hex-quad:
526/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000527///
Steve Naroff4f88b312007-03-13 22:37:02 +0000528StringLiteralParser::
529StringLiteralParser(const LexerToken *StringToks, unsigned NumStringToks,
Chris Lattner2f5add62007-04-05 06:57:15 +0000530 Preprocessor &pp, TargetInfo &t)
531 : PP(pp), Target(t) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000532 // Scan all of the string portions, remember the max individual token length,
533 // computing a bound on the concatenated string length, and see whether any
534 // piece is a wide-string. If any of the string portions is a wide-string
535 // literal, the result is a wide-string literal [C99 6.4.5p4].
536 MaxTokenLength = StringToks[0].getLength();
537 SizeBound = StringToks[0].getLength()-2; // -2 for "".
538 AnyWide = StringToks[0].getKind() == tok::wide_string_literal;
539
Steve Narofff1e53692007-03-23 22:27:02 +0000540 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000541
542 // Implement Translation Phase #6: concatenation of string literals
543 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000544 for (unsigned i = 1; i != NumStringToks; ++i) {
545 // The string could be shorter than this if it needs cleaning, but this is a
546 // reasonable bound, which is all we need.
547 SizeBound += StringToks[i].getLength()-2; // -2 for "".
548
549 // Remember maximum string piece length.
550 if (StringToks[i].getLength() > MaxTokenLength)
551 MaxTokenLength = StringToks[i].getLength();
552
553 // Remember if we see any wide strings.
554 AnyWide |= StringToks[i].getKind() == tok::wide_string_literal;
555 }
556
557
558 // Include space for the null terminator.
559 ++SizeBound;
560
561 // TODO: K&R warning: "traditional C rejects string constant concatenation"
562
563 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
564 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
565 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000566 if (AnyWide) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000567 wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation());
Chris Lattner2f5add62007-04-05 06:57:15 +0000568 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
569 wchar_tByteWidth /= 8;
570 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000571
572 // The output buffer size needs to be large enough to hold wide characters.
573 // This is a worst-case assumption which basically corresponds to L"" "long".
574 if (AnyWide)
575 SizeBound *= wchar_tByteWidth;
576
577 // Size the temporary buffer to hold the result string data.
578 ResultBuf.resize(SizeBound);
579
580 // Likewise, but for each string piece.
581 SmallString<512> TokenBuf;
582 TokenBuf.resize(MaxTokenLength);
583
584 // Loop over all the strings, getting their spelling, and expanding them to
585 // wide strings as appropriate.
586 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
587
588 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
589 const char *ThisTokBuf = &TokenBuf[0];
590 // Get the spelling of the token, which eliminates trigraphs, etc. We know
591 // that ThisTokBuf points to a buffer that is big enough for the whole token
592 // and 'spelled' tokens can only shrink.
593 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
594 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
595
596 // TODO: Input character set mapping support.
597
598 // Skip L marker for wide strings.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000599 bool ThisIsWide = false;
600 if (ThisTokBuf[0] == 'L') {
601 ++ThisTokBuf;
602 ThisIsWide = true;
603 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000604
605 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
606 ++ThisTokBuf;
607
608 while (ThisTokBuf != ThisTokEnd) {
609 // Is this a span of non-escape characters?
610 if (ThisTokBuf[0] != '\\') {
611 const char *InStart = ThisTokBuf;
612 do {
613 ++ThisTokBuf;
614 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
615
616 // Copy the character span over.
617 unsigned Len = ThisTokBuf-InStart;
618 if (!AnyWide) {
619 memcpy(ResultPtr, InStart, Len);
620 ResultPtr += Len;
621 } else {
622 // Note: our internal rep of wide char tokens is always little-endian.
623 for (; Len; --Len, ++InStart) {
624 *ResultPtr++ = InStart[0];
625 // Add zeros at the end.
626 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
627 *ResultPtr++ = 0;
628 }
629 }
630 continue;
631 }
632
Chris Lattner2f5add62007-04-05 06:57:15 +0000633 // Otherwise, this is an escape character. Process it.
634 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +0000635 StringToks[i].getLocation(),
636 ThisIsWide, PP);
Steve Naroff4f88b312007-03-13 22:37:02 +0000637
638 // Note: our internal rep of wide char tokens is always little-endian.
639 *ResultPtr++ = ResultChar & 0xFF;
640
641 if (AnyWide) {
642 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
643 *ResultPtr++ = ResultChar >> i*8;
644 }
645 }
646 }
647
648 // Add zero terminator.
649 *ResultPtr = 0;
650 if (AnyWide) {
651 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
652 *ResultPtr++ = 0;
653 }
654}