blob: 5b13487f502336530cf1adc4024d399bb3bf2282 [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 Lattner3f4b6e32007-06-09 06:20:47 +0000112 --ThisTokBuf;
Chris Lattner812eda82007-05-20 05:17:04 +0000113 ResultChar = 0;
114
115 // Octal escapes are a series of octal digits with maximum length 3.
116 // "\0123" is a two digit sequence equal to "\012" "3".
117 unsigned NumDigits = 0;
118 do {
119 ResultChar <<= 3;
120 ResultChar |= *ThisTokBuf++ - '0';
121 ++NumDigits;
122 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
123 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
124
125 // Check for overflow. Reject '\777', but not L'\777'.
126 unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc)
127 : PP.getTargetInfo().getCharWidth(Loc);
128 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
129 PP.Diag(Loc, diag::warn_octal_escape_too_large);
130 ResultChar &= ~0U >> (32-CharWidth);
131 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000132 break;
Chris Lattner812eda82007-05-20 05:17:04 +0000133 }
Chris Lattner2f5add62007-04-05 06:57:15 +0000134
135 // Otherwise, these are not valid escapes.
136 case '(': case '{': case '[': case '%':
137 // GCC accepts these as extensions. We warn about them as such though.
138 if (!PP.getLangOptions().NoExtensions) {
139 PP.Diag(Loc, diag::ext_nonstandard_escape,
140 std::string()+(char)ResultChar);
141 break;
142 }
143 // FALL THROUGH.
144 default:
145 if (isgraph(ThisTokBuf[0])) {
146 PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar);
147 } else {
148 PP.Diag(Loc, diag::ext_unknown_escape, "x"+utohexstr(ResultChar));
149 }
150 break;
151 }
152
153 return ResultChar;
154}
155
156
157
158
Steve Naroff09ef4742007-03-09 23:16:33 +0000159/// integer-constant: [C99 6.4.4.1]
160/// decimal-constant integer-suffix
161/// octal-constant integer-suffix
162/// hexadecimal-constant integer-suffix
163/// decimal-constant:
164/// nonzero-digit
165/// decimal-constant digit
166/// octal-constant:
167/// 0
168/// octal-constant octal-digit
169/// hexadecimal-constant:
170/// hexadecimal-prefix hexadecimal-digit
171/// hexadecimal-constant hexadecimal-digit
172/// hexadecimal-prefix: one of
173/// 0x 0X
174/// integer-suffix:
175/// unsigned-suffix [long-suffix]
176/// unsigned-suffix [long-long-suffix]
177/// long-suffix [unsigned-suffix]
178/// long-long-suffix [unsigned-sufix]
179/// nonzero-digit:
180/// 1 2 3 4 5 6 7 8 9
181/// octal-digit:
182/// 0 1 2 3 4 5 6 7
183/// hexadecimal-digit:
184/// 0 1 2 3 4 5 6 7 8 9
185/// a b c d e f
186/// A B C D E F
187/// unsigned-suffix: one of
188/// u U
189/// long-suffix: one of
190/// l L
191/// long-long-suffix: one of
192/// ll LL
193///
194/// floating-constant: [C99 6.4.4.2]
195/// TODO: add rules...
196///
197
198NumericLiteralParser::
199NumericLiteralParser(const char *begin, const char *end,
Chris Lattner2f5add62007-04-05 06:57:15 +0000200 SourceLocation TokLoc, Preprocessor &pp)
201 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Steve Naroff09ef4742007-03-09 23:16:33 +0000202 s = DigitsBegin = begin;
203 saw_exponent = false;
204 saw_period = false;
205 saw_float_suffix = false;
206 isLong = false;
207 isUnsigned = false;
208 isLongLong = false;
209 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) {
Chris Lattnerde12ae22007-06-08 22:42:30 +0000219 // Done.
Steve Naroff09ef4742007-03-09 23:16:33 +0000220 } 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.
227 if (*s == 'p' || *s == 'P') {
228 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 }
Chris Lattnerde12ae22007-06-08 22:42:30 +0000242 } 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);
Steve Naroff09ef4742007-03-09 23:16:33 +0000255 } 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) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000263 // Done.
264 } else if (isxdigit(*s)) {
265 Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1));
266 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000267 } else if (*s == '.') {
268 s++;
269 radix = 10;
270 saw_period = true;
271 s = SkipDigits(s);
272 }
273 if (*s == 'e' || *s == 'E') { // exponent
274 s++;
275 radix = 10;
276 saw_exponent = true;
277 if (*s == '+' || *s == '-') s++; // sign
278 const char *first_non_digit = SkipDigits(s);
279 if (first_non_digit == s) {
280 Diag(TokLoc, diag::err_exponent_has_no_digits);
281 return;
282 } else {
283 s = first_non_digit;
284 }
285 }
286 }
287 } else { // the first digit is non-zero
288 radix = 10;
289 s = SkipDigits(s);
290 if (s == ThisTokEnd) {
Chris Lattner328fa5c2007-06-08 17:12:06 +0000291 // Done.
292 } else if (isxdigit(*s)) {
293 Diag(TokLoc, diag::err_invalid_decimal_digit, std::string(s, s+1));
294 return;
Steve Naroff09ef4742007-03-09 23:16:33 +0000295 } else if (*s == '.') {
296 s++;
297 saw_period = true;
298 s = SkipDigits(s);
299 }
300 if (*s == 'e' || *s == 'E') { // exponent
301 s++;
302 saw_exponent = true;
303 if (*s == '+' || *s == '-') s++; // sign
304 const char *first_non_digit = SkipDigits(s);
305 if (first_non_digit == s) {
306 Diag(TokLoc, diag::err_exponent_has_no_digits);
307 return;
308 } else {
309 s = first_non_digit;
310 }
311 }
312 }
313
314 SuffixBegin = s;
315
316 if (saw_period || saw_exponent) {
317 if (s < ThisTokEnd) { // parse size suffix (float, long double)
318 if (*s == 'f' || *s == 'F') {
319 saw_float_suffix = true;
320 s++;
321 } else if (*s == 'l' || *s == 'L') {
322 isLong = true;
323 s++;
324 }
325 if (s != ThisTokEnd) {
326 Diag(TokLoc, diag::err_invalid_suffix_float_constant,
327 std::string(SuffixBegin, ThisTokEnd));
328 return;
329 }
330 }
331 } else {
332 if (s < ThisTokEnd) {
333 // parse int suffix - they can appear in any order ("ul", "lu", "llu").
334 if (*s == 'u' || *s == 'U') {
335 s++;
336 isUnsigned = true; // unsigned
337
338 if ((s < ThisTokEnd) && (*s == 'l' || *s == 'L')) {
339 s++;
340 // handle "long long" type - l's need to be adjacent and same case.
341 if ((s < ThisTokEnd) && (*s == *(s-1))) {
342 isLongLong = true; // unsigned long long
343 s++;
344 } else {
345 isLong = true; // unsigned long
346 }
347 }
348 } else if (*s == 'l' || *s == 'L') {
349 s++;
350 // handle "long long" types - l's need to be adjacent and same case.
351 if ((s < ThisTokEnd) && (*s == *(s-1))) {
352 s++;
353 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
354 isUnsigned = true; // unsigned long long
355 s++;
356 } else {
357 isLongLong = true; // long long
358 }
359 } else { // handle "long" types
360 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
361 isUnsigned = true; // unsigned long
362 s++;
363 } else {
364 isLong = true; // long
365 }
366 }
367 }
368 if (s != ThisTokEnd) {
369 Diag(TokLoc, diag::err_invalid_suffix_integer_constant,
370 std::string(SuffixBegin, ThisTokEnd));
371 return;
372 }
373 }
374 }
375}
376
Chris Lattner5b743d32007-04-04 05:52:58 +0000377/// GetIntegerValue - Convert this numeric literal value to an APInt that
Chris Lattner871b4e12007-04-04 06:36:34 +0000378/// matches Val's input width. If there is an overflow, set Val to the low bits
379/// of the result and return true. Otherwise, return false.
Chris Lattner5b743d32007-04-04 05:52:58 +0000380bool NumericLiteralParser::GetIntegerValue(APInt &Val) {
381 Val = 0;
382 s = DigitsBegin;
383
Chris Lattner5b743d32007-04-04 05:52:58 +0000384 APInt RadixVal(Val.getBitWidth(), radix);
385 APInt CharVal(Val.getBitWidth(), 0);
386 APInt OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000387
388 bool OverflowOccurred = false;
Chris Lattner5b743d32007-04-04 05:52:58 +0000389 while (s < SuffixBegin) {
Chris Lattner2f5add62007-04-05 06:57:15 +0000390 unsigned C = HexDigitValue(*s++);
Chris Lattner5b743d32007-04-04 05:52:58 +0000391
392 // If this letter is out of bound for this radix, reject it.
Chris Lattner531efa42007-04-04 06:49:26 +0000393 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Chris Lattner5b743d32007-04-04 05:52:58 +0000394
395 CharVal = C;
396
Chris Lattner871b4e12007-04-04 06:36:34 +0000397 // Add the digit to the value in the appropriate radix. If adding in digits
398 // made the value smaller, then this overflowed.
Chris Lattner5b743d32007-04-04 05:52:58 +0000399 OldVal = Val;
Chris Lattner871b4e12007-04-04 06:36:34 +0000400
401 // Multiply by radix, did overflow occur on the multiply?
Chris Lattner5b743d32007-04-04 05:52:58 +0000402 Val *= RadixVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000403 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
404
405 OldVal = Val;
406 // Add value, did overflow occur on the value?
Chris Lattner5b743d32007-04-04 05:52:58 +0000407 Val += CharVal;
Chris Lattner871b4e12007-04-04 06:36:34 +0000408 OverflowOccurred |= Val.ult(OldVal);
409 OverflowOccurred |= Val.ult(CharVal);
Chris Lattner5b743d32007-04-04 05:52:58 +0000410 }
Chris Lattner871b4e12007-04-04 06:36:34 +0000411 return OverflowOccurred;
Chris Lattner5b743d32007-04-04 05:52:58 +0000412}
413
414
Steve Narofff2fb89e2007-03-13 20:29:44 +0000415void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID,
416 const std::string &M) {
417 PP.Diag(Loc, DiagID, M);
418 hadError = true;
419}
Steve Naroff4f88b312007-03-13 22:37:02 +0000420
Chris Lattner2f5add62007-04-05 06:57:15 +0000421
422CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
423 SourceLocation Loc, Preprocessor &PP) {
424 // At this point we know that the character matches the regex "L?'.*'".
425 HadError = false;
426 Value = 0;
427
428 // Determine if this is a wide character.
429 IsWide = begin[0] == 'L';
430 if (IsWide) ++begin;
431
432 // Skip over the entry quote.
433 assert(begin[0] == '\'' && "Invalid token lexed");
434 ++begin;
435
436 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
437 // size of "value".
438 assert(PP.getTargetInfo().getIntWidth(Loc) == 32 &&
439 "Assumes sizeof(int) == 4 for now");
440 // FIXME: This assumes that wchar_t is 32-bits for now.
441 assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 &&
442 "Assumes sizeof(wchar_t) == 4 for now");
443 // FIXME: This extensively assumes that 'char' is 8-bits.
444 assert(PP.getTargetInfo().getCharWidth(Loc) == 8 &&
445 "Assumes char is 8 bits");
446
447 bool isFirstChar = true;
448 bool isMultiChar = false;
449 while (begin[0] != '\'') {
450 unsigned ResultChar;
451 if (begin[0] != '\\') // If this is a normal character, consume it.
452 ResultChar = *begin++;
453 else // Otherwise, this is an escape character.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000454 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
Chris Lattner2f5add62007-04-05 06:57:15 +0000455
456 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
457 // implementation defined (C99 6.4.4.4p10).
458 if (!isFirstChar) {
459 // If this is the second character being processed, do special handling.
460 if (!isMultiChar) {
461 isMultiChar = true;
462
463 // Warn about discarding the top bits for multi-char wide-character
464 // constants (L'abcd').
465 if (IsWide)
466 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
467 }
468
469 if (IsWide) {
470 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
471 Value = 0;
472 } else {
473 // Narrow character literals act as though their value is concatenated
474 // in this implementation.
475 if (((Value << 8) >> 8) != Value)
476 PP.Diag(Loc, diag::warn_char_constant_too_large);
477 Value <<= 8;
478 }
479 }
480
481 Value += ResultChar;
482 isFirstChar = false;
483 }
484
485 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
486 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
487 // character constants are not sign extended in the this implementation:
488 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
489 if (!IsWide && !isMultiChar && (Value & 128) &&
490 PP.getTargetInfo().isCharSigned(Loc))
491 Value = (signed char)Value;
492}
493
494
Steve Naroff4f88b312007-03-13 22:37:02 +0000495/// string-literal: [C99 6.4.5]
496/// " [s-char-sequence] "
497/// L" [s-char-sequence] "
498/// s-char-sequence:
499/// s-char
500/// s-char-sequence s-char
501/// s-char:
502/// any source character except the double quote ",
503/// backslash \, or newline character
504/// escape-character
505/// universal-character-name
506/// escape-character: [C99 6.4.4.4]
507/// \ escape-code
508/// universal-character-name
509/// escape-code:
510/// character-escape-code
511/// octal-escape-code
512/// hex-escape-code
513/// character-escape-code: one of
514/// n t b r f v a
515/// \ ' " ?
516/// octal-escape-code:
517/// octal-digit
518/// octal-digit octal-digit
519/// octal-digit octal-digit octal-digit
520/// hex-escape-code:
521/// x hex-digit
522/// hex-escape-code hex-digit
523/// universal-character-name:
524/// \u hex-quad
525/// \U hex-quad hex-quad
526/// hex-quad:
527/// hex-digit hex-digit hex-digit hex-digit
Chris Lattner2f5add62007-04-05 06:57:15 +0000528///
Steve Naroff4f88b312007-03-13 22:37:02 +0000529StringLiteralParser::
530StringLiteralParser(const LexerToken *StringToks, unsigned NumStringToks,
Chris Lattner2f5add62007-04-05 06:57:15 +0000531 Preprocessor &pp, TargetInfo &t)
532 : PP(pp), Target(t) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000533 // Scan all of the string portions, remember the max individual token length,
534 // computing a bound on the concatenated string length, and see whether any
535 // piece is a wide-string. If any of the string portions is a wide-string
536 // literal, the result is a wide-string literal [C99 6.4.5p4].
537 MaxTokenLength = StringToks[0].getLength();
538 SizeBound = StringToks[0].getLength()-2; // -2 for "".
539 AnyWide = StringToks[0].getKind() == tok::wide_string_literal;
540
Steve Narofff1e53692007-03-23 22:27:02 +0000541 hadError = false;
Chris Lattner2f5add62007-04-05 06:57:15 +0000542
543 // Implement Translation Phase #6: concatenation of string literals
544 /// (C99 5.1.1.2p1). The common case is only one string fragment.
Steve Naroff4f88b312007-03-13 22:37:02 +0000545 for (unsigned i = 1; i != NumStringToks; ++i) {
546 // The string could be shorter than this if it needs cleaning, but this is a
547 // reasonable bound, which is all we need.
548 SizeBound += StringToks[i].getLength()-2; // -2 for "".
549
550 // Remember maximum string piece length.
551 if (StringToks[i].getLength() > MaxTokenLength)
552 MaxTokenLength = StringToks[i].getLength();
553
554 // Remember if we see any wide strings.
555 AnyWide |= StringToks[i].getKind() == tok::wide_string_literal;
556 }
557
558
559 // Include space for the null terminator.
560 ++SizeBound;
561
562 // TODO: K&R warning: "traditional C rejects string constant concatenation"
563
564 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
565 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
566 wchar_tByteWidth = ~0U;
Chris Lattner2f5add62007-04-05 06:57:15 +0000567 if (AnyWide) {
Steve Naroff4f88b312007-03-13 22:37:02 +0000568 wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation());
Chris Lattner2f5add62007-04-05 06:57:15 +0000569 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
570 wchar_tByteWidth /= 8;
571 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000572
573 // The output buffer size needs to be large enough to hold wide characters.
574 // This is a worst-case assumption which basically corresponds to L"" "long".
575 if (AnyWide)
576 SizeBound *= wchar_tByteWidth;
577
578 // Size the temporary buffer to hold the result string data.
579 ResultBuf.resize(SizeBound);
580
581 // Likewise, but for each string piece.
582 SmallString<512> TokenBuf;
583 TokenBuf.resize(MaxTokenLength);
584
585 // Loop over all the strings, getting their spelling, and expanding them to
586 // wide strings as appropriate.
587 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
588
589 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
590 const char *ThisTokBuf = &TokenBuf[0];
591 // Get the spelling of the token, which eliminates trigraphs, etc. We know
592 // that ThisTokBuf points to a buffer that is big enough for the whole token
593 // and 'spelled' tokens can only shrink.
594 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
595 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
596
597 // TODO: Input character set mapping support.
598
599 // Skip L marker for wide strings.
Chris Lattnerc10adde2007-05-20 05:00:58 +0000600 bool ThisIsWide = false;
601 if (ThisTokBuf[0] == 'L') {
602 ++ThisTokBuf;
603 ThisIsWide = true;
604 }
Steve Naroff4f88b312007-03-13 22:37:02 +0000605
606 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
607 ++ThisTokBuf;
608
609 while (ThisTokBuf != ThisTokEnd) {
610 // Is this a span of non-escape characters?
611 if (ThisTokBuf[0] != '\\') {
612 const char *InStart = ThisTokBuf;
613 do {
614 ++ThisTokBuf;
615 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
616
617 // Copy the character span over.
618 unsigned Len = ThisTokBuf-InStart;
619 if (!AnyWide) {
620 memcpy(ResultPtr, InStart, Len);
621 ResultPtr += Len;
622 } else {
623 // Note: our internal rep of wide char tokens is always little-endian.
624 for (; Len; --Len, ++InStart) {
625 *ResultPtr++ = InStart[0];
626 // Add zeros at the end.
627 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
628 *ResultPtr++ = 0;
629 }
630 }
631 continue;
632 }
633
Chris Lattner2f5add62007-04-05 06:57:15 +0000634 // Otherwise, this is an escape character. Process it.
635 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattnerc10adde2007-05-20 05:00:58 +0000636 StringToks[i].getLocation(),
637 ThisIsWide, PP);
Steve Naroff4f88b312007-03-13 22:37:02 +0000638
639 // Note: our internal rep of wide char tokens is always little-endian.
640 *ResultPtr++ = ResultChar & 0xFF;
641
642 if (AnyWide) {
643 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
644 *ResultPtr++ = ResultChar >> i*8;
645 }
646 }
647 }
648
649 // Add zero terminator.
650 *ResultPtr = 0;
651 if (AnyWide) {
652 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
653 *ResultPtr++ = 0;
654 }
655}