blob: 6f0584b1822f1743338aca2428e7e96622cf180c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Lex/LexDiagnostic.h"
Chris Lattner136f93a2007-07-16 06:55:01 +000018#include "clang/Basic/TargetInfo.h"
Erick Tryzelaare9f195f2009-08-16 23:36:28 +000019#include "llvm/ADT/StringRef.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
21using namespace clang;
22
23/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
24/// not valid.
25static 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/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
33/// either a character or a string literal.
34static unsigned ProcessCharEscape(const char *&ThisTokBuf,
35 const char *ThisTokEnd, bool &HadError,
Chris Lattner91f54ce2010-11-17 06:26:08 +000036 FullSourceLoc Loc, bool IsWide,
37 Diagnostic *Diags, const TargetInfo &Target) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Skip the '\' char.
39 ++ThisTokBuf;
40
41 // We know that this character can't be off the end of the buffer, because
42 // that would have been \", which would not have been the end of string.
43 unsigned ResultChar = *ThisTokBuf++;
44 switch (ResultChar) {
45 // These map to themselves.
46 case '\\': case '\'': case '"': case '?': break;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Reid Spencer5f016e22007-07-11 17:01:13 +000048 // These have fixed mappings.
49 case 'a':
50 // TODO: K&R: the meaning of '\\a' is different in traditional C
51 ResultChar = 7;
52 break;
53 case 'b':
54 ResultChar = 8;
55 break;
56 case 'e':
Chris Lattner91f54ce2010-11-17 06:26:08 +000057 if (Diags)
58 Diags->Report(Loc, diag::ext_nonstandard_escape) << "e";
Reid Spencer5f016e22007-07-11 17:01:13 +000059 ResultChar = 27;
60 break;
Eli Friedman3c548012009-06-10 01:32:39 +000061 case 'E':
Chris Lattner91f54ce2010-11-17 06:26:08 +000062 if (Diags)
63 Diags->Report(Loc, diag::ext_nonstandard_escape) << "E";
Eli Friedman3c548012009-06-10 01:32:39 +000064 ResultChar = 27;
65 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000066 case 'f':
67 ResultChar = 12;
68 break;
69 case 'n':
70 ResultChar = 10;
71 break;
72 case 'r':
73 ResultChar = 13;
74 break;
75 case 't':
76 ResultChar = 9;
77 break;
78 case 'v':
79 ResultChar = 11;
80 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000081 case 'x': { // Hex escape.
82 ResultChar = 0;
83 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner91f54ce2010-11-17 06:26:08 +000084 if (Diags)
85 Diags->Report(Loc, diag::err_hex_escape_no_digits);
Reid Spencer5f016e22007-07-11 17:01:13 +000086 HadError = 1;
87 break;
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Hex escapes are a maximal series of hex digits.
91 bool Overflow = false;
92 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
93 int CharVal = HexDigitValue(ThisTokBuf[0]);
94 if (CharVal == -1) break;
Chris Lattnerc29bbde2008-09-30 20:45:40 +000095 // About to shift out a digit?
96 Overflow |= (ResultChar & 0xF0000000) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ResultChar <<= 4;
98 ResultChar |= CharVal;
99 }
100
101 // See if any bits will be truncated when evaluated as a character.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000102 unsigned CharWidth =
103 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
106 Overflow = true;
107 ResultChar &= ~0U >> (32-CharWidth);
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // Check for overflow.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000111 if (Overflow && Diags) // Too many digits to fit in
112 Diags->Report(Loc, diag::warn_hex_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 break;
114 }
115 case '0': case '1': case '2': case '3':
116 case '4': case '5': case '6': case '7': {
117 // Octal escapes.
118 --ThisTokBuf;
119 ResultChar = 0;
120
121 // Octal escapes are a series of octal digits with maximum length 3.
122 // "\0123" is a two digit sequence equal to "\012" "3".
123 unsigned NumDigits = 0;
124 do {
125 ResultChar <<= 3;
126 ResultChar |= *ThisTokBuf++ - '0';
127 ++NumDigits;
128 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
129 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // Check for overflow. Reject '\777', but not L'\777'.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000132 unsigned CharWidth =
133 IsWide ? Target.getWCharWidth() : Target.getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Chris Lattner91f54ce2010-11-17 06:26:08 +0000136 if (Diags)
137 Diags->Report(Loc, diag::warn_octal_escape_too_large);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 ResultChar &= ~0U >> (32-CharWidth);
139 }
140 break;
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 // Otherwise, these are not valid escapes.
144 case '(': case '{': case '[': case '%':
145 // GCC accepts these as extensions. We warn about them as such though.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000146 if (Diags)
147 Diags->Report(Loc, diag::ext_nonstandard_escape)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000148 << std::string()+(char)ResultChar;
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000149 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 default:
Chris Lattner91f54ce2010-11-17 06:26:08 +0000151 if (Diags == 0)
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000152 break;
153
Chris Lattnerac92d822008-11-22 07:23:31 +0000154 if (isgraph(ThisTokBuf[0]))
Chris Lattner91f54ce2010-11-17 06:26:08 +0000155 Diags->Report(Loc, diag::ext_unknown_escape)
156 << std::string()+(char)ResultChar;
Chris Lattnerac92d822008-11-22 07:23:31 +0000157 else
Chris Lattner91f54ce2010-11-17 06:26:08 +0000158 Diags->Report(Loc, diag::ext_unknown_escape)
159 << "x"+llvm::utohexstr(ResultChar);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 break;
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 return ResultChar;
164}
165
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000166/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
Nico Weber59705ae2010-10-09 00:27:47 +0000167/// return the UTF32.
168static bool ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
169 uint32_t &UcnVal, unsigned short &UcnLen,
Chris Lattner872a45e2010-11-17 06:55:10 +0000170 FullSourceLoc Loc, Diagnostic *Diags,
Chris Lattner6c66f072010-11-17 06:46:14 +0000171 const LangOptions &Features) {
172 if (!Features.CPlusPlus && !Features.C99 && Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000173 Diags->Report(Loc, diag::warn_ucn_not_valid_in_c89);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Steve Naroff4e93b342009-04-01 11:09:15 +0000175 // Save the beginning of the string (for error diagnostics).
176 const char *ThisTokBegin = ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000178 // Skip the '\u' char's.
179 ThisTokBuf += 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000180
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000181 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
Chris Lattner6c66f072010-11-17 06:46:14 +0000182 if (Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000183 Diags->Report(Loc, diag::err_ucn_escape_no_digits);
Nico Weber59705ae2010-10-09 00:27:47 +0000184 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000185 }
Nico Weber59705ae2010-10-09 00:27:47 +0000186 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000187 unsigned short UcnLenSave = UcnLen;
Nico Weber59705ae2010-10-09 00:27:47 +0000188 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000189 int CharVal = HexDigitValue(ThisTokBuf[0]);
190 if (CharVal == -1) break;
191 UcnVal <<= 4;
192 UcnVal |= CharVal;
193 }
194 // If we didn't consume the proper number of digits, there is a problem.
Nico Weber59705ae2010-10-09 00:27:47 +0000195 if (UcnLenSave) {
Chris Lattner872a45e2010-11-17 06:55:10 +0000196 if (Diags) {
Chris Lattner7ef5c272010-11-17 07:05:50 +0000197 SourceLocation L =
198 Lexer::AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin,
199 Loc.getManager(), Features);
200 Diags->Report(FullSourceLoc(L, Loc.getManager()),
201 diag::err_ucn_escape_incomplete);
Chris Lattner872a45e2010-11-17 06:55:10 +0000202 }
Nico Weber59705ae2010-10-09 00:27:47 +0000203 return false;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205 // Check UCN constraints (C99 6.4.3p2).
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000206 if ((UcnVal < 0xa0 &&
207 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
Mike Stump1eb44332009-09-09 15:08:12 +0000208 || (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000209 || (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
Chris Lattner6c66f072010-11-17 06:46:14 +0000210 if (Diags)
Chris Lattner872a45e2010-11-17 06:55:10 +0000211 Diags->Report(Loc, diag::err_ucn_escape_invalid);
Nico Weber59705ae2010-10-09 00:27:47 +0000212 return false;
213 }
214 return true;
215}
216
217/// EncodeUCNEscape - Read the Universal Character Name, check constraints and
218/// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
219/// StringLiteralParser. When we decide to implement UCN's for identifiers,
220/// we will likely rework our support for UCN's.
221static void EncodeUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
Chris Lattnera95880d2010-11-17 07:12:42 +0000222 char *&ResultBuf, bool &HadError,
223 FullSourceLoc Loc, bool wide, Diagnostic *Diags,
224 const LangOptions &Features) {
Nico Weber59705ae2010-10-09 00:27:47 +0000225 typedef uint32_t UTF32;
226 UTF32 UcnVal = 0;
227 unsigned short UcnLen = 0;
Chris Lattnera95880d2010-11-17 07:12:42 +0000228 if (!ProcessUCNEscape(ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, Loc, Diags,
229 Features)) {
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000230 HadError = 1;
231 return;
232 }
Nico Weber59705ae2010-10-09 00:27:47 +0000233
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000234 if (wide) {
Nico Weber59705ae2010-10-09 00:27:47 +0000235 (void)UcnLen;
Chris Lattnera95880d2010-11-17 07:12:42 +0000236 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
Nico Webera0f15b02010-10-06 04:57:26 +0000237
Chris Lattnera95880d2010-11-17 07:12:42 +0000238 if (!Features.ShortWChar) {
Nico Webera0f15b02010-10-06 04:57:26 +0000239 // Note: our internal rep of wide char tokens is always little-endian.
240 *ResultBuf++ = (UcnVal & 0x000000FF);
241 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
242 *ResultBuf++ = (UcnVal & 0x00FF0000) >> 16;
243 *ResultBuf++ = (UcnVal & 0xFF000000) >> 24;
244 return;
245 }
246
247 // Convert to UTF16.
248 if (UcnVal < (UTF32)0xFFFF) {
249 *ResultBuf++ = (UcnVal & 0x000000FF);
250 *ResultBuf++ = (UcnVal & 0x0000FF00) >> 8;
251 return;
252 }
Chris Lattnera95880d2010-11-17 07:12:42 +0000253 if (Diags) Diags->Report(Loc, diag::warn_ucn_escape_too_large);
Nico Webera0f15b02010-10-06 04:57:26 +0000254
255 typedef uint16_t UTF16;
256 UcnVal -= 0x10000;
257 UTF16 surrogate1 = 0xD800 + (UcnVal >> 10);
258 UTF16 surrogate2 = 0xDC00 + (UcnVal & 0x3FF);
259 *ResultBuf++ = (surrogate1 & 0x000000FF);
260 *ResultBuf++ = (surrogate1 & 0x0000FF00) >> 8;
261 *ResultBuf++ = (surrogate2 & 0x000000FF);
262 *ResultBuf++ = (surrogate2 & 0x0000FF00) >> 8;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000263 return;
264 }
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000265 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
266 // The conversion below was inspired by:
267 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
Mike Stump1eb44332009-09-09 15:08:12 +0000268 // First, we determine how many bytes the result will require.
Steve Naroff4e93b342009-04-01 11:09:15 +0000269 typedef uint8_t UTF8;
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000270
271 unsigned short bytesToWrite = 0;
272 if (UcnVal < (UTF32)0x80)
273 bytesToWrite = 1;
274 else if (UcnVal < (UTF32)0x800)
275 bytesToWrite = 2;
276 else if (UcnVal < (UTF32)0x10000)
277 bytesToWrite = 3;
278 else
279 bytesToWrite = 4;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000281 const unsigned byteMask = 0xBF;
282 const unsigned byteMark = 0x80;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000284 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000285 // into the first byte, depending on how many bytes follow.
Mike Stump1eb44332009-09-09 15:08:12 +0000286 static const UTF8 firstByteMark[5] = {
Steve Naroff8a5c0cd2009-03-31 10:29:45 +0000287 0x00, 0x00, 0xC0, 0xE0, 0xF0
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000288 };
289 // Finally, we write the bytes into ResultBuf.
290 ResultBuf += bytesToWrite;
291 switch (bytesToWrite) { // note: everything falls through.
292 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
293 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
294 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
295 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
296 }
297 // Update the buffer.
298 ResultBuf += bytesToWrite;
299}
Reid Spencer5f016e22007-07-11 17:01:13 +0000300
301
302/// integer-constant: [C99 6.4.4.1]
303/// decimal-constant integer-suffix
304/// octal-constant integer-suffix
305/// hexadecimal-constant integer-suffix
Mike Stump1eb44332009-09-09 15:08:12 +0000306/// decimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000307/// nonzero-digit
308/// decimal-constant digit
Mike Stump1eb44332009-09-09 15:08:12 +0000309/// octal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000310/// 0
311/// octal-constant octal-digit
Mike Stump1eb44332009-09-09 15:08:12 +0000312/// hexadecimal-constant:
Reid Spencer5f016e22007-07-11 17:01:13 +0000313/// hexadecimal-prefix hexadecimal-digit
314/// hexadecimal-constant hexadecimal-digit
315/// hexadecimal-prefix: one of
316/// 0x 0X
317/// integer-suffix:
318/// unsigned-suffix [long-suffix]
319/// unsigned-suffix [long-long-suffix]
320/// long-suffix [unsigned-suffix]
321/// long-long-suffix [unsigned-sufix]
322/// nonzero-digit:
323/// 1 2 3 4 5 6 7 8 9
324/// octal-digit:
325/// 0 1 2 3 4 5 6 7
326/// hexadecimal-digit:
327/// 0 1 2 3 4 5 6 7 8 9
328/// a b c d e f
329/// A B C D E F
330/// unsigned-suffix: one of
331/// u U
332/// long-suffix: one of
333/// l L
Mike Stump1eb44332009-09-09 15:08:12 +0000334/// long-long-suffix: one of
Reid Spencer5f016e22007-07-11 17:01:13 +0000335/// ll LL
336///
337/// floating-constant: [C99 6.4.4.2]
338/// TODO: add rules...
339///
Reid Spencer5f016e22007-07-11 17:01:13 +0000340NumericLiteralParser::
341NumericLiteralParser(const char *begin, const char *end,
342 SourceLocation TokLoc, Preprocessor &pp)
343 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattnerc29bbde2008-09-30 20:45:40 +0000345 // This routine assumes that the range begin/end matches the regex for integer
346 // and FP constants (specifically, the 'pp-number' regex), and assumes that
347 // the byte at "*end" is both valid and not part of the regex. Because of
348 // this, it doesn't have to check for 'overscan' in various places.
349 assert(!isalnum(*end) && *end != '.' && *end != '_' &&
350 "Lexer didn't maximally munch?");
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 s = DigitsBegin = begin;
353 saw_exponent = false;
354 saw_period = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 isLong = false;
356 isUnsigned = false;
357 isLongLong = false;
Chris Lattner6e400c22007-08-26 03:29:23 +0000358 isFloat = false;
Chris Lattner506b8de2007-08-26 01:58:14 +0000359 isImaginary = false;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000360 isMicrosoftInteger = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 hadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 if (*s == '0') { // parse radix
Chris Lattner368328c2008-06-30 06:39:54 +0000364 ParseNumberStartingWithZero(TokLoc);
365 if (hadError)
366 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 } else { // the first digit is non-zero
368 radix = 10;
369 s = SkipDigits(s);
370 if (s == ThisTokEnd) {
371 // Done.
Christopher Lamb016765e2007-11-29 06:06:27 +0000372 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000373 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000374 diag::err_invalid_decimal_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000375 hadError = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 return;
377 } else if (*s == '.') {
378 s++;
379 saw_period = true;
380 s = SkipDigits(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000381 }
Chris Lattner4411f462008-09-29 23:12:31 +0000382 if ((*s == 'e' || *s == 'E')) { // exponent
Chris Lattner70f66ab2008-04-20 18:47:55 +0000383 const char *Exponent = s;
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 s++;
385 saw_exponent = true;
386 if (*s == '+' || *s == '-') s++; // sign
387 const char *first_non_digit = SkipDigits(s);
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000388 if (first_non_digit != s) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 s = first_non_digit;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000390 } else {
Chris Lattnerac92d822008-11-22 07:23:31 +0000391 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
392 diag::err_exponent_has_no_digits);
393 hadError = true;
Chris Lattner0b7f69d2008-04-20 18:41:46 +0000394 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
396 }
397 }
398
399 SuffixBegin = s;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner506b8de2007-08-26 01:58:14 +0000401 // Parse the suffix. At this point we can classify whether we have an FP or
402 // integer constant.
403 bool isFPConstant = isFloatingLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner506b8de2007-08-26 01:58:14 +0000405 // Loop over all of the characters of the suffix. If we see something bad,
406 // we break out of the loop.
407 for (; s != ThisTokEnd; ++s) {
408 switch (*s) {
409 case 'f': // FP Suffix for "float"
410 case 'F':
411 if (!isFPConstant) break; // Error for integer constant.
Chris Lattner6e400c22007-08-26 03:29:23 +0000412 if (isFloat || isLong) break; // FF, LF invalid.
413 isFloat = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000414 continue; // Success.
415 case 'u':
416 case 'U':
417 if (isFPConstant) break; // Error for floating constant.
418 if (isUnsigned) break; // Cannot be repeated.
419 isUnsigned = true;
420 continue; // Success.
421 case 'l':
422 case 'L':
423 if (isLong || isLongLong) break; // Cannot be repeated.
Chris Lattner6e400c22007-08-26 03:29:23 +0000424 if (isFloat) break; // LF invalid.
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner506b8de2007-08-26 01:58:14 +0000426 // Check for long long. The L's need to be adjacent and the same case.
427 if (s+1 != ThisTokEnd && s[1] == s[0]) {
428 if (isFPConstant) break; // long long invalid for floats.
429 isLongLong = true;
430 ++s; // Eat both of them.
431 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 isLong = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000434 continue; // Success.
435 case 'i':
Chris Lattnerc6374152010-10-14 00:24:10 +0000436 case 'I':
Steve Naroff0c29b222008-04-04 21:02:54 +0000437 if (PP.getLangOptions().Microsoft) {
Fariborz Jahaniana8be02b2010-01-22 21:36:53 +0000438 if (isFPConstant || isLong || isLongLong) break;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000439
Steve Naroff0c29b222008-04-04 21:02:54 +0000440 // Allow i8, i16, i32, i64, and i128.
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000441 if (s + 1 != ThisTokEnd) {
442 switch (s[1]) {
443 case '8':
444 s += 2; // i8 suffix
445 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000446 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000447 case '1':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000448 if (s + 2 == ThisTokEnd) break;
449 if (s[2] == '6') s += 3; // i16 suffix
450 else if (s[2] == '2') {
451 if (s + 3 == ThisTokEnd) break;
452 if (s[3] == '8') s += 4; // i128 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000453 }
454 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000455 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000456 case '3':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000457 if (s + 2 == ThisTokEnd) break;
458 if (s[2] == '2') s += 3; // i32 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000459 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000460 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000461 case '6':
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000462 if (s + 2 == ThisTokEnd) break;
463 if (s[2] == '4') s += 3; // i64 suffix
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000464 isMicrosoftInteger = true;
Nuno Lopes6e8c7ac2009-11-28 13:37:52 +0000465 break;
Mike Stumpb79fe2d2009-10-08 22:55:36 +0000466 default:
467 break;
468 }
469 break;
Steve Naroff0c29b222008-04-04 21:02:54 +0000470 }
Steve Naroff0c29b222008-04-04 21:02:54 +0000471 }
472 // fall through.
Chris Lattner506b8de2007-08-26 01:58:14 +0000473 case 'j':
474 case 'J':
475 if (isImaginary) break; // Cannot be repeated.
476 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
477 diag::ext_imaginary_constant);
478 isImaginary = true;
479 continue; // Success.
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 }
Chris Lattner506b8de2007-08-26 01:58:14 +0000481 // If we reached here, there was an error.
482 break;
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattner506b8de2007-08-26 01:58:14 +0000485 // Report an error if there are any.
486 if (s != ThisTokEnd) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000487 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
488 isFPConstant ? diag::err_invalid_suffix_float_constant :
489 diag::err_invalid_suffix_integer_constant)
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000490 << llvm::StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
Chris Lattnerac92d822008-11-22 07:23:31 +0000491 hadError = true;
Chris Lattner506b8de2007-08-26 01:58:14 +0000492 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 }
494}
495
Chris Lattner368328c2008-06-30 06:39:54 +0000496/// ParseNumberStartingWithZero - This method is called when the first character
497/// of the number is found to be a zero. This means it is either an octal
498/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
Mike Stump1eb44332009-09-09 15:08:12 +0000499/// a floating point number (01239.123e4). Eat the prefix, determining the
Chris Lattner368328c2008-06-30 06:39:54 +0000500/// radix etc.
501void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
502 assert(s[0] == '0' && "Invalid method call");
503 s++;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner368328c2008-06-30 06:39:54 +0000505 // Handle a hex number like 0x1234.
506 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
507 s++;
508 radix = 16;
509 DigitsBegin = s;
510 s = SkipHexDigits(s);
511 if (s == ThisTokEnd) {
512 // Done.
513 } else if (*s == '.') {
514 s++;
515 saw_period = true;
516 s = SkipHexDigits(s);
517 }
518 // A binary exponent can appear with or with a '.'. If dotted, the
Mike Stump1eb44332009-09-09 15:08:12 +0000519 // binary exponent is required.
Sean Hunt8c723402010-01-10 23:37:56 +0000520 if ((*s == 'p' || *s == 'P') && !PP.getLangOptions().CPlusPlus0x) {
Chris Lattner368328c2008-06-30 06:39:54 +0000521 const char *Exponent = s;
522 s++;
523 saw_exponent = true;
524 if (*s == '+' || *s == '-') s++; // sign
525 const char *first_non_digit = SkipDigits(s);
Chris Lattner6ea62382008-07-25 18:18:34 +0000526 if (first_non_digit == s) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000527 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
528 diag::err_exponent_has_no_digits);
529 hadError = true;
Chris Lattner6ea62382008-07-25 18:18:34 +0000530 return;
Chris Lattner368328c2008-06-30 06:39:54 +0000531 }
Chris Lattner6ea62382008-07-25 18:18:34 +0000532 s = first_non_digit;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Sean Hunt8c723402010-01-10 23:37:56 +0000534 // In C++0x, we cannot support hexadecmial floating literals because
535 // they conflict with user-defined literals, so we warn in previous
536 // versions of C++ by default.
537 if (PP.getLangOptions().CPlusPlus)
538 PP.Diag(TokLoc, diag::ext_hexconstant_cplusplus);
539 else if (!PP.getLangOptions().HexFloats)
Chris Lattnerac92d822008-11-22 07:23:31 +0000540 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
Chris Lattner368328c2008-06-30 06:39:54 +0000541 } else if (saw_period) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000542 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
543 diag::err_hexconstant_requires_exponent);
544 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000545 }
546 return;
547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner368328c2008-06-30 06:39:54 +0000549 // Handle simple binary numbers 0b01010
550 if (*s == 'b' || *s == 'B') {
551 // 0b101010 is a GCC extension.
Chris Lattner413d3552008-06-30 06:44:49 +0000552 PP.Diag(TokLoc, diag::ext_binary_literal);
Chris Lattner368328c2008-06-30 06:39:54 +0000553 ++s;
554 radix = 2;
555 DigitsBegin = s;
556 s = SkipBinaryDigits(s);
557 if (s == ThisTokEnd) {
558 // Done.
559 } else if (isxdigit(*s)) {
Chris Lattnerac92d822008-11-22 07:23:31 +0000560 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000561 diag::err_invalid_binary_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000562 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000563 }
Chris Lattner413d3552008-06-30 06:44:49 +0000564 // Other suffixes will be diagnosed by the caller.
Chris Lattner368328c2008-06-30 06:39:54 +0000565 return;
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner368328c2008-06-30 06:39:54 +0000568 // For now, the radix is set to 8. If we discover that we have a
569 // floating point constant, the radix will change to 10. Octal floating
Mike Stump1eb44332009-09-09 15:08:12 +0000570 // point constants are not permitted (only decimal and hexadecimal).
Chris Lattner368328c2008-06-30 06:39:54 +0000571 radix = 8;
572 DigitsBegin = s;
573 s = SkipOctalDigits(s);
574 if (s == ThisTokEnd)
575 return; // Done, simple octal number like 01234
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner413d3552008-06-30 06:44:49 +0000577 // If we have some other non-octal digit that *is* a decimal digit, see if
578 // this is part of a floating point number like 094.123 or 09e1.
579 if (isdigit(*s)) {
580 const char *EndDecimal = SkipDigits(s);
581 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
582 s = EndDecimal;
583 radix = 10;
584 }
585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner413d3552008-06-30 06:44:49 +0000587 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
588 // the code is using an incorrect base.
Chris Lattner368328c2008-06-30 06:39:54 +0000589 if (isxdigit(*s) && *s != 'e' && *s != 'E') {
Chris Lattnerac92d822008-11-22 07:23:31 +0000590 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000591 diag::err_invalid_octal_digit) << llvm::StringRef(s, 1);
Chris Lattnerac92d822008-11-22 07:23:31 +0000592 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000593 return;
594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattner368328c2008-06-30 06:39:54 +0000596 if (*s == '.') {
597 s++;
598 radix = 10;
599 saw_period = true;
Chris Lattner413d3552008-06-30 06:44:49 +0000600 s = SkipDigits(s); // Skip suffix.
Chris Lattner368328c2008-06-30 06:39:54 +0000601 }
602 if (*s == 'e' || *s == 'E') { // exponent
603 const char *Exponent = s;
604 s++;
605 radix = 10;
606 saw_exponent = true;
607 if (*s == '+' || *s == '-') s++; // sign
608 const char *first_non_digit = SkipDigits(s);
609 if (first_non_digit != s) {
610 s = first_non_digit;
611 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000612 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
Chris Lattnerac92d822008-11-22 07:23:31 +0000613 diag::err_exponent_has_no_digits);
614 hadError = true;
Chris Lattner368328c2008-06-30 06:39:54 +0000615 return;
616 }
617 }
618}
619
620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621/// GetIntegerValue - Convert this numeric literal value to an APInt that
622/// matches Val's input width. If there is an overflow, set Val to the low bits
623/// of the result and return true. Otherwise, return false.
624bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
Daniel Dunbara179be32008-10-16 07:32:01 +0000625 // Fast path: Compute a conservative bound on the maximum number of
626 // bits per digit in this radix. If we can't possibly overflow a
627 // uint64 based on that bound then do the simple conversion to
628 // integer. This avoids the expensive overflow checking below, and
629 // handles the common cases that matter (small decimal integers and
630 // hex/octal values which don't overflow).
631 unsigned MaxBitsPerDigit = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000632 while ((1U << MaxBitsPerDigit) < radix)
Daniel Dunbara179be32008-10-16 07:32:01 +0000633 MaxBitsPerDigit += 1;
634 if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
635 uint64_t N = 0;
636 for (s = DigitsBegin; s != SuffixBegin; ++s)
637 N = N*radix + HexDigitValue(*s);
638
639 // This will truncate the value to Val's input width. Simply check
640 // for overflow by comparing.
641 Val = N;
642 return Val.getZExtValue() != N;
643 }
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 Val = 0;
646 s = DigitsBegin;
647
648 llvm::APInt RadixVal(Val.getBitWidth(), radix);
649 llvm::APInt CharVal(Val.getBitWidth(), 0);
650 llvm::APInt OldVal = Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 bool OverflowOccurred = false;
653 while (s < SuffixBegin) {
654 unsigned C = HexDigitValue(*s++);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // If this letter is out of bound for this radix, reject it.
657 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 CharVal = C;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 // Add the digit to the value in the appropriate radix. If adding in digits
662 // made the value smaller, then this overflowed.
663 OldVal = Val;
664
665 // Multiply by radix, did overflow occur on the multiply?
666 Val *= RadixVal;
667 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
668
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // Add value, did overflow occur on the value?
Daniel Dunbard70cb642008-10-16 06:39:30 +0000670 // (a + b) ult b <=> overflow
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 Val += CharVal;
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 OverflowOccurred |= Val.ult(CharVal);
673 }
674 return OverflowOccurred;
675}
676
John McCall94c939d2009-12-24 09:08:04 +0000677llvm::APFloat::opStatus
678NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Ted Kremenek427d5af2007-11-26 23:12:30 +0000679 using llvm::APFloat;
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000680 using llvm::StringRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Erick Tryzelaare9f195f2009-08-16 23:36:28 +0000682 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
John McCall94c939d2009-12-24 09:08:04 +0000683 return Result.convertFromString(StringRef(ThisTokBegin, n),
684 APFloat::rmNearestTiesToEven);
Reid Spencer5f016e22007-07-11 17:01:13 +0000685}
686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687
688CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
689 SourceLocation Loc, Preprocessor &PP) {
690 // At this point we know that the character matches the regex "L?'.*'".
691 HadError = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // Determine if this is a wide character.
694 IsWide = begin[0] == 'L';
695 if (IsWide) ++begin;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // Skip over the entry quote.
698 assert(begin[0] == '\'' && "Invalid token lexed");
699 ++begin;
700
Mike Stump1eb44332009-09-09 15:08:12 +0000701 // FIXME: The "Value" is an uint64_t so we can handle char literals of
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000702 // upto 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 // FIXME: This extensively assumes that 'char' is 8-bits.
Chris Lattner98be4942008-03-05 18:54:05 +0000704 assert(PP.getTargetInfo().getCharWidth() == 8 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 "Assumes char is 8 bits");
Chris Lattnere3ad8812009-04-28 21:51:46 +0000706 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
707 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
708 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
709 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
710 "Assumes sizeof(wchar) on target is <= 64");
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000711
Mike Stump1eb44332009-09-09 15:08:12 +0000712 // This is what we will use for overflow detection
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000713 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattnere3ad8812009-04-28 21:51:46 +0000715 unsigned NumCharsSoFar = 0;
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000716 bool Warned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 while (begin[0] != '\'') {
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000718 uint64_t ResultChar;
Nico Weber59705ae2010-10-09 00:27:47 +0000719
720 // Is this a Universal Character Name escape?
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 if (begin[0] != '\\') // If this is a normal character, consume it.
722 ResultChar = *begin++;
Nico Weber59705ae2010-10-09 00:27:47 +0000723 else { // Otherwise, this is an escape character.
724 // Check for UCN.
725 if (begin[1] == 'u' || begin[1] == 'U') {
726 uint32_t utf32 = 0;
727 unsigned short UcnLen = 0;
Chris Lattner872a45e2010-11-17 06:55:10 +0000728 if (!ProcessUCNEscape(begin, end, utf32, UcnLen,
729 FullSourceLoc(Loc, PP.getSourceManager()),
Chris Lattner6c66f072010-11-17 06:46:14 +0000730 &PP.getDiagnostics(), PP.getLangOptions())) {
Nico Weber59705ae2010-10-09 00:27:47 +0000731 HadError = 1;
732 }
733 ResultChar = utf32;
734 } else {
735 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000736 ResultChar = ProcessCharEscape(begin, end, HadError,
737 FullSourceLoc(Loc,PP.getSourceManager()),
738 IsWide,
739 &PP.getDiagnostics(), PP.getTargetInfo());
Nico Weber59705ae2010-10-09 00:27:47 +0000740 }
741 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000742
743 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
744 // implementation defined (C99 6.4.4.4p10).
Chris Lattnere3ad8812009-04-28 21:51:46 +0000745 if (NumCharsSoFar) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 if (IsWide) {
747 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000748 LitVal = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 } else {
750 // Narrow character literals act as though their value is concatenated
Chris Lattnere3ad8812009-04-28 21:51:46 +0000751 // in this implementation, but warn on overflow.
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000752 if (LitVal.countLeadingZeros() < 8 && !Warned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 PP.Diag(Loc, diag::warn_char_constant_too_large);
Chris Lattner1c6c64b2010-04-16 23:44:05 +0000754 Warned = true;
755 }
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000756 LitVal <<= 8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 }
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000760 LitVal = LitVal + ResultChar;
Chris Lattnere3ad8812009-04-28 21:51:46 +0000761 ++NumCharsSoFar;
762 }
763
764 // If this is the second character being processed, do special handling.
765 if (NumCharsSoFar > 1) {
766 // Warn about discarding the top bits for multi-char wide-character
767 // constants (L'abcd').
768 if (IsWide)
769 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
770 else if (NumCharsSoFar != 4)
771 PP.Diag(Loc, diag::ext_multichar_character_literal);
772 else
773 PP.Diag(Loc, diag::ext_four_char_character_literal);
Eli Friedman2a1c3632009-06-01 05:25:02 +0000774 IsMultiChar = true;
Daniel Dunbar930b71a2009-07-29 01:46:05 +0000775 } else
776 IsMultiChar = false;
Sanjiv Gupta4bc11af2009-04-21 02:21:29 +0000777
778 // Transfer the value from APInt to uint64_t
779 Value = LitVal.getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Nico Weber59705ae2010-10-09 00:27:47 +0000781 if (IsWide && PP.getLangOptions().ShortWChar && Value > 0xFFFF)
782 PP.Diag(Loc, diag::warn_ucn_escape_too_large);
783
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
785 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
786 // character constants are not sign extended in the this implementation:
787 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
Chris Lattnere3ad8812009-04-28 21:51:46 +0000788 if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
Eli Friedman15b91762009-06-05 07:05:05 +0000789 PP.getLangOptions().CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 Value = (signed char)Value;
791}
792
793
794/// string-literal: [C99 6.4.5]
795/// " [s-char-sequence] "
796/// L" [s-char-sequence] "
797/// s-char-sequence:
798/// s-char
799/// s-char-sequence s-char
800/// s-char:
801/// any source character except the double quote ",
802/// backslash \, or newline character
803/// escape-character
804/// universal-character-name
805/// escape-character: [C99 6.4.4.4]
806/// \ escape-code
807/// universal-character-name
808/// escape-code:
809/// character-escape-code
810/// octal-escape-code
811/// hex-escape-code
812/// character-escape-code: one of
813/// n t b r f v a
814/// \ ' " ?
815/// octal-escape-code:
816/// octal-digit
817/// octal-digit octal-digit
818/// octal-digit octal-digit octal-digit
819/// hex-escape-code:
820/// x hex-digit
821/// hex-escape-code hex-digit
822/// universal-character-name:
823/// \u hex-quad
824/// \U hex-quad hex-quad
825/// hex-quad:
826/// hex-digit hex-digit hex-digit hex-digit
827///
828StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000829StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Chris Lattner6c66f072010-11-17 06:46:14 +0000830 Preprocessor &pp, bool Complain)
831 : PP(pp), SM(PP.getSourceManager()), Features(PP.getLangOptions()),
832 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 // Scan all of the string portions, remember the max individual token length,
834 // computing a bound on the concatenated string length, and see whether any
835 // piece is a wide-string. If any of the string portions is a wide-string
836 // literal, the result is a wide-string literal [C99 6.4.5p4].
Sean Hunt6cf75022010-08-30 17:47:05 +0000837 MaxTokenLength = StringToks[0].getLength();
838 SizeBound = StringToks[0].getLength()-2; // -2 for "".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000839 AnyWide = StringToks[0].is(tok::wide_string_literal);
Sean Hunt6cf75022010-08-30 17:47:05 +0000840
841 hadError = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842
843 // Implement Translation Phase #6: concatenation of string literals
844 /// (C99 5.1.1.2p1). The common case is only one string fragment.
845 for (unsigned i = 1; i != NumStringToks; ++i) {
846 // The string could be shorter than this if it needs cleaning, but this is a
847 // reasonable bound, which is all we need.
Sean Hunt6cf75022010-08-30 17:47:05 +0000848 SizeBound += StringToks[i].getLength()-2; // -2 for "".
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 // Remember maximum string piece length.
Sean Hunt6cf75022010-08-30 17:47:05 +0000851 if (StringToks[i].getLength() > MaxTokenLength)
852 MaxTokenLength = StringToks[i].getLength();
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 // Remember if we see any wide strings.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000855 AnyWide |= StringToks[i].is(tok::wide_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 }
Chris Lattnerdbb1ecc2009-02-26 23:01:51 +0000857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // Include space for the null terminator.
859 ++SizeBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 // TODO: K&R warning: "traditional C rejects string constant concatenation"
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
864 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
865 wchar_tByteWidth = ~0U;
866 if (AnyWide) {
Chris Lattnera95880d2010-11-17 07:12:42 +0000867 wchar_tByteWidth = Target.getWCharWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
869 wchar_tByteWidth /= 8;
870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 // The output buffer size needs to be large enough to hold wide characters.
873 // This is a worst-case assumption which basically corresponds to L"" "long".
874 if (AnyWide)
875 SizeBound *= wchar_tByteWidth;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 // Size the temporary buffer to hold the result string data.
878 ResultBuf.resize(SizeBound);
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 // Likewise, but for each string piece.
881 llvm::SmallString<512> TokenBuf;
882 TokenBuf.resize(MaxTokenLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 // Loop over all the strings, getting their spelling, and expanding them to
885 // wide strings as appropriate.
886 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Anders Carlssonee98ac52007-10-15 02:50:23 +0000888 Pascal = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
891 const char *ThisTokBuf = &TokenBuf[0];
892 // Get the spelling of the token, which eliminates trigraphs, etc. We know
893 // that ThisTokBuf points to a buffer that is big enough for the whole token
894 // and 'spelled' tokens can only shrink.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000895 bool StringInvalid = false;
896 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf,
Sean Hunt6cf75022010-08-30 17:47:05 +0000897 &StringInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000898 if (StringInvalid) {
899 hadError = 1;
900 continue;
901 }
902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000904 bool wide = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 // TODO: Input character set mapping support.
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // Skip L marker for wide strings.
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000908 if (ThisTokBuf[0] == 'L') {
909 wide = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 ++ThisTokBuf;
Fariborz Jahanian56bedef2010-08-31 23:34:27 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
914 ++ThisTokBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Anders Carlssonee98ac52007-10-15 02:50:23 +0000916 // Check if this is a pascal string
Chris Lattnera95880d2010-11-17 07:12:42 +0000917 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
Anders Carlssonee98ac52007-10-15 02:50:23 +0000918 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Anders Carlssonee98ac52007-10-15 02:50:23 +0000920 // If the \p sequence is found in the first token, we have a pascal string
921 // Otherwise, if we already have a pascal string, ignore the first \p
922 if (i == 0) {
923 ++ThisTokBuf;
924 Pascal = true;
925 } else if (Pascal)
926 ThisTokBuf += 2;
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 while (ThisTokBuf != ThisTokEnd) {
930 // Is this a span of non-escape characters?
931 if (ThisTokBuf[0] != '\\') {
932 const char *InStart = ThisTokBuf;
933 do {
934 ++ThisTokBuf;
935 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 // Copy the character span over.
938 unsigned Len = ThisTokBuf-InStart;
939 if (!AnyWide) {
940 memcpy(ResultPtr, InStart, Len);
941 ResultPtr += Len;
942 } else {
943 // Note: our internal rep of wide char tokens is always little-endian.
944 for (; Len; --Len, ++InStart) {
945 *ResultPtr++ = InStart[0];
946 // Add zeros at the end.
947 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000948 *ResultPtr++ = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 }
950 }
951 continue;
952 }
Steve Naroff4e93b342009-04-01 11:09:15 +0000953 // Is this a Universal Character Name escape?
Steve Naroff0e3e3eb2009-03-30 23:46:03 +0000954 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
Nico Weber59705ae2010-10-09 00:27:47 +0000955 EncodeUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
Chris Lattnera95880d2010-11-17 07:12:42 +0000956 hadError, FullSourceLoc(StringToks[i].getLocation(),SM),
957 wide, Diags, Features);
Steve Naroff4e93b342009-04-01 11:09:15 +0000958 continue;
959 }
960 // Otherwise, this is a non-UCN escape character. Process it.
Chris Lattner91f54ce2010-11-17 06:26:08 +0000961 unsigned ResultChar =
962 ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
Chris Lattner6c66f072010-11-17 06:46:14 +0000963 FullSourceLoc(StringToks[i].getLocation(), SM),
964 AnyWide, Diags, Target);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Steve Naroff4e93b342009-04-01 11:09:15 +0000966 // Note: our internal rep of wide char tokens is always little-endian.
967 *ResultPtr++ = ResultChar & 0xFF;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Steve Naroff4e93b342009-04-01 11:09:15 +0000969 if (AnyWide) {
970 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
971 *ResultPtr++ = ResultChar >> i*8;
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 }
973 }
974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000976 if (Pascal) {
Anders Carlssonee98ac52007-10-15 02:50:23 +0000977 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
Fariborz Jahanian64a80342010-05-28 19:40:48 +0000978 if (AnyWide)
979 ResultBuf[0] /= wchar_tByteWidth;
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000980
981 // Verify that pascal strings aren't too large.
Douglas Gregorb90f4b32010-05-26 05:35:51 +0000982 if (GetStringLength() > 256 && Complain) {
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000983 PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
984 << SourceRange(StringToks[0].getLocation(),
985 StringToks[NumStringToks-1].getLocation());
Eli Friedman57d7dde2009-04-01 03:17:08 +0000986 hadError = 1;
987 return;
988 }
Douglas Gregor427c4922010-07-20 14:33:20 +0000989 } else if (Complain) {
990 // Complain if this string literal has too many characters.
Chris Lattnera95880d2010-11-17 07:12:42 +0000991 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
Douglas Gregor427c4922010-07-20 14:33:20 +0000992
993 if (GetNumStringChars() > MaxChars)
994 PP.Diag(StringToks[0].getLocation(), diag::ext_string_too_long)
995 << GetNumStringChars() << MaxChars
Chris Lattnera95880d2010-11-17 07:12:42 +0000996 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
Douglas Gregor427c4922010-07-20 14:33:20 +0000997 << SourceRange(StringToks[0].getLocation(),
998 StringToks[NumStringToks-1].getLocation());
Chris Lattnerbbee00b2009-01-16 18:51:42 +0000999 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001000}
Chris Lattner719e6152009-02-18 19:21:10 +00001001
1002
1003/// getOffsetOfStringByte - This function returns the offset of the
1004/// specified byte of the string data represented by Token. This handles
1005/// advancing over escape sequences in the string.
1006unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
Chris Lattner6c66f072010-11-17 06:46:14 +00001007 unsigned ByteNo) const {
Chris Lattner719e6152009-02-18 19:21:10 +00001008 // Get the spelling of the token.
Chris Lattnerca1475e2010-11-17 06:35:43 +00001009 llvm::SmallString<32> SpellingBuffer;
Sean Hunt6cf75022010-08-30 17:47:05 +00001010 SpellingBuffer.resize(Tok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregor50f6af72010-03-16 05:20:39 +00001012 bool StringInvalid = false;
Chris Lattner719e6152009-02-18 19:21:10 +00001013 const char *SpellingPtr = &SpellingBuffer[0];
Chris Lattnerca1475e2010-11-17 06:35:43 +00001014 unsigned TokLen = Preprocessor::getSpelling(Tok, SpellingPtr, SM, Features,
Chris Lattner48cf9822010-11-17 06:31:48 +00001015 &StringInvalid);
Chris Lattner91f54ce2010-11-17 06:26:08 +00001016 if (StringInvalid)
Douglas Gregor50f6af72010-03-16 05:20:39 +00001017 return 0;
Chris Lattner719e6152009-02-18 19:21:10 +00001018
1019 assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
1020
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattner719e6152009-02-18 19:21:10 +00001022 const char *SpellingStart = SpellingPtr;
1023 const char *SpellingEnd = SpellingPtr+TokLen;
1024
1025 // Skip over the leading quote.
1026 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1027 ++SpellingPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattner719e6152009-02-18 19:21:10 +00001029 // Skip over bytes until we find the offset we're looking for.
1030 while (ByteNo) {
1031 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattner719e6152009-02-18 19:21:10 +00001033 // Step over non-escapes simply.
1034 if (*SpellingPtr != '\\') {
1035 ++SpellingPtr;
1036 --ByteNo;
1037 continue;
1038 }
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattner719e6152009-02-18 19:21:10 +00001040 // Otherwise, this is an escape character. Advance over it.
1041 bool HadError = false;
1042 ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
Chris Lattnerca1475e2010-11-17 06:35:43 +00001043 FullSourceLoc(Tok.getLocation(), SM),
Chris Lattner91f54ce2010-11-17 06:26:08 +00001044 false, Diags, Target);
Chris Lattner719e6152009-02-18 19:21:10 +00001045 assert(!HadError && "This method isn't valid on erroneous strings");
1046 --ByteNo;
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Chris Lattner719e6152009-02-18 19:21:10 +00001049 return SpellingPtr-SpellingStart;
1050}