1 //===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the NumericLiteralParser, CharLiteralParser, and
11 // StringLiteralParser interfaces.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Lex/LiteralSupport.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Lex/LexDiagnostic.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21 using namespace clang;
22 
23 /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
24 /// not valid.
25 static int HexDigitValue(char C) {
26   if (C >= '0' && C <= '9') return C-'0';
27   if (C >= 'a' && C <= 'f') return C-'a'+10;
28   if (C >= 'A' && C <= 'F') return C-'A'+10;
29   return -1;
30 }
31 
32 static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
33   switch (kind) {
34   default: llvm_unreachable("Unknown token type!");
35   case tok::char_constant:
36   case tok::string_literal:
37   case tok::utf8_string_literal:
38     return Target.getCharWidth();
39   case tok::wide_char_constant:
40   case tok::wide_string_literal:
41     return Target.getWCharWidth();
42   case tok::utf16_char_constant:
43   case tok::utf16_string_literal:
44     return Target.getChar16Width();
45   case tok::utf32_char_constant:
46   case tok::utf32_string_literal:
47     return Target.getChar32Width();
48   }
49 }
50 
51 /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
52 /// either a character or a string literal.
53 static unsigned ProcessCharEscape(const char *&ThisTokBuf,
54                                   const char *ThisTokEnd, bool &HadError,
55                                   FullSourceLoc Loc, unsigned CharWidth,
56                                   DiagnosticsEngine *Diags) {
57   // Skip the '\' char.
58   ++ThisTokBuf;
59 
60   // We know that this character can't be off the end of the buffer, because
61   // that would have been \", which would not have been the end of string.
62   unsigned ResultChar = *ThisTokBuf++;
63   switch (ResultChar) {
64   // These map to themselves.
65   case '\\': case '\'': case '"': case '?': break;
66 
67     // These have fixed mappings.
68   case 'a':
69     // TODO: K&R: the meaning of '\\a' is different in traditional C
70     ResultChar = 7;
71     break;
72   case 'b':
73     ResultChar = 8;
74     break;
75   case 'e':
76     if (Diags)
77       Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
78     ResultChar = 27;
79     break;
80   case 'E':
81     if (Diags)
82       Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
83     ResultChar = 27;
84     break;
85   case 'f':
86     ResultChar = 12;
87     break;
88   case 'n':
89     ResultChar = 10;
90     break;
91   case 'r':
92     ResultChar = 13;
93     break;
94   case 't':
95     ResultChar = 9;
96     break;
97   case 'v':
98     ResultChar = 11;
99     break;
100   case 'x': { // Hex escape.
101     ResultChar = 0;
102     if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
103       if (Diags)
104         Diags->Report(Loc, diag::err_hex_escape_no_digits);
105       HadError = 1;
106       break;
107     }
108 
109     // Hex escapes are a maximal series of hex digits.
110     bool Overflow = false;
111     for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
112       int CharVal = HexDigitValue(ThisTokBuf[0]);
113       if (CharVal == -1) break;
114       // About to shift out a digit?
115       Overflow |= (ResultChar & 0xF0000000) ? true : false;
116       ResultChar <<= 4;
117       ResultChar |= CharVal;
118     }
119 
120     // See if any bits will be truncated when evaluated as a character.
121     if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
122       Overflow = true;
123       ResultChar &= ~0U >> (32-CharWidth);
124     }
125 
126     // Check for overflow.
127     if (Overflow && Diags)   // Too many digits to fit in
128       Diags->Report(Loc, diag::warn_hex_escape_too_large);
129     break;
130   }
131   case '0': case '1': case '2': case '3':
132   case '4': case '5': case '6': case '7': {
133     // Octal escapes.
134     --ThisTokBuf;
135     ResultChar = 0;
136 
137     // Octal escapes are a series of octal digits with maximum length 3.
138     // "\0123" is a two digit sequence equal to "\012" "3".
139     unsigned NumDigits = 0;
140     do {
141       ResultChar <<= 3;
142       ResultChar |= *ThisTokBuf++ - '0';
143       ++NumDigits;
144     } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
145              ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
146 
147     // Check for overflow.  Reject '\777', but not L'\777'.
148     if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
149       if (Diags)
150         Diags->Report(Loc, diag::warn_octal_escape_too_large);
151       ResultChar &= ~0U >> (32-CharWidth);
152     }
153     break;
154   }
155 
156     // Otherwise, these are not valid escapes.
157   case '(': case '{': case '[': case '%':
158     // GCC accepts these as extensions.  We warn about them as such though.
159     if (Diags)
160       Diags->Report(Loc, diag::ext_nonstandard_escape)
161         << std::string()+(char)ResultChar;
162     break;
163   default:
164     if (Diags == 0)
165       break;
166 
167     if (isgraph(ResultChar))
168       Diags->Report(Loc, diag::ext_unknown_escape)
169         << std::string()+(char)ResultChar;
170     else
171       Diags->Report(Loc, diag::ext_unknown_escape)
172         << "x"+llvm::utohexstr(ResultChar);
173     break;
174   }
175 
176   return ResultChar;
177 }
178 
179 /// ProcessUCNEscape - Read the Universal Character Name, check constraints and
180 /// return the UTF32.
181 static bool ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
182                              uint32_t &UcnVal, unsigned short &UcnLen,
183                              FullSourceLoc Loc, DiagnosticsEngine *Diags,
184                              const LangOptions &Features) {
185   if (!Features.CPlusPlus && !Features.C99 && Diags)
186     Diags->Report(Loc, diag::warn_ucn_not_valid_in_c89);
187 
188   // Save the beginning of the string (for error diagnostics).
189   const char *ThisTokBegin = ThisTokBuf;
190 
191   // Skip the '\u' char's.
192   ThisTokBuf += 2;
193 
194   if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
195     if (Diags)
196       Diags->Report(Loc, diag::err_ucn_escape_no_digits);
197     return false;
198   }
199   UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
200   unsigned short UcnLenSave = UcnLen;
201   for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
202     int CharVal = HexDigitValue(ThisTokBuf[0]);
203     if (CharVal == -1) break;
204     UcnVal <<= 4;
205     UcnVal |= CharVal;
206   }
207   // If we didn't consume the proper number of digits, there is a problem.
208   if (UcnLenSave) {
209     if (Diags) {
210       SourceLocation L =
211         Lexer::AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin,
212                                        Loc.getManager(), Features);
213       Diags->Report(FullSourceLoc(L, Loc.getManager()),
214                     diag::err_ucn_escape_incomplete);
215     }
216     return false;
217   }
218   // Check UCN constraints (C99 6.4.3p2).
219   if ((UcnVal < 0xa0 &&
220       (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
221       || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
222       || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
223     if (Diags)
224       Diags->Report(Loc, diag::err_ucn_escape_invalid);
225     return false;
226   }
227   return true;
228 }
229 
230 /// EncodeUCNEscape - Read the Universal Character Name, check constraints and
231 /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
232 /// StringLiteralParser. When we decide to implement UCN's for identifiers,
233 /// we will likely rework our support for UCN's.
234 static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
235                             char *&ResultBuf, bool &HadError,
236                             FullSourceLoc Loc, unsigned CharByteWidth,
237                             DiagnosticsEngine *Diags,
238                             const LangOptions &Features) {
239   typedef uint32_t UTF32;
240   UTF32 UcnVal = 0;
241   unsigned short UcnLen = 0;
242   if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, Loc, Diags,
243                         Features)) {
244     HadError = 1;
245     return;
246   }
247 
248   assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) &&
249          "only character widths of 1, 2, or 4 bytes supported");
250 
251   (void)UcnLen;
252   assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
253 
254   if (CharByteWidth == 4) {
255     // Note: our internal rep of wide char tokens is always little-endian.
256     *ResultBuf++ = (UcnVal & 0x000000FF);
257     *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
258     *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
259     *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
260     return;
261   }
262 
263   if (CharByteWidth == 2) {
264     // Convert to UTF16.
265     if (UcnVal < (UTF32)0xFFFF) {
266       *ResultBuf++ = (UcnVal & 0x000000FF);
267       *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
268       return;
269     }
270     if (Diags) Diags->Report(Loc, diag::warn_ucn_escape_too_large);
271 
272     typedef uint16_t UTF16;
273     UcnVal -= 0x10000;
274     UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
275     UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
276     *ResultBuf++ = (surrogate1 & 0x000000FF);
277     *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
278     *ResultBuf++ = (surrogate2 & 0x000000FF);
279     *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
280     return;
281   }
282 
283   assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
284 
285   // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
286   // The conversion below was inspired by:
287   //   http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
288   // First, we determine how many bytes the result will require.
289   typedef uint8_t UTF8;
290 
291   unsigned short bytesToWrite = 0;
292   if (UcnVal < (UTF32)0x80)
293     bytesToWrite = 1;
294   else if (UcnVal < (UTF32)0x800)
295     bytesToWrite = 2;
296   else if (UcnVal < (UTF32)0x10000)
297     bytesToWrite = 3;
298   else
299     bytesToWrite = 4;
300 
301   const unsigned byteMask = 0xBF;
302   const unsigned byteMark = 0x80;
303 
304   // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
305   // into the first byte, depending on how many bytes follow.
306   static const UTF8 firstByteMark[5] = {
307     0x00, 0x00, 0xC0, 0xE0, 0xF0
308   };
309   // Finally, we write the bytes into ResultBuf.
310   ResultBuf += bytesToWrite;
311   switch (bytesToWrite) { // note: everything falls through.
312     case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
313     case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
314     case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
315     case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
316   }
317   // Update the buffer.
318   ResultBuf += bytesToWrite;
319 }
320 
321 
322 ///       integer-constant: [C99 6.4.4.1]
323 ///         decimal-constant integer-suffix
324 ///         octal-constant integer-suffix
325 ///         hexadecimal-constant integer-suffix
326 ///       decimal-constant:
327 ///         nonzero-digit
328 ///         decimal-constant digit
329 ///       octal-constant:
330 ///         0
331 ///         octal-constant octal-digit
332 ///       hexadecimal-constant:
333 ///         hexadecimal-prefix hexadecimal-digit
334 ///         hexadecimal-constant hexadecimal-digit
335 ///       hexadecimal-prefix: one of
336 ///         0x 0X
337 ///       integer-suffix:
338 ///         unsigned-suffix [long-suffix]
339 ///         unsigned-suffix [long-long-suffix]
340 ///         long-suffix [unsigned-suffix]
341 ///         long-long-suffix [unsigned-sufix]
342 ///       nonzero-digit:
343 ///         1 2 3 4 5 6 7 8 9
344 ///       octal-digit:
345 ///         0 1 2 3 4 5 6 7
346 ///       hexadecimal-digit:
347 ///         0 1 2 3 4 5 6 7 8 9
348 ///         a b c d e f
349 ///         A B C D E F
350 ///       unsigned-suffix: one of
351 ///         u U
352 ///       long-suffix: one of
353 ///         l L
354 ///       long-long-suffix: one of
355 ///         ll LL
356 ///
357 ///       floating-constant: [C99 6.4.4.2]
358 ///         TODO: add rules...
359 ///
360 NumericLiteralParser::
361 NumericLiteralParser(const char *begin, const char *end,
362                      SourceLocation TokLoc, Preprocessor &pp)
363   : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
364 
365   // This routine assumes that the range begin/end matches the regex for integer
366   // and FP constants (specifically, the 'pp-number' regex), and assumes that
367   // the byte at "*end" is both valid and not part of the regex.  Because of
368   // this, it doesn't have to check for 'overscan' in various places.
369   assert(!isalnum(*end) && *end != '.' && *end != '_' &&
370          "Lexer didn't maximally munch?");
371 
372   s = DigitsBegin = begin;
373   saw_exponent = false;
374   saw_period = false;
375   isLong = false;
376   isUnsigned = false;
377   isLongLong = false;
378   isFloat = false;
379   isImaginary = false;
380   isMicrosoftInteger = false;
381   hadError = false;
382 
383   if (*s == '0') { // parse radix
384     ParseNumberStartingWithZero(TokLoc);
385     if (hadError)
386       return;
387   } else { // the first digit is non-zero
388     radix = 10;
389     s = SkipDigits(s);
390     if (s == ThisTokEnd) {
391       // Done.
392     } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
393       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
394               diag::err_invalid_decimal_digit) << StringRef(s, 1);
395       hadError = true;
396       return;
397     } else if (*s == '.') {
398       s++;
399       saw_period = true;
400       s = SkipDigits(s);
401     }
402     if ((*s == 'e' || *s == 'E')) { // exponent
403       const char *Exponent = s;
404       s++;
405       saw_exponent = true;
406       if (*s == '+' || *s == '-')  s++; // sign
407       const char *first_non_digit = SkipDigits(s);
408       if (first_non_digit != s) {
409         s = first_non_digit;
410       } else {
411         PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
412                 diag::err_exponent_has_no_digits);
413         hadError = true;
414         return;
415       }
416     }
417   }
418 
419   SuffixBegin = s;
420 
421   // Parse the suffix.  At this point we can classify whether we have an FP or
422   // integer constant.
423   bool isFPConstant = isFloatingLiteral();
424 
425   // Loop over all of the characters of the suffix.  If we see something bad,
426   // we break out of the loop.
427   for (; s != ThisTokEnd; ++s) {
428     switch (*s) {
429     case 'f':      // FP Suffix for "float"
430     case 'F':
431       if (!isFPConstant) break;  // Error for integer constant.
432       if (isFloat || isLong) break; // FF, LF invalid.
433       isFloat = true;
434       continue;  // Success.
435     case 'u':
436     case 'U':
437       if (isFPConstant) break;  // Error for floating constant.
438       if (isUnsigned) break;    // Cannot be repeated.
439       isUnsigned = true;
440       continue;  // Success.
441     case 'l':
442     case 'L':
443       if (isLong || isLongLong) break;  // Cannot be repeated.
444       if (isFloat) break;               // LF invalid.
445 
446       // Check for long long.  The L's need to be adjacent and the same case.
447       if (s+1 != ThisTokEnd && s[1] == s[0]) {
448         if (isFPConstant) break;        // long long invalid for floats.
449         isLongLong = true;
450         ++s;  // Eat both of them.
451       } else {
452         isLong = true;
453       }
454       continue;  // Success.
455     case 'i':
456     case 'I':
457       if (PP.getLangOptions().MicrosoftExt) {
458         if (isFPConstant || isLong || isLongLong) break;
459 
460         // Allow i8, i16, i32, i64, and i128.
461         if (s + 1 != ThisTokEnd) {
462           switch (s[1]) {
463             case '8':
464               s += 2; // i8 suffix
465               isMicrosoftInteger = true;
466               break;
467             case '1':
468               if (s + 2 == ThisTokEnd) break;
469               if (s[2] == '6') {
470                 s += 3; // i16 suffix
471                 isMicrosoftInteger = true;
472               }
473               else if (s[2] == '2') {
474                 if (s + 3 == ThisTokEnd) break;
475                 if (s[3] == '8') {
476                   s += 4; // i128 suffix
477                   isMicrosoftInteger = true;
478                 }
479               }
480               break;
481             case '3':
482               if (s + 2 == ThisTokEnd) break;
483               if (s[2] == '2') {
484                 s += 3; // i32 suffix
485                 isLong = true;
486                 isMicrosoftInteger = true;
487               }
488               break;
489             case '6':
490               if (s + 2 == ThisTokEnd) break;
491               if (s[2] == '4') {
492                 s += 3; // i64 suffix
493                 isLongLong = true;
494                 isMicrosoftInteger = true;
495               }
496               break;
497             default:
498               break;
499           }
500           break;
501         }
502       }
503       // fall through.
504     case 'j':
505     case 'J':
506       if (isImaginary) break;   // Cannot be repeated.
507       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
508               diag::ext_imaginary_constant);
509       isImaginary = true;
510       continue;  // Success.
511     }
512     // If we reached here, there was an error.
513     break;
514   }
515 
516   // Report an error if there are any.
517   if (s != ThisTokEnd) {
518     PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
519             isFPConstant ? diag::err_invalid_suffix_float_constant :
520                            diag::err_invalid_suffix_integer_constant)
521       << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
522     hadError = true;
523     return;
524   }
525 }
526 
527 /// ParseNumberStartingWithZero - This method is called when the first character
528 /// of the number is found to be a zero.  This means it is either an octal
529 /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
530 /// a floating point number (01239.123e4).  Eat the prefix, determining the
531 /// radix etc.
532 void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
533   assert(s[0] == '0' && "Invalid method call");
534   s++;
535 
536   // Handle a hex number like 0x1234.
537   if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
538     s++;
539     radix = 16;
540     DigitsBegin = s;
541     s = SkipHexDigits(s);
542     if (s == ThisTokEnd) {
543       // Done.
544     } else if (*s == '.') {
545       s++;
546       saw_period = true;
547       s = SkipHexDigits(s);
548     }
549     // A binary exponent can appear with or with a '.'. If dotted, the
550     // binary exponent is required.
551     if (*s == 'p' || *s == 'P') {
552       const char *Exponent = s;
553       s++;
554       saw_exponent = true;
555       if (*s == '+' || *s == '-')  s++; // sign
556       const char *first_non_digit = SkipDigits(s);
557       if (first_non_digit == s) {
558         PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
559                 diag::err_exponent_has_no_digits);
560         hadError = true;
561         return;
562       }
563       s = first_non_digit;
564 
565       // In C++0x, we cannot support hexadecmial floating literals because
566       // they conflict with user-defined literals, so we warn in previous
567       // versions of C++ by default.
568       if (!PP.getLangOptions().HexFloats)
569         PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
570     } else if (saw_period) {
571       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
572               diag::err_hexconstant_requires_exponent);
573       hadError = true;
574     }
575     return;
576   }
577 
578   // Handle simple binary numbers 0b01010
579   if (*s == 'b' || *s == 'B') {
580     // 0b101010 is a GCC extension.
581     PP.Diag(TokLoc, diag::ext_binary_literal);
582     ++s;
583     radix = 2;
584     DigitsBegin = s;
585     s = SkipBinaryDigits(s);
586     if (s == ThisTokEnd) {
587       // Done.
588     } else if (isxdigit(*s)) {
589       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
590               diag::err_invalid_binary_digit) << StringRef(s, 1);
591       hadError = true;
592     }
593     // Other suffixes will be diagnosed by the caller.
594     return;
595   }
596 
597   // For now, the radix is set to 8. If we discover that we have a
598   // floating point constant, the radix will change to 10. Octal floating
599   // point constants are not permitted (only decimal and hexadecimal).
600   radix = 8;
601   DigitsBegin = s;
602   s = SkipOctalDigits(s);
603   if (s == ThisTokEnd)
604     return; // Done, simple octal number like 01234
605 
606   // If we have some other non-octal digit that *is* a decimal digit, see if
607   // this is part of a floating point number like 094.123 or 09e1.
608   if (isdigit(*s)) {
609     const char *EndDecimal = SkipDigits(s);
610     if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
611       s = EndDecimal;
612       radix = 10;
613     }
614   }
615 
616   // If we have a hex digit other than 'e' (which denotes a FP exponent) then
617   // the code is using an incorrect base.
618   if (isxdigit(*s) && *s != 'e' && *s != 'E') {
619     PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
620             diag::err_invalid_octal_digit) << StringRef(s, 1);
621     hadError = true;
622     return;
623   }
624 
625   if (*s == '.') {
626     s++;
627     radix = 10;
628     saw_period = true;
629     s = SkipDigits(s); // Skip suffix.
630   }
631   if (*s == 'e' || *s == 'E') { // exponent
632     const char *Exponent = s;
633     s++;
634     radix = 10;
635     saw_exponent = true;
636     if (*s == '+' || *s == '-')  s++; // sign
637     const char *first_non_digit = SkipDigits(s);
638     if (first_non_digit != s) {
639       s = first_non_digit;
640     } else {
641       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
642               diag::err_exponent_has_no_digits);
643       hadError = true;
644       return;
645     }
646   }
647 }
648 
649 
650 /// GetIntegerValue - Convert this numeric literal value to an APInt that
651 /// matches Val's input width.  If there is an overflow, set Val to the low bits
652 /// of the result and return true.  Otherwise, return false.
653 bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
654   // Fast path: Compute a conservative bound on the maximum number of
655   // bits per digit in this radix. If we can't possibly overflow a
656   // uint64 based on that bound then do the simple conversion to
657   // integer. This avoids the expensive overflow checking below, and
658   // handles the common cases that matter (small decimal integers and
659   // hex/octal values which don't overflow).
660   unsigned MaxBitsPerDigit = 1;
661   while ((1U << MaxBitsPerDigit) < radix)
662     MaxBitsPerDigit += 1;
663   if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
664     uint64_t N = 0;
665     for (s = DigitsBegin; s != SuffixBegin; ++s)
666       N = N*radix + HexDigitValue(*s);
667 
668     // This will truncate the value to Val's input width. Simply check
669     // for overflow by comparing.
670     Val = N;
671     return Val.getZExtValue() != N;
672   }
673 
674   Val = 0;
675   s = DigitsBegin;
676 
677   llvm::APInt RadixVal(Val.getBitWidth(), radix);
678   llvm::APInt CharVal(Val.getBitWidth(), 0);
679   llvm::APInt OldVal = Val;
680 
681   bool OverflowOccurred = false;
682   while (s < SuffixBegin) {
683     unsigned C = HexDigitValue(*s++);
684 
685     // If this letter is out of bound for this radix, reject it.
686     assert(C < radix && "NumericLiteralParser ctor should have rejected this");
687 
688     CharVal = C;
689 
690     // Add the digit to the value in the appropriate radix.  If adding in digits
691     // made the value smaller, then this overflowed.
692     OldVal = Val;
693 
694     // Multiply by radix, did overflow occur on the multiply?
695     Val *= RadixVal;
696     OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
697 
698     // Add value, did overflow occur on the value?
699     //   (a + b) ult b  <=> overflow
700     Val += CharVal;
701     OverflowOccurred |= Val.ult(CharVal);
702   }
703   return OverflowOccurred;
704 }
705 
706 llvm::APFloat::opStatus
707 NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
708   using llvm::APFloat;
709 
710   unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
711   return Result.convertFromString(StringRef(ThisTokBegin, n),
712                                   APFloat::rmNearestTiesToEven);
713 }
714 
715 
716 ///       character-literal: [C++0x lex.ccon]
717 ///         ' c-char-sequence '
718 ///         u' c-char-sequence '
719 ///         U' c-char-sequence '
720 ///         L' c-char-sequence '
721 ///       c-char-sequence:
722 ///         c-char
723 ///         c-char-sequence c-char
724 ///       c-char:
725 ///         any member of the source character set except the single-quote ',
726 ///           backslash \, or new-line character
727 ///         escape-sequence
728 ///         universal-character-name
729 ///       escape-sequence: [C++0x lex.ccon]
730 ///         simple-escape-sequence
731 ///         octal-escape-sequence
732 ///         hexadecimal-escape-sequence
733 ///       simple-escape-sequence:
734 ///         one of \' \" \? \\ \a \b \f \n \r \t \v
735 ///       octal-escape-sequence:
736 ///         \ octal-digit
737 ///         \ octal-digit octal-digit
738 ///         \ octal-digit octal-digit octal-digit
739 ///       hexadecimal-escape-sequence:
740 ///         \x hexadecimal-digit
741 ///         hexadecimal-escape-sequence hexadecimal-digit
742 ///       universal-character-name:
743 ///         \u hex-quad
744 ///         \U hex-quad hex-quad
745 ///       hex-quad:
746 ///         hex-digit hex-digit hex-digit hex-digit
747 ///
748 CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
749                                      SourceLocation Loc, Preprocessor &PP,
750                                      tok::TokenKind kind) {
751   // At this point we know that the character matches the regex "L?'.*'".
752   HadError = false;
753 
754   Kind = kind;
755 
756   // Determine if this is a wide or UTF character.
757   if (Kind == tok::wide_char_constant || Kind == tok::utf16_char_constant ||
758       Kind == tok::utf32_char_constant) {
759     ++begin;
760   }
761 
762   // Skip over the entry quote.
763   assert(begin[0] == '\'' && "Invalid token lexed");
764   ++begin;
765 
766   // FIXME: The "Value" is an uint64_t so we can handle char literals of
767   // up to 64-bits.
768   // FIXME: This extensively assumes that 'char' is 8-bits.
769   assert(PP.getTargetInfo().getCharWidth() == 8 &&
770          "Assumes char is 8 bits");
771   assert(PP.getTargetInfo().getIntWidth() <= 64 &&
772          (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
773          "Assumes sizeof(int) on target is <= 64 and a multiple of char");
774   assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
775          "Assumes sizeof(wchar) on target is <= 64");
776 
777   // This is what we will use for overflow detection
778   llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
779 
780   unsigned NumCharsSoFar = 0;
781   bool Warned = false;
782   while (begin[0] != '\'') {
783     uint64_t ResultChar;
784 
785       // Is this a Universal Character Name escape?
786     if (begin[0] != '\\')     // If this is a normal character, consume it.
787       ResultChar = (unsigned char)*begin++;
788     else {                    // Otherwise, this is an escape character.
789       unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
790       // Check for UCN.
791       if (begin[1] == 'u' || begin[1] == 'U') {
792         uint32_t utf32 = 0;
793         unsigned short UcnLen = 0;
794         if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
795                               FullSourceLoc(Loc, PP.getSourceManager()),
796                               &PP.getDiagnostics(), PP.getLangOptions())) {
797           HadError = 1;
798         }
799         ResultChar = utf32;
800         if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
801           PP.Diag(Loc, diag::warn_ucn_escape_too_large);
802           ResultChar &= ~0U >> (32-CharWidth);
803         }
804       } else {
805         // Otherwise, this is a non-UCN escape character.  Process it.
806         ResultChar = ProcessCharEscape(begin, end, HadError,
807                                        FullSourceLoc(Loc,PP.getSourceManager()),
808                                        CharWidth, &PP.getDiagnostics());
809       }
810     }
811 
812     // If this is a multi-character constant (e.g. 'abc'), handle it.  These are
813     // implementation defined (C99 6.4.4.4p10).
814     if (NumCharsSoFar) {
815       if (!isAscii()) {
816         // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
817         LitVal = 0;
818       } else {
819         // Narrow character literals act as though their value is concatenated
820         // in this implementation, but warn on overflow.
821         if (LitVal.countLeadingZeros() < 8 && !Warned) {
822           PP.Diag(Loc, diag::warn_char_constant_too_large);
823           Warned = true;
824         }
825         LitVal <<= 8;
826       }
827     }
828 
829     LitVal = LitVal + ResultChar;
830     ++NumCharsSoFar;
831   }
832 
833   // If this is the second character being processed, do special handling.
834   if (NumCharsSoFar > 1) {
835     // Warn about discarding the top bits for multi-char wide-character
836     // constants (L'abcd').
837     if (!isAscii())
838       PP.Diag(Loc, diag::warn_extraneous_char_constant);
839     else if (NumCharsSoFar != 4)
840       PP.Diag(Loc, diag::ext_multichar_character_literal);
841     else
842       PP.Diag(Loc, diag::ext_four_char_character_literal);
843     IsMultiChar = true;
844   } else
845     IsMultiChar = false;
846 
847   // Transfer the value from APInt to uint64_t
848   Value = LitVal.getZExtValue();
849 
850   // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
851   // if 'char' is signed for this target (C99 6.4.4.4p10).  Note that multiple
852   // character constants are not sign extended in the this implementation:
853   // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
854   if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
855       PP.getLangOptions().CharIsSigned)
856     Value = (signed char)Value;
857 }
858 
859 
860 ///       string-literal: [C++0x lex.string]
861 ///         encoding-prefix " [s-char-sequence] "
862 ///         encoding-prefix R raw-string
863 ///       encoding-prefix:
864 ///         u8
865 ///         u
866 ///         U
867 ///         L
868 ///       s-char-sequence:
869 ///         s-char
870 ///         s-char-sequence s-char
871 ///       s-char:
872 ///         any member of the source character set except the double-quote ",
873 ///           backslash \, or new-line character
874 ///         escape-sequence
875 ///         universal-character-name
876 ///       raw-string:
877 ///         " d-char-sequence ( r-char-sequence ) d-char-sequence "
878 ///       r-char-sequence:
879 ///         r-char
880 ///         r-char-sequence r-char
881 ///       r-char:
882 ///         any member of the source character set, except a right parenthesis )
883 ///           followed by the initial d-char-sequence (which may be empty)
884 ///           followed by a double quote ".
885 ///       d-char-sequence:
886 ///         d-char
887 ///         d-char-sequence d-char
888 ///       d-char:
889 ///         any member of the basic source character set except:
890 ///           space, the left parenthesis (, the right parenthesis ),
891 ///           the backslash \, and the control characters representing horizontal
892 ///           tab, vertical tab, form feed, and newline.
893 ///       escape-sequence: [C++0x lex.ccon]
894 ///         simple-escape-sequence
895 ///         octal-escape-sequence
896 ///         hexadecimal-escape-sequence
897 ///       simple-escape-sequence:
898 ///         one of \' \" \? \\ \a \b \f \n \r \t \v
899 ///       octal-escape-sequence:
900 ///         \ octal-digit
901 ///         \ octal-digit octal-digit
902 ///         \ octal-digit octal-digit octal-digit
903 ///       hexadecimal-escape-sequence:
904 ///         \x hexadecimal-digit
905 ///         hexadecimal-escape-sequence hexadecimal-digit
906 ///       universal-character-name:
907 ///         \u hex-quad
908 ///         \U hex-quad hex-quad
909 ///       hex-quad:
910 ///         hex-digit hex-digit hex-digit hex-digit
911 ///
912 StringLiteralParser::
913 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
914                     Preprocessor &PP, bool Complain)
915   : SM(PP.getSourceManager()), Features(PP.getLangOptions()),
916     Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0),
917     MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
918     ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
919   init(StringToks, NumStringToks);
920 }
921 
922 void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){
923   // The literal token may have come from an invalid source location (e.g. due
924   // to a PCH error), in which case the token length will be 0.
925   if (NumStringToks == 0 || StringToks[0].getLength() < 2) {
926     hadError = true;
927     return;
928   }
929 
930   // Scan all of the string portions, remember the max individual token length,
931   // computing a bound on the concatenated string length, and see whether any
932   // piece is a wide-string.  If any of the string portions is a wide-string
933   // literal, the result is a wide-string literal [C99 6.4.5p4].
934   assert(NumStringToks && "expected at least one token");
935   MaxTokenLength = StringToks[0].getLength();
936   assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
937   SizeBound = StringToks[0].getLength()-2;  // -2 for "".
938   Kind = StringToks[0].getKind();
939 
940   hadError = false;
941 
942   // Implement Translation Phase #6: concatenation of string literals
943   /// (C99 5.1.1.2p1).  The common case is only one string fragment.
944   for (unsigned i = 1; i != NumStringToks; ++i) {
945     if (StringToks[i].getLength() < 2) {
946       hadError = true;
947       return;
948     }
949 
950     // The string could be shorter than this if it needs cleaning, but this is a
951     // reasonable bound, which is all we need.
952     assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
953     SizeBound += StringToks[i].getLength()-2;  // -2 for "".
954 
955     // Remember maximum string piece length.
956     if (StringToks[i].getLength() > MaxTokenLength)
957       MaxTokenLength = StringToks[i].getLength();
958 
959     // Remember if we see any wide or utf-8/16/32 strings.
960     // Also check for illegal concatenations.
961     if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
962       if (isAscii()) {
963         Kind = StringToks[i].getKind();
964       } else {
965         if (Diags)
966           Diags->Report(FullSourceLoc(StringToks[i].getLocation(), SM),
967                         diag::err_unsupported_string_concat);
968         hadError = true;
969       }
970     }
971   }
972 
973   // Include space for the null terminator.
974   ++SizeBound;
975 
976   // TODO: K&R warning: "traditional C rejects string constant concatenation"
977 
978   // Get the width in bytes of char/wchar_t/char16_t/char32_t
979   CharByteWidth = getCharWidth(Kind, Target);
980   assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
981   CharByteWidth /= 8;
982 
983   // The output buffer size needs to be large enough to hold wide characters.
984   // This is a worst-case assumption which basically corresponds to L"" "long".
985   SizeBound *= CharByteWidth;
986 
987   // Size the temporary buffer to hold the result string data.
988   ResultBuf.resize(SizeBound);
989 
990   // Likewise, but for each string piece.
991   llvm::SmallString<512> TokenBuf;
992   TokenBuf.resize(MaxTokenLength);
993 
994   // Loop over all the strings, getting their spelling, and expanding them to
995   // wide strings as appropriate.
996   ResultPtr = &ResultBuf[0];   // Next byte to fill in.
997 
998   Pascal = false;
999 
1000   for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
1001     const char *ThisTokBuf = &TokenBuf[0];
1002     // Get the spelling of the token, which eliminates trigraphs, etc.  We know
1003     // that ThisTokBuf points to a buffer that is big enough for the whole token
1004     // and 'spelled' tokens can only shrink.
1005     bool StringInvalid = false;
1006     unsigned ThisTokLen =
1007       Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1008                          &StringInvalid);
1009     if (StringInvalid) {
1010       hadError = true;
1011       continue;
1012     }
1013 
1014     const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1;  // Skip end quote.
1015     // TODO: Input character set mapping support.
1016 
1017     // Skip marker for wide or unicode strings.
1018     if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
1019       ++ThisTokBuf;
1020       // Skip 8 of u8 marker for utf8 strings.
1021       if (ThisTokBuf[0] == '8')
1022         ++ThisTokBuf;
1023     }
1024 
1025     // Check for raw string
1026     if (ThisTokBuf[0] == 'R') {
1027       ThisTokBuf += 2; // skip R"
1028 
1029       const char *Prefix = ThisTokBuf;
1030       while (ThisTokBuf[0] != '(')
1031         ++ThisTokBuf;
1032       ++ThisTokBuf; // skip '('
1033 
1034       // remove same number of characters from the end
1035       if (ThisTokEnd >= ThisTokBuf + (ThisTokBuf - Prefix))
1036         ThisTokEnd -= (ThisTokBuf - Prefix);
1037 
1038       // Copy the string over
1039       CopyStringFragment(StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf));
1040     } else {
1041       assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
1042       ++ThisTokBuf; // skip "
1043 
1044       // Check if this is a pascal string
1045       if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1046           ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1047 
1048         // If the \p sequence is found in the first token, we have a pascal string
1049         // Otherwise, if we already have a pascal string, ignore the first \p
1050         if (i == 0) {
1051           ++ThisTokBuf;
1052           Pascal = true;
1053         } else if (Pascal)
1054           ThisTokBuf += 2;
1055       }
1056 
1057       while (ThisTokBuf != ThisTokEnd) {
1058         // Is this a span of non-escape characters?
1059         if (ThisTokBuf[0] != '\\') {
1060           const char *InStart = ThisTokBuf;
1061           do {
1062             ++ThisTokBuf;
1063           } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1064 
1065           // Copy the character span over.
1066           CopyStringFragment(StringRef(InStart, ThisTokBuf - InStart));
1067           continue;
1068         }
1069         // Is this a Universal Character Name escape?
1070         if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
1071           EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
1072                           hadError, FullSourceLoc(StringToks[i].getLocation(),SM),
1073                           CharByteWidth, Diags, Features);
1074           continue;
1075         }
1076         // Otherwise, this is a non-UCN escape character.  Process it.
1077         unsigned ResultChar =
1078           ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
1079                             FullSourceLoc(StringToks[i].getLocation(), SM),
1080                             CharByteWidth*8, Diags);
1081 
1082         // Note: our internal rep of wide char tokens is always little-endian.
1083         *ResultPtr++ = ResultChar & 0xFF;
1084 
1085         for (unsigned i = 1, e = CharByteWidth; i != e; ++i)
1086           *ResultPtr++ = ResultChar >> i*8;
1087       }
1088     }
1089   }
1090 
1091   if (Pascal) {
1092     ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
1093     ResultBuf[0] /= CharByteWidth;
1094 
1095     // Verify that pascal strings aren't too large.
1096     if (GetStringLength() > 256) {
1097       if (Diags)
1098         Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1099                       diag::err_pascal_string_too_long)
1100           << SourceRange(StringToks[0].getLocation(),
1101                          StringToks[NumStringToks-1].getLocation());
1102       hadError = true;
1103       return;
1104     }
1105   } else if (Diags) {
1106     // Complain if this string literal has too many characters.
1107     unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1108 
1109     if (GetNumStringChars() > MaxChars)
1110       Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM),
1111                     diag::ext_string_too_long)
1112         << GetNumStringChars() << MaxChars
1113         << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1114         << SourceRange(StringToks[0].getLocation(),
1115                        StringToks[NumStringToks-1].getLocation());
1116   }
1117 }
1118 
1119 
1120 /// copyStringFragment - This function copies from Start to End into ResultPtr.
1121 /// Performs widening for multi-byte characters.
1122 void StringLiteralParser::CopyStringFragment(StringRef Fragment) {
1123   // Copy the character span over.
1124   if (CharByteWidth == 1) {
1125     memcpy(ResultPtr, Fragment.data(), Fragment.size());
1126     ResultPtr += Fragment.size();
1127   } else {
1128     // Note: our internal rep of wide char tokens is always little-endian.
1129     for (StringRef::iterator I=Fragment.begin(), E=Fragment.end(); I!=E; ++I) {
1130       *ResultPtr++ = *I;
1131       // Add zeros at the end.
1132       for (unsigned i = 1, e = CharByteWidth; i != e; ++i)
1133         *ResultPtr++ = 0;
1134     }
1135   }
1136 }
1137 
1138 
1139 /// getOffsetOfStringByte - This function returns the offset of the
1140 /// specified byte of the string data represented by Token.  This handles
1141 /// advancing over escape sequences in the string.
1142 unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
1143                                                     unsigned ByteNo) const {
1144   // Get the spelling of the token.
1145   llvm::SmallString<32> SpellingBuffer;
1146   SpellingBuffer.resize(Tok.getLength());
1147 
1148   bool StringInvalid = false;
1149   const char *SpellingPtr = &SpellingBuffer[0];
1150   unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1151                                        &StringInvalid);
1152   if (StringInvalid)
1153     return 0;
1154 
1155   assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1156          SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
1157 
1158 
1159   const char *SpellingStart = SpellingPtr;
1160   const char *SpellingEnd = SpellingPtr+TokLen;
1161 
1162   // Skip over the leading quote.
1163   assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1164   ++SpellingPtr;
1165 
1166   // Skip over bytes until we find the offset we're looking for.
1167   while (ByteNo) {
1168     assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
1169 
1170     // Step over non-escapes simply.
1171     if (*SpellingPtr != '\\') {
1172       ++SpellingPtr;
1173       --ByteNo;
1174       continue;
1175     }
1176 
1177     // Otherwise, this is an escape character.  Advance over it.
1178     bool HadError = false;
1179     ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
1180                       FullSourceLoc(Tok.getLocation(), SM),
1181                       CharByteWidth*8, Diags);
1182     assert(!HadError && "This method isn't valid on erroneous strings");
1183     --ByteNo;
1184   }
1185 
1186   return SpellingPtr-SpellingStart;
1187 }
1188