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