blob: 509230eacf1124f85c4dd11bec7646a717c91583 [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 Lattnerc10adde2007-05-20 05:00:58 +000086 bool Overflow = false;
87 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
88 int CharVal = HexDigitValue(ThisTokBuf[0]);
89 if (CharVal == -1) break;
90 Overflow |= ResultChar & 0xF0000000; // About to shift out a digit?
91 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);
Chris Lattner2f5add62007-04-05 06:57:15 +0000106 break;
Chris Lattnerc10adde2007-05-20 05:00:58 +0000107 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000108 case '0': case '1': case '2': case '3':
109 case '4': case '5': case '6': case '7':
110 // Octal escapes.
111 // FIXME: warn_octal_escape_too_large. '\012345'
112 assert(0 && "octal escape: unimp!");
113 break;
114
115 // Otherwise, these are not valid escapes.
116 case '(': case '{': case '[': case '%':
117 // GCC accepts these as extensions. We warn about them as such though.
118 if (!PP.getLangOptions().NoExtensions) {
119 PP.Diag(Loc, diag::ext_nonstandard_escape,
120 std::string()+(char)ResultChar);
121 break;
122 }
123 // FALL THROUGH.
124 default:
125 if (isgraph(ThisTokBuf[0])) {
126 PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar);
127 } else {
128 PP.Diag(Loc, diag::ext_unknown_escape, "x"+utohexstr(ResultChar));
129 }
130 break;
131 }
132
133 return ResultChar;
134}
135
136
137
138
Steve Naroff09ef4742007-03-09 23:16:33 +0000139/// integer-constant: [C99 6.4.4.1]
140/// decimal-constant integer-suffix
141/// octal-constant integer-suffix
142/// hexadecimal-constant integer-suffix
143/// decimal-constant:
144/// nonzero-digit
145/// decimal-constant digit
146/// octal-constant:
147/// 0
148/// octal-constant octal-digit
149/// hexadecimal-constant:
150/// hexadecimal-prefix hexadecimal-digit
151/// hexadecimal-constant hexadecimal-digit
152/// hexadecimal-prefix: one of
153/// 0x 0X
154/// integer-suffix:
155/// unsigned-suffix [long-suffix]
156/// unsigned-suffix [long-long-suffix]
157/// long-suffix [unsigned-suffix]
158/// long-long-suffix [unsigned-sufix]
159/// nonzero-digit:
160/// 1 2 3 4 5 6 7 8 9
161/// octal-digit:
162/// 0 1 2 3 4 5 6 7
163/// hexadecimal-digit:
164/// 0 1 2 3 4 5 6 7 8 9
165/// a b c d e f
166/// A B C D E F
167/// unsigned-suffix: one of
168/// u U
169/// long-suffix: one of
170/// l L
171/// long-long-suffix: one of
172/// ll LL
173///
174/// floating-constant: [C99 6.4.4.2]
175/// TODO: add rules...
176///
177
178NumericLiteralParser::
179NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000180 SourceLocation TokLoc, Preprocessor &pp)
181 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000182 s = DigitsBegin = begin;
183 saw_exponent = false;
184 saw_period = false;
185 saw_float_suffix = false;
186 isLong = false;
187 isUnsigned = false;
188 isLongLong = false;
189 hadError = false;
190
191 if (*s == '0') { // parse radix
192 s++;
193 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
194 s++;
195 radix = 16;
196 DigitsBegin = s;
197 s = SkipHexDigits(s);
198 if (s == ThisTokEnd) {
199 } else if (*s == '.') {
200 s++;
201 saw_period = true;
202 s = SkipHexDigits(s);
203 }
204 // A binary exponent can appear with or with a '.'. If dotted, the
205 // binary exponent is required.
206 if (*s == 'p' || *s == 'P') {
207 s++;
208 saw_exponent = true;
209 if (*s == '+' || *s == '-') s++; // sign
210 const char *first_non_digit = SkipDigits(s);
211 if (first_non_digit == s) {
212 Diag(TokLoc, diag::err_exponent_has_no_digits);
213 return;
214 } else {
215 s = first_non_digit;
216 }
217 } else if (saw_period) {
218 Diag(TokLoc, diag::err_hexconstant_requires_exponent);
219 return;
220 }
221 } else {
222 // For now, the radix is set to 8. If we discover that we have a
223 // floating point constant, the radix will change to 10. Octal floating
224 // point constants are not permitted (only decimal and hexadecimal).
225 radix = 8;
226 DigitsBegin = s;
227 s = SkipOctalDigits(s);
228 if (s == ThisTokEnd) {
229 } else if (*s == '.') {
230 s++;
231 radix = 10;
232 saw_period = true;
233 s = SkipDigits(s);
234 }
235 if (*s == 'e' || *s == 'E') { // exponent
236 s++;
237 radix = 10;
238 saw_exponent = true;
239 if (*s == '+' || *s == '-') s++; // sign
240 const char *first_non_digit = SkipDigits(s);
241 if (first_non_digit == s) {
242 Diag(TokLoc, diag::err_exponent_has_no_digits);
243 return;
244 } else {
245 s = first_non_digit;
246 }
247 }
248 }
249 } else { // the first digit is non-zero
250 radix = 10;
251 s = SkipDigits(s);
252 if (s == ThisTokEnd) {
253 } else if (*s == '.') {
254 s++;
255 saw_period = true;
256 s = SkipDigits(s);
257 }
258 if (*s == 'e' || *s == 'E') { // exponent
259 s++;
260 saw_exponent = true;
261 if (*s == '+' || *s == '-') s++; // sign
262 const char *first_non_digit = SkipDigits(s);
263 if (first_non_digit == s) {
264 Diag(TokLoc, diag::err_exponent_has_no_digits);
265 return;
266 } else {
267 s = first_non_digit;
268 }
269 }
270 }
271
272 SuffixBegin = s;
273
274 if (saw_period || saw_exponent) {
275 if (s < ThisTokEnd) { // parse size suffix (float, long double)
276 if (*s == 'f' || *s == 'F') {
277 saw_float_suffix = true;
278 s++;
279 } else if (*s == 'l' || *s == 'L') {
280 isLong = true;
281 s++;
282 }
283 if (s != ThisTokEnd) {
284 Diag(TokLoc, diag::err_invalid_suffix_float_constant,
285 std::string(SuffixBegin, ThisTokEnd));
286 return;
287 }
288 }
289 } else {
290 if (s < ThisTokEnd) {
291 // parse int suffix - they can appear in any order ("ul", "lu", "llu").
292 if (*s == 'u' || *s == 'U') {
293 s++;
294 isUnsigned = true; // unsigned
295
296 if ((s < ThisTokEnd) && (*s == 'l' || *s == 'L')) {
297 s++;
298 // handle "long long" type - l's need to be adjacent and same case.
299 if ((s < ThisTokEnd) && (*s == *(s-1))) {
300 isLongLong = true; // unsigned long long
301 s++;
302 } else {
303 isLong = true; // unsigned long
304 }
305 }
306 } else if (*s == 'l' || *s == 'L') {
307 s++;
308 // handle "long long" types - l's need to be adjacent and same case.
309 if ((s < ThisTokEnd) && (*s == *(s-1))) {
310 s++;
311 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
312 isUnsigned = true; // unsigned long long
313 s++;
314 } else {
315 isLongLong = true; // long long
316 }
317 } else { // handle "long" types
318 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
319 isUnsigned = true; // unsigned long
320 s++;
321 } else {
322 isLong = true; // long
323 }
324 }
325 }
326 if (s != ThisTokEnd) {
327 Diag(TokLoc, diag::err_invalid_suffix_integer_constant,
328 std::string(SuffixBegin, ThisTokEnd));
329 return;
330 }
331 }
332 }
333}
334
Steve Naroff451d8f162007-03-12 23:22:38 +0000335bool NumericLiteralParser::GetIntegerValue(uintmax_t &val) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000336 uintmax_t max_value = UINTMAX_MAX / radix;
Chris Lattner5b743d32007-04-04 05:52:58 +0000337 unsigned max_digit = UINTMAX_MAX % radix;
Steve Naroff09ef4742007-03-09 23:16:33 +0000338
339 val = 0;
Steve Naroff451d8f162007-03-12 23:22:38 +0000340 s = DigitsBegin;
341 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000342 unsigned C = HexDigitValue(*s++);
Steve Naroff451d8f162007-03-12 23:22:38 +0000343
Chris Lattner5b743d32007-04-04 05:52:58 +0000344 if (val > max_value || (val == max_value && C > max_digit)) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000345 return false; // Overflow!
346 } else {
347 val *= radix;
Chris Lattner5b743d32007-04-04 05:52:58 +0000348 val += C;
Steve Naroff451d8f162007-03-12 23:22:38 +0000349 }
350 }
351 return true;
352}
353
354bool NumericLiteralParser::GetIntegerValue(int &val) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000355 intmax_t max_value = INT_MAX / radix;
Chris Lattner5b743d32007-04-04 05:52:58 +0000356 unsigned max_digit = INT_MAX % radix;
Steve Naroff451d8f162007-03-12 23:22:38 +0000357
358 val = 0;
Steve Naroff09ef4742007-03-09 23:16:33 +0000359 s = DigitsBegin;
360 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000361 unsigned C = HexDigitValue(*s++);
Steve Naroff09ef4742007-03-09 23:16:33 +0000362
Chris Lattner5b743d32007-04-04 05:52:58 +0000363 if (val > max_value || (val == max_value && C > max_digit)) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000364 return false; // Overflow!
365 } else {
366 val *= radix;
Chris Lattner5b743d32007-04-04 05:52:58 +0000367 val += C;
Steve Naroff09ef4742007-03-09 23:16:33 +0000368 }
369 }
370 return true;
371}
Steve Narofff2fb89e2007-03-13 20:29:44 +0000372
Chris Lattner5b743d32007-04-04 05:52:58 +0000373/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000374/// matches Val's input width. If there is an overflow, set Val to the low bits
375/// of the result and return true. Otherwise, return false.
Chris Lattner5b743d32007-04-04 05:52:58 +0000376bool NumericLiteralParser::GetIntegerValue(APInt &Val) {
377 Val = 0;
378 s = DigitsBegin;
379
Chris Lattner5b743d32007-04-04 05:52:58 +0000380 APInt RadixVal(Val.getBitWidth(), radix);
381 APInt CharVal(Val.getBitWidth(), 0);
382 APInt OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000383
384 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000385 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000386 unsigned C = HexDigitValue(*s++);
Chris Lattner5b743d32007-04-04 05:52:58 +0000387
388 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000389 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Chris Lattner5b743d32007-04-04 05:52:58 +0000390
391 CharVal = C;
392
Chris Lattner871b4e12007-04-04 06:36:34 +0000393 // Add the digit to the value in the appropriate radix. If adding in digits
394 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000395 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000396
397 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000398 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000399 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
400
401 OldVal = Val;
402 // Add value, did overflow occur on the value?
Chris Lattner5b743d32007-04-04 05:52:58 +0000403 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000404 OverflowOccurred |= Val.ult(OldVal);
405 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000406 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000407 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000408}
409
410
Steve Narofff2fb89e2007-03-13 20:29:44 +0000411void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID,
412 const std::string &M) {
413 PP.Diag(Loc, DiagID, M);
414 hadError = true;
415}
Steve Naroff4f88b312007-03-13 22:37:02 +0000416
Chris Lattner2f5add62007-04-05 06:57:15 +0000417
418CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
419 SourceLocation Loc, Preprocessor &PP) {
420 // At this point we know that the character matches the regex "L?'.*'".
421 HadError = false;
422 Value = 0;
423
424 // Determine if this is a wide character.
425 IsWide = begin[0] == 'L';
426 if (IsWide) ++begin;
427
428 // Skip over the entry quote.
429 assert(begin[0] == '\'' && "Invalid token lexed");
430 ++begin;
431
432 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
433 // size of "value".
434 assert(PP.getTargetInfo().getIntWidth(Loc) == 32 &&
435 "Assumes sizeof(int) == 4 for now");
436 // FIXME: This assumes that wchar_t is 32-bits for now.
437 assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 &&
438 "Assumes sizeof(wchar_t) == 4 for now");
439 // FIXME: This extensively assumes that 'char' is 8-bits.
440 assert(PP.getTargetInfo().getCharWidth(Loc) == 8 &&
441 "Assumes char is 8 bits");
442
443 bool isFirstChar = true;
444 bool isMultiChar = false;
445 while (begin[0] != '\'') {
446 unsigned ResultChar;
447 if (begin[0] != '\\') // If this is a normal character, consume it.
448 ResultChar = *begin++;
449 else // Otherwise, this is an escape character.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000450 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
Chris Lattner2f5add62007-04-05 06:57:15 +0000451
452 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
453 // implementation defined (C99 6.4.4.4p10).
454 if (!isFirstChar) {
455 // If this is the second character being processed, do special handling.
456 if (!isMultiChar) {
457 isMultiChar = true;
458
459 // Warn about discarding the top bits for multi-char wide-character
460 // constants (L'abcd').
461 if (IsWide)
462 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
463 }
464
465 if (IsWide) {
466 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
467 Value = 0;
468 } else {
469 // Narrow character literals act as though their value is concatenated
470 // in this implementation.
471 if (((Value << 8) >> 8) != Value)
472 PP.Diag(Loc, diag::warn_char_constant_too_large);
473 Value <<= 8;
474 }
475 }
476
477 Value += ResultChar;
478 isFirstChar = false;
479 }
480
481 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
482 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
483 // character constants are not sign extended in the this implementation:
484 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
485 if (!IsWide && !isMultiChar && (Value & 128) &&
486 PP.getTargetInfo().isCharSigned(Loc))
487 Value = (signed char)Value;
488}
489
490
Steve Naroff4f88b312007-03-13 22:37:02 +0000491/// string-literal: [C99 6.4.5]
492/// " [s-char-sequence] "
493/// L" [s-char-sequence] "
494/// s-char-sequence:
495/// s-char
496/// s-char-sequence s-char
497/// s-char:
498/// any source character except the double quote ",
499/// backslash \, or newline character
500/// escape-character
501/// universal-character-name
502/// escape-character: [C99 6.4.4.4]
503/// \ escape-code
504/// universal-character-name
505/// escape-code:
506/// character-escape-code
507/// octal-escape-code
508/// hex-escape-code
509/// character-escape-code: one of
510/// n t b r f v a
511/// \ ' " ?
512/// octal-escape-code:
513/// octal-digit
514/// octal-digit octal-digit
515/// octal-digit octal-digit octal-digit
516/// hex-escape-code:
517/// x hex-digit
518/// hex-escape-code hex-digit
519/// universal-character-name:
520/// \u hex-quad
521/// \U hex-quad hex-quad
522/// hex-quad:
523/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000524///
Steve Naroff4f88b312007-03-13 22:37:02 +0000525StringLiteralParser::
526StringLiteralParser(const LexerToken *StringToks, unsigned NumStringToks,
Chris Lattner2f5add62007-04-05 06:57:15 +0000527 Preprocessor &pp, TargetInfo &t)
528 : PP(pp), Target(t) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000529 // Scan all of the string portions, remember the max individual token length,
530 // computing a bound on the concatenated string length, and see whether any
531 // piece is a wide-string. If any of the string portions is a wide-string
532 // literal, the result is a wide-string literal [C99 6.4.5p4].
533 MaxTokenLength = StringToks[0].getLength();
534 SizeBound = StringToks[0].getLength()-2; // -2 for "".
535 AnyWide = StringToks[0].getKind() == tok::wide_string_literal;
536
Steve Narofff1e53692007-03-23 22:27:02 +0000537 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000538
539 // Implement Translation Phase #6: concatenation of string literals
540 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000541 for (unsigned i = 1; i != NumStringToks; ++i) {
542 // The string could be shorter than this if it needs cleaning, but this is a
543 // reasonable bound, which is all we need.
544 SizeBound += StringToks[i].getLength()-2; // -2 for "".
545
546 // Remember maximum string piece length.
547 if (StringToks[i].getLength() > MaxTokenLength)
548 MaxTokenLength = StringToks[i].getLength();
549
550 // Remember if we see any wide strings.
551 AnyWide |= StringToks[i].getKind() == tok::wide_string_literal;
552 }
553
554
555 // Include space for the null terminator.
556 ++SizeBound;
557
558 // TODO: K&R warning: "traditional C rejects string constant concatenation"
559
560 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
561 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
562 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000563 if (AnyWide) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000564 wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation());
Chris Lattner2f5add62007-04-05 06:57:15 +0000565 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
566 wchar_tByteWidth /= 8;
567 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000568
569 // The output buffer size needs to be large enough to hold wide characters.
570 // This is a worst-case assumption which basically corresponds to L"" "long".
571 if (AnyWide)
572 SizeBound *= wchar_tByteWidth;
573
574 // Size the temporary buffer to hold the result string data.
575 ResultBuf.resize(SizeBound);
576
577 // Likewise, but for each string piece.
578 SmallString<512> TokenBuf;
579 TokenBuf.resize(MaxTokenLength);
580
581 // Loop over all the strings, getting their spelling, and expanding them to
582 // wide strings as appropriate.
583 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
584
585 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
586 const char *ThisTokBuf = &TokenBuf[0];
587 // Get the spelling of the token, which eliminates trigraphs, etc. We know
588 // that ThisTokBuf points to a buffer that is big enough for the whole token
589 // and 'spelled' tokens can only shrink.
590 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
591 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
592
593 // TODO: Input character set mapping support.
594
595 // Skip L marker for wide strings.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000596 bool ThisIsWide = false;
597 if (ThisTokBuf[0] == 'L') {
598 ++ThisTokBuf;
599 ThisIsWide = true;
600 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000601
602 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
603 ++ThisTokBuf;
604
605 while (ThisTokBuf != ThisTokEnd) {
606 // Is this a span of non-escape characters?
607 if (ThisTokBuf[0] != '\\') {
608 const char *InStart = ThisTokBuf;
609 do {
610 ++ThisTokBuf;
611 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
612
613 // Copy the character span over.
614 unsigned Len = ThisTokBuf-InStart;
615 if (!AnyWide) {
616 memcpy(ResultPtr, InStart, Len);
617 ResultPtr += Len;
618 } else {
619 // Note: our internal rep of wide char tokens is always little-endian.
620 for (; Len; --Len, ++InStart) {
621 *ResultPtr++ = InStart[0];
622 // Add zeros at the end.
623 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
624 *ResultPtr++ = 0;
625 }
626 }
627 continue;
628 }
629
Chris Lattner2f5add62007-04-05 06:57:15 +0000630 // Otherwise, this is an escape character. Process it.
631 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +0000632 StringToks[i].getLocation(),
633 ThisIsWide, PP);
Steve Naroff4f88b312007-03-13 22:37:02 +0000634
635 // Note: our internal rep of wide char tokens is always little-endian.
636 *ResultPtr++ = ResultChar & 0xFF;
637
638 if (AnyWide) {
639 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
640 *ResultPtr++ = ResultChar >> i*8;
641 }
642 }
643 }
644
645 // Add zero terminator.
646 *ResultPtr = 0;
647 if (AnyWide) {
648 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
649 *ResultPtr++ = 0;
650 }
651}