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