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" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 16 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "clang/Lex/LexDiagnostic.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ConvertUTF.h" |
David Blaikie | 76bd3c8 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 23 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 26 | static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) { |
| 27 | switch (kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 28 | default: llvm_unreachable("Unknown token type!"); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 29 | case tok::char_constant: |
| 30 | case tok::string_literal: |
| 31 | case tok::utf8_string_literal: |
| 32 | return Target.getCharWidth(); |
| 33 | case tok::wide_char_constant: |
| 34 | case tok::wide_string_literal: |
| 35 | return Target.getWCharWidth(); |
| 36 | case tok::utf16_char_constant: |
| 37 | case tok::utf16_string_literal: |
| 38 | return Target.getChar16Width(); |
| 39 | case tok::utf32_char_constant: |
| 40 | case tok::utf32_string_literal: |
| 41 | return Target.getChar32Width(); |
| 42 | } |
| 43 | } |
| 44 | |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 45 | static CharSourceRange MakeCharSourceRange(const LangOptions &Features, |
| 46 | FullSourceLoc TokLoc, |
| 47 | const char *TokBegin, |
| 48 | const char *TokRangeBegin, |
| 49 | const char *TokRangeEnd) { |
| 50 | SourceLocation Begin = |
| 51 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
| 52 | TokLoc.getManager(), Features); |
| 53 | SourceLocation End = |
| 54 | Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin, |
| 55 | TokLoc.getManager(), Features); |
| 56 | return CharSourceRange::getCharRange(Begin, End); |
| 57 | } |
| 58 | |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 59 | /// \brief Produce a diagnostic highlighting some portion of a literal. |
| 60 | /// |
| 61 | /// Emits the diagnostic \p DiagID, highlighting the range of characters from |
| 62 | /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be |
| 63 | /// a substring of a spelling buffer for the token beginning at \p TokBegin. |
| 64 | static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, |
| 65 | const LangOptions &Features, FullSourceLoc TokLoc, |
| 66 | const char *TokBegin, const char *TokRangeBegin, |
| 67 | const char *TokRangeEnd, unsigned DiagID) { |
| 68 | SourceLocation Begin = |
| 69 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
| 70 | TokLoc.getManager(), Features); |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 71 | return Diags->Report(Begin, DiagID) << |
| 72 | MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd); |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 75 | /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in |
| 76 | /// either a character or a string literal. |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 77 | static unsigned ProcessCharEscape(const char *ThisTokBegin, |
| 78 | const char *&ThisTokBuf, |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 79 | const char *ThisTokEnd, bool &HadError, |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 80 | FullSourceLoc Loc, unsigned CharWidth, |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 81 | DiagnosticsEngine *Diags, |
| 82 | const LangOptions &Features) { |
| 83 | const char *EscapeBegin = ThisTokBuf; |
| 84 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 85 | // Skip the '\' char. |
| 86 | ++ThisTokBuf; |
| 87 | |
| 88 | // We know that this character can't be off the end of the buffer, because |
| 89 | // that would have been \", which would not have been the end of string. |
| 90 | unsigned ResultChar = *ThisTokBuf++; |
| 91 | switch (ResultChar) { |
| 92 | // These map to themselves. |
| 93 | case '\\': case '\'': case '"': case '?': break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 95 | // These have fixed mappings. |
| 96 | case 'a': |
| 97 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 98 | ResultChar = 7; |
| 99 | break; |
| 100 | case 'b': |
| 101 | ResultChar = 8; |
| 102 | break; |
| 103 | case 'e': |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 104 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 105 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 106 | diag::ext_nonstandard_escape) << "e"; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 107 | ResultChar = 27; |
| 108 | break; |
Eli Friedman | 28a00aa | 2009-06-10 01:32:39 +0000 | [diff] [blame] | 109 | case 'E': |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 110 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 111 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 112 | diag::ext_nonstandard_escape) << "E"; |
Eli Friedman | 28a00aa | 2009-06-10 01:32:39 +0000 | [diff] [blame] | 113 | ResultChar = 27; |
| 114 | break; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 115 | case 'f': |
| 116 | ResultChar = 12; |
| 117 | break; |
| 118 | case 'n': |
| 119 | ResultChar = 10; |
| 120 | break; |
| 121 | case 'r': |
| 122 | ResultChar = 13; |
| 123 | break; |
| 124 | case 't': |
| 125 | ResultChar = 9; |
| 126 | break; |
| 127 | case 'v': |
| 128 | ResultChar = 11; |
| 129 | break; |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 130 | case 'x': { // Hex escape. |
| 131 | ResultChar = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 132 | if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 133 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 134 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
Jordan Rose | aa89cf1 | 2013-01-24 20:50:13 +0000 | [diff] [blame] | 135 | diag::err_hex_escape_no_digits) << "x"; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 136 | HadError = 1; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 140 | // Hex escapes are a maximal series of hex digits. |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 141 | bool Overflow = false; |
| 142 | for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { |
Jordan Rose | 78ed86a | 2013-01-18 22:33:58 +0000 | [diff] [blame] | 143 | int CharVal = llvm::hexDigitValue(ThisTokBuf[0]); |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 144 | if (CharVal == -1) break; |
Chris Lattner | 59f09b6 | 2008-09-30 20:45:40 +0000 | [diff] [blame] | 145 | // About to shift out a digit? |
| 146 | Overflow |= (ResultChar & 0xF0000000) ? true : false; |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 147 | ResultChar <<= 4; |
| 148 | ResultChar |= CharVal; |
| 149 | } |
| 150 | |
| 151 | // See if any bits will be truncated when evaluated as a character. |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 152 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 153 | Overflow = true; |
| 154 | ResultChar &= ~0U >> (32-CharWidth); |
| 155 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 157 | // Check for overflow. |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 158 | if (Overflow && Diags) // Too many digits to fit in |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 159 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
Eli Friedman | 088d39a | 2013-07-23 00:25:18 +0000 | [diff] [blame] | 160 | diag::err_hex_escape_too_large); |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 161 | break; |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 163 | case '0': case '1': case '2': case '3': |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 164 | case '4': case '5': case '6': case '7': { |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 165 | // Octal escapes. |
Chris Lattner | 3f4b6e3 | 2007-06-09 06:20:47 +0000 | [diff] [blame] | 166 | --ThisTokBuf; |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 167 | ResultChar = 0; |
| 168 | |
| 169 | // Octal escapes are a series of octal digits with maximum length 3. |
| 170 | // "\0123" is a two digit sequence equal to "\012" "3". |
| 171 | unsigned NumDigits = 0; |
| 172 | do { |
| 173 | ResultChar <<= 3; |
| 174 | ResultChar |= *ThisTokBuf++ - '0'; |
| 175 | ++NumDigits; |
| 176 | } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && |
| 177 | ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 179 | // Check for overflow. Reject '\777', but not L'\777'. |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 180 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 181 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 182 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
Eli Friedman | 088d39a | 2013-07-23 00:25:18 +0000 | [diff] [blame] | 183 | diag::err_octal_escape_too_large); |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 184 | ResultChar &= ~0U >> (32-CharWidth); |
| 185 | } |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 186 | break; |
Chris Lattner | 812eda8 | 2007-05-20 05:17:04 +0000 | [diff] [blame] | 187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 189 | // Otherwise, these are not valid escapes. |
| 190 | case '(': case '{': case '[': case '%': |
| 191 | // GCC accepts these as extensions. We warn about them as such though. |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 192 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 193 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 194 | diag::ext_nonstandard_escape) |
| 195 | << std::string(1, ResultChar); |
Eli Friedman | 5d72d41 | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 196 | break; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 197 | default: |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 198 | if (Diags == 0) |
Douglas Gregor | 9af0302 | 2010-05-26 05:35:51 +0000 | [diff] [blame] | 199 | break; |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 200 | |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 201 | if (isPrintable(ResultChar)) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 202 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 203 | diag::ext_unknown_escape) |
| 204 | << std::string(1, ResultChar); |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 205 | else |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 206 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 207 | diag::ext_unknown_escape) |
| 208 | << "x" + llvm::utohexstr(ResultChar); |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 212 | return ResultChar; |
| 213 | } |
| 214 | |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 215 | /// ProcessUCNEscape - Read the Universal Character Name, check constraints and |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 216 | /// return the UTF32. |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 217 | static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 218 | const char *ThisTokEnd, |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 219 | uint32_t &UcnVal, unsigned short &UcnLen, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 220 | FullSourceLoc Loc, DiagnosticsEngine *Diags, |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 221 | const LangOptions &Features, |
| 222 | bool in_char_string_literal = false) { |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 223 | const char *UcnBegin = ThisTokBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 225 | // Skip the '\u' char's. |
| 226 | ThisTokBuf += 2; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 227 | |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 228 | if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { |
Chris Lattner | bde1b81 | 2010-11-17 06:46:14 +0000 | [diff] [blame] | 229 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 230 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
Jordan Rose | aa89cf1 | 2013-01-24 20:50:13 +0000 | [diff] [blame] | 231 | diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1); |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 232 | return false; |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 233 | } |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 234 | UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8); |
Fariborz Jahanian | abaae2b | 2010-08-31 23:34:27 +0000 | [diff] [blame] | 235 | unsigned short UcnLenSave = UcnLen; |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 236 | for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) { |
Jordan Rose | 78ed86a | 2013-01-18 22:33:58 +0000 | [diff] [blame] | 237 | int CharVal = llvm::hexDigitValue(ThisTokBuf[0]); |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 238 | if (CharVal == -1) break; |
| 239 | UcnVal <<= 4; |
| 240 | UcnVal |= CharVal; |
| 241 | } |
| 242 | // 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] | 243 | if (UcnLenSave) { |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 244 | if (Diags) |
| 245 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 246 | diag::err_ucn_escape_incomplete); |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 247 | return false; |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 248 | } |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 249 | |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 250 | // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2] |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 251 | if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints |
| 252 | UcnVal > 0x10FFFF) { // maximum legal UTF32 value |
Chris Lattner | bde1b81 | 2010-11-17 06:46:14 +0000 | [diff] [blame] | 253 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 254 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 255 | diag::err_ucn_escape_invalid); |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 258 | |
| 259 | // C++11 allows UCNs that refer to control characters and basic source |
| 260 | // characters inside character and string literals |
| 261 | if (UcnVal < 0xa0 && |
| 262 | (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, ` |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 263 | bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal); |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 264 | if (Diags) { |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 265 | char BasicSCSChar = UcnVal; |
| 266 | if (UcnVal >= 0x20 && UcnVal < 0x7f) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 267 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 268 | IsError ? diag::err_ucn_escape_basic_scs : |
| 269 | diag::warn_cxx98_compat_literal_ucn_escape_basic_scs) |
| 270 | << StringRef(&BasicSCSChar, 1); |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 271 | else |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 272 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 273 | IsError ? diag::err_ucn_control_character : |
| 274 | diag::warn_cxx98_compat_literal_ucn_control_character); |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 275 | } |
| 276 | if (IsError) |
| 277 | return false; |
| 278 | } |
| 279 | |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 280 | if (!Features.CPlusPlus && !Features.C99 && Diags) |
| 281 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
Jordan Rose | c0cba27 | 2013-01-27 20:12:04 +0000 | [diff] [blame] | 282 | diag::warn_ucn_not_valid_in_c89_literal); |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 283 | |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 287 | /// MeasureUCNEscape - Determine the number of bytes within the resulting string |
| 288 | /// which this UCN will occupy. |
| 289 | static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 290 | const char *ThisTokEnd, unsigned CharByteWidth, |
| 291 | const LangOptions &Features, bool &HadError) { |
| 292 | // UTF-32: 4 bytes per escape. |
| 293 | if (CharByteWidth == 4) |
| 294 | return 4; |
| 295 | |
| 296 | uint32_t UcnVal = 0; |
| 297 | unsigned short UcnLen = 0; |
| 298 | FullSourceLoc Loc; |
| 299 | |
| 300 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, |
| 301 | UcnLen, Loc, 0, Features, true)) { |
| 302 | HadError = true; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | // UTF-16: 2 bytes for BMP, 4 bytes otherwise. |
| 307 | if (CharByteWidth == 2) |
| 308 | return UcnVal <= 0xFFFF ? 2 : 4; |
| 309 | |
| 310 | // UTF-8. |
| 311 | if (UcnVal < 0x80) |
| 312 | return 1; |
| 313 | if (UcnVal < 0x800) |
| 314 | return 2; |
| 315 | if (UcnVal < 0x10000) |
| 316 | return 3; |
| 317 | return 4; |
| 318 | } |
| 319 | |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 320 | /// EncodeUCNEscape - Read the Universal Character Name, check constraints and |
| 321 | /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of |
| 322 | /// StringLiteralParser. When we decide to implement UCN's for identifiers, |
| 323 | /// we will likely rework our support for UCN's. |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 324 | static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 325 | const char *ThisTokEnd, |
Chris Lattner | 2be8aa9 | 2010-11-17 07:12:42 +0000 | [diff] [blame] | 326 | char *&ResultBuf, bool &HadError, |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 327 | FullSourceLoc Loc, unsigned CharByteWidth, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 328 | DiagnosticsEngine *Diags, |
| 329 | const LangOptions &Features) { |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 330 | typedef uint32_t UTF32; |
| 331 | UTF32 UcnVal = 0; |
| 332 | unsigned short UcnLen = 0; |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 333 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, |
| 334 | Loc, Diags, Features, true)) { |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 335 | HadError = true; |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 336 | return; |
| 337 | } |
Nico Weber | a6bde81 | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 338 | |
Eli Friedman | f9edb00 | 2013-09-18 23:23:13 +0000 | [diff] [blame] | 339 | assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 340 | "only character widths of 1, 2, or 4 bytes supported"); |
Nico Weber | 9762e0a | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 342 | (void)UcnLen; |
| 343 | assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported"); |
Nico Weber | 9762e0a | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 345 | if (CharByteWidth == 4) { |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 346 | // FIXME: Make the type of the result buffer correct instead of |
| 347 | // using reinterpret_cast. |
| 348 | UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf); |
| 349 | *ResultPtr = UcnVal; |
| 350 | ResultBuf += 4; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (CharByteWidth == 2) { |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 355 | // FIXME: Make the type of the result buffer correct instead of |
| 356 | // using reinterpret_cast. |
| 357 | UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf); |
| 358 | |
Richard Smith | 0948d93 | 2012-06-13 05:41:29 +0000 | [diff] [blame] | 359 | if (UcnVal <= (UTF32)0xFFFF) { |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 360 | *ResultPtr = UcnVal; |
| 361 | ResultBuf += 2; |
Nico Weber | 9762e0a | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
Nico Weber | 9762e0a | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 364 | |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 365 | // Convert to UTF16. |
Nico Weber | 9762e0a | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 366 | UcnVal -= 0x10000; |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 367 | *ResultPtr = 0xD800 + (UcnVal >> 10); |
| 368 | *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF); |
| 369 | ResultBuf += 4; |
Fariborz Jahanian | abaae2b | 2010-08-31 23:34:27 +0000 | [diff] [blame] | 370 | return; |
| 371 | } |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 372 | |
| 373 | assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters"); |
| 374 | |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 375 | // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8. |
| 376 | // The conversion below was inspired by: |
| 377 | // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | // First, we determine how many bytes the result will require. |
Steve Naroff | c94adda | 2009-04-01 11:09:15 +0000 | [diff] [blame] | 379 | typedef uint8_t UTF8; |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 380 | |
| 381 | unsigned short bytesToWrite = 0; |
| 382 | if (UcnVal < (UTF32)0x80) |
| 383 | bytesToWrite = 1; |
| 384 | else if (UcnVal < (UTF32)0x800) |
| 385 | bytesToWrite = 2; |
| 386 | else if (UcnVal < (UTF32)0x10000) |
| 387 | bytesToWrite = 3; |
| 388 | else |
| 389 | bytesToWrite = 4; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 391 | const unsigned byteMask = 0xBF; |
| 392 | const unsigned byteMark = 0x80; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 394 | // 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] | 395 | // into the first byte, depending on how many bytes follow. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | static const UTF8 firstByteMark[5] = { |
Steve Naroff | f2a880c | 2009-03-31 10:29:45 +0000 | [diff] [blame] | 397 | 0x00, 0x00, 0xC0, 0xE0, 0xF0 |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 398 | }; |
| 399 | // Finally, we write the bytes into ResultBuf. |
| 400 | ResultBuf += bytesToWrite; |
| 401 | switch (bytesToWrite) { // note: everything falls through. |
Benjamin Kramer | f23a6e6 | 2012-11-08 19:22:26 +0000 | [diff] [blame] | 402 | case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 403 | case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 404 | case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 405 | case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]); |
Steve Naroff | 7b753d2 | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 406 | } |
| 407 | // Update the buffer. |
| 408 | ResultBuf += bytesToWrite; |
| 409 | } |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 410 | |
| 411 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 412 | /// integer-constant: [C99 6.4.4.1] |
| 413 | /// decimal-constant integer-suffix |
| 414 | /// octal-constant integer-suffix |
| 415 | /// hexadecimal-constant integer-suffix |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 416 | /// binary-literal integer-suffix [GNU, C++1y] |
Richard Smith | 8129245 | 2012-03-08 21:59:28 +0000 | [diff] [blame] | 417 | /// user-defined-integer-literal: [C++11 lex.ext] |
Richard Smith | 39570d00 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 418 | /// decimal-literal ud-suffix |
| 419 | /// octal-literal ud-suffix |
| 420 | /// hexadecimal-literal ud-suffix |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 421 | /// binary-literal ud-suffix [GNU, C++1y] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | /// decimal-constant: |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 423 | /// nonzero-digit |
| 424 | /// decimal-constant digit |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | /// octal-constant: |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 426 | /// 0 |
| 427 | /// octal-constant octal-digit |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | /// hexadecimal-constant: |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 429 | /// hexadecimal-prefix hexadecimal-digit |
| 430 | /// hexadecimal-constant hexadecimal-digit |
| 431 | /// hexadecimal-prefix: one of |
| 432 | /// 0x 0X |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 433 | /// binary-literal: |
| 434 | /// 0b binary-digit |
| 435 | /// 0B binary-digit |
| 436 | /// binary-literal binary-digit |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 437 | /// integer-suffix: |
| 438 | /// unsigned-suffix [long-suffix] |
| 439 | /// unsigned-suffix [long-long-suffix] |
| 440 | /// long-suffix [unsigned-suffix] |
| 441 | /// long-long-suffix [unsigned-sufix] |
| 442 | /// nonzero-digit: |
| 443 | /// 1 2 3 4 5 6 7 8 9 |
| 444 | /// octal-digit: |
| 445 | /// 0 1 2 3 4 5 6 7 |
| 446 | /// hexadecimal-digit: |
| 447 | /// 0 1 2 3 4 5 6 7 8 9 |
| 448 | /// a b c d e f |
| 449 | /// A B C D E F |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 450 | /// binary-digit: |
| 451 | /// 0 |
| 452 | /// 1 |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 453 | /// unsigned-suffix: one of |
| 454 | /// u U |
| 455 | /// long-suffix: one of |
| 456 | /// l L |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | /// long-long-suffix: one of |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 458 | /// ll LL |
| 459 | /// |
| 460 | /// floating-constant: [C99 6.4.4.2] |
| 461 | /// TODO: add rules... |
| 462 | /// |
Dmitri Gribenko | 7ba9172 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 463 | NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, |
| 464 | SourceLocation TokLoc, |
| 465 | Preprocessor &PP) |
| 466 | : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 59f09b6 | 2008-09-30 20:45:40 +0000 | [diff] [blame] | 468 | // This routine assumes that the range begin/end matches the regex for integer |
| 469 | // and FP constants (specifically, the 'pp-number' regex), and assumes that |
| 470 | // the byte at "*end" is both valid and not part of the regex. Because of |
| 471 | // this, it doesn't have to check for 'overscan' in various places. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 472 | assert(!isPreprocessingNumberBody(*ThisTokEnd) && "didn't maximally munch?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Dmitri Gribenko | 7ba9172 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 474 | s = DigitsBegin = ThisTokBegin; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 475 | saw_exponent = false; |
| 476 | saw_period = false; |
Richard Smith | 39570d00 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 477 | saw_ud_suffix = false; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 478 | isLong = false; |
| 479 | isUnsigned = false; |
| 480 | isLongLong = false; |
Chris Lattner | ed04542 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 481 | isFloat = false; |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 482 | isImaginary = false; |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 483 | isMicrosoftInteger = false; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 484 | hadError = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 486 | if (*s == '0') { // parse radix |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 487 | ParseNumberStartingWithZero(TokLoc); |
| 488 | if (hadError) |
| 489 | return; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 490 | } else { // the first digit is non-zero |
| 491 | radix = 10; |
| 492 | s = SkipDigits(s); |
| 493 | if (s == ThisTokEnd) { |
Chris Lattner | 328fa5c | 2007-06-08 17:12:06 +0000 | [diff] [blame] | 494 | // Done. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 495 | } else if (isHexDigit(*s) && !(*s == 'e' || *s == 'E')) { |
Dmitri Gribenko | 7ba9172 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 496 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 497 | diag::err_invalid_decimal_digit) << StringRef(s, 1); |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 498 | hadError = true; |
Chris Lattner | 328fa5c | 2007-06-08 17:12:06 +0000 | [diff] [blame] | 499 | return; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 500 | } else if (*s == '.') { |
| 501 | s++; |
| 502 | saw_period = true; |
| 503 | s = SkipDigits(s); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | } |
Chris Lattner | fb8b8f2 | 2008-09-29 23:12:31 +0000 | [diff] [blame] | 505 | if ((*s == 'e' || *s == 'E')) { // exponent |
Chris Lattner | 4885b97 | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 506 | const char *Exponent = s; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 507 | s++; |
| 508 | saw_exponent = true; |
| 509 | if (*s == '+' || *s == '-') s++; // sign |
| 510 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | 48a9b9b | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 511 | if (first_non_digit != s) { |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 512 | s = first_non_digit; |
Chris Lattner | 48a9b9b | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 513 | } else { |
Dmitri Gribenko | 7ba9172 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 514 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin), |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 515 | diag::err_exponent_has_no_digits); |
| 516 | hadError = true; |
Chris Lattner | 48a9b9b | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 517 | return; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | SuffixBegin = s; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 524 | // Parse the suffix. At this point we can classify whether we have an FP or |
| 525 | // integer constant. |
| 526 | bool isFPConstant = isFloatingLiteral(); |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 527 | const char *ImaginarySuffixLoc = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 529 | // Loop over all of the characters of the suffix. If we see something bad, |
| 530 | // we break out of the loop. |
| 531 | for (; s != ThisTokEnd; ++s) { |
| 532 | switch (*s) { |
| 533 | case 'f': // FP Suffix for "float" |
| 534 | case 'F': |
| 535 | if (!isFPConstant) break; // Error for integer constant. |
Chris Lattner | ed04542 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 536 | if (isFloat || isLong) break; // FF, LF invalid. |
| 537 | isFloat = true; |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 538 | continue; // Success. |
| 539 | case 'u': |
| 540 | case 'U': |
| 541 | if (isFPConstant) break; // Error for floating constant. |
| 542 | if (isUnsigned) break; // Cannot be repeated. |
| 543 | isUnsigned = true; |
| 544 | continue; // Success. |
| 545 | case 'l': |
| 546 | case 'L': |
| 547 | if (isLong || isLongLong) break; // Cannot be repeated. |
Chris Lattner | ed04542 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 548 | if (isFloat) break; // LF invalid. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 550 | // Check for long long. The L's need to be adjacent and the same case. |
| 551 | if (s+1 != ThisTokEnd && s[1] == s[0]) { |
| 552 | if (isFPConstant) break; // long long invalid for floats. |
| 553 | isLongLong = true; |
| 554 | ++s; // Eat both of them. |
| 555 | } else { |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 556 | isLong = true; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 557 | } |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 558 | continue; // Success. |
| 559 | case 'i': |
Chris Lattner | 26f6c22 | 2010-10-14 00:24:10 +0000 | [diff] [blame] | 560 | case 'I': |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 561 | if (PP.getLangOpts().MicrosoftExt) { |
Fariborz Jahanian | 8c6c0b6 | 2010-01-22 21:36:53 +0000 | [diff] [blame] | 562 | if (isFPConstant || isLong || isLongLong) break; |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 563 | |
Steve Naroff | a1f4145 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 564 | // Allow i8, i16, i32, i64, and i128. |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 565 | if (s + 1 != ThisTokEnd) { |
| 566 | switch (s[1]) { |
| 567 | case '8': |
| 568 | s += 2; // i8 suffix |
| 569 | isMicrosoftInteger = true; |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 570 | break; |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 571 | case '1': |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 572 | if (s + 2 == ThisTokEnd) break; |
Francois Pichet | 12df1dc | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 573 | if (s[2] == '6') { |
| 574 | s += 3; // i16 suffix |
| 575 | isMicrosoftInteger = true; |
| 576 | } |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 577 | else if (s[2] == '2') { |
| 578 | if (s + 3 == ThisTokEnd) break; |
Francois Pichet | 12df1dc | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 579 | if (s[3] == '8') { |
| 580 | s += 4; // i128 suffix |
| 581 | isMicrosoftInteger = true; |
| 582 | } |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 583 | } |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 584 | break; |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 585 | case '3': |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 586 | if (s + 2 == ThisTokEnd) break; |
Francois Pichet | 12df1dc | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 587 | if (s[2] == '2') { |
| 588 | s += 3; // i32 suffix |
| 589 | isLong = true; |
| 590 | isMicrosoftInteger = true; |
| 591 | } |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 592 | break; |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 593 | case '6': |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 594 | if (s + 2 == ThisTokEnd) break; |
Francois Pichet | 12df1dc | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 595 | if (s[2] == '4') { |
| 596 | s += 3; // i64 suffix |
| 597 | isLongLong = true; |
| 598 | isMicrosoftInteger = true; |
| 599 | } |
Nuno Lopes | baa1bc4 | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 600 | break; |
Mike Stump | c99c022 | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 601 | default: |
| 602 | break; |
| 603 | } |
| 604 | break; |
Steve Naroff | a1f4145 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 605 | } |
Steve Naroff | a1f4145 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 606 | } |
Richard Smith | 2a98862 | 2013-09-24 04:06:10 +0000 | [diff] [blame^] | 607 | // "i", "if", and "il" are user-defined suffixes in C++1y. |
| 608 | if (PP.getLangOpts().CPlusPlus1y && *s == 'i') |
| 609 | break; |
Steve Naroff | a1f4145 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 610 | // fall through. |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 611 | case 'j': |
| 612 | case 'J': |
| 613 | if (isImaginary) break; // Cannot be repeated. |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 614 | isImaginary = true; |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 615 | ImaginarySuffixLoc = s; |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 616 | continue; // Success. |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 617 | } |
Richard Smith | 39570d00 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 618 | // If we reached here, there was an error or a ud-suffix. |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 619 | break; |
| 620 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 622 | if (s != ThisTokEnd) { |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 623 | if (isValidUDSuffix(PP.getLangOpts(), |
| 624 | StringRef(SuffixBegin, ThisTokEnd - SuffixBegin))) { |
| 625 | // Any suffix pieces we might have parsed are actually part of the |
| 626 | // ud-suffix. |
| 627 | isLong = false; |
| 628 | isUnsigned = false; |
| 629 | isLongLong = false; |
| 630 | isFloat = false; |
| 631 | isImaginary = false; |
| 632 | isMicrosoftInteger = false; |
| 633 | |
Richard Smith | 39570d00 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 634 | saw_ud_suffix = true; |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | // Report an error if there are any. |
Dmitri Gribenko | 7ba9172 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 639 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin), |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 640 | isFPConstant ? diag::err_invalid_suffix_float_constant : |
| 641 | diag::err_invalid_suffix_integer_constant) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 642 | << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin); |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 643 | hadError = true; |
Chris Lattner | f55ab18 | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 644 | return; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 645 | } |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 646 | |
| 647 | if (isImaginary) { |
| 648 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, |
| 649 | ImaginarySuffixLoc - ThisTokBegin), |
| 650 | diag::ext_imaginary_constant); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved |
| 655 | /// suffixes as ud-suffixes, because the diagnostic experience is better if we |
| 656 | /// treat it as an invalid suffix. |
| 657 | bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, |
| 658 | StringRef Suffix) { |
| 659 | if (!LangOpts.CPlusPlus11 || Suffix.empty()) |
| 660 | return false; |
| 661 | |
| 662 | // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid. |
| 663 | if (Suffix[0] == '_') |
| 664 | return true; |
| 665 | |
| 666 | // In C++11, there are no library suffixes. |
| 667 | if (!LangOpts.CPlusPlus1y) |
| 668 | return false; |
| 669 | |
| 670 | // In C++1y, "s", "h", "min", "ms", "us", and "ns" are used in the library. |
Richard Smith | 2a98862 | 2013-09-24 04:06:10 +0000 | [diff] [blame^] | 671 | // Per tweaked N3660, "il", "i", and "if" are also used in the library. |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 672 | return llvm::StringSwitch<bool>(Suffix) |
| 673 | .Cases("h", "min", "s", true) |
| 674 | .Cases("ms", "us", "ns", true) |
Richard Smith | 2a98862 | 2013-09-24 04:06:10 +0000 | [diff] [blame^] | 675 | .Cases("il", "i", "if", true) |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 676 | .Default(false); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 679 | /// ParseNumberStartingWithZero - This method is called when the first character |
| 680 | /// of the number is found to be a zero. This means it is either an octal |
| 681 | /// 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] | 682 | /// a floating point number (01239.123e4). Eat the prefix, determining the |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 683 | /// radix etc. |
| 684 | void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { |
| 685 | assert(s[0] == '0' && "Invalid method call"); |
| 686 | s++; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 688 | // Handle a hex number like 0x1234. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 689 | if ((*s == 'x' || *s == 'X') && (isHexDigit(s[1]) || s[1] == '.')) { |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 690 | s++; |
| 691 | radix = 16; |
| 692 | DigitsBegin = s; |
| 693 | s = SkipHexDigits(s); |
Aaron Ballman | e1224a5 | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 694 | bool noSignificand = (s == DigitsBegin); |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 695 | if (s == ThisTokEnd) { |
| 696 | // Done. |
| 697 | } else if (*s == '.') { |
| 698 | s++; |
| 699 | saw_period = true; |
Aaron Ballman | e1224a5 | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 700 | const char *floatDigitsBegin = s; |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 701 | s = SkipHexDigits(s); |
Aaron Ballman | e1224a5 | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 702 | noSignificand &= (floatDigitsBegin == s); |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 703 | } |
Aaron Ballman | e1224a5 | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 704 | |
| 705 | if (noSignificand) { |
Dmitri Gribenko | 7ba9172 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 706 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
Aaron Ballman | e1224a5 | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 707 | diag::err_hexconstant_requires_digits); |
| 708 | hadError = true; |
| 709 | return; |
| 710 | } |
| 711 | |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 712 | // A binary exponent can appear with or with a '.'. If dotted, the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | // binary exponent is required. |
Douglas Gregor | 86325ad | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 714 | if (*s == 'p' || *s == 'P') { |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 715 | const char *Exponent = s; |
| 716 | s++; |
| 717 | saw_exponent = true; |
| 718 | if (*s == '+' || *s == '-') s++; // sign |
| 719 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | c94ad4a | 2008-07-25 18:18:34 +0000 | [diff] [blame] | 720 | if (first_non_digit == s) { |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 721 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), |
| 722 | diag::err_exponent_has_no_digits); |
| 723 | hadError = true; |
Chris Lattner | c94ad4a | 2008-07-25 18:18:34 +0000 | [diff] [blame] | 724 | return; |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 725 | } |
Chris Lattner | c94ad4a | 2008-07-25 18:18:34 +0000 | [diff] [blame] | 726 | s = first_non_digit; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 728 | if (!PP.getLangOpts().HexFloats) |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 729 | PP.Diag(TokLoc, diag::ext_hexconstant_invalid); |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 730 | } else if (saw_period) { |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 731 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
| 732 | diag::err_hexconstant_requires_exponent); |
| 733 | hadError = true; |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 734 | } |
| 735 | return; |
| 736 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 738 | // Handle simple binary numbers 0b01010 |
| 739 | if (*s == 'b' || *s == 'B') { |
Richard Smith | c5c27f2 | 2013-04-19 20:47:20 +0000 | [diff] [blame] | 740 | // 0b101010 is a C++1y / GCC extension. |
| 741 | PP.Diag(TokLoc, |
| 742 | PP.getLangOpts().CPlusPlus1y |
| 743 | ? diag::warn_cxx11_compat_binary_literal |
| 744 | : PP.getLangOpts().CPlusPlus |
| 745 | ? diag::ext_binary_literal_cxx1y |
| 746 | : diag::ext_binary_literal); |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 747 | ++s; |
| 748 | radix = 2; |
| 749 | DigitsBegin = s; |
| 750 | s = SkipBinaryDigits(s); |
| 751 | if (s == ThisTokEnd) { |
| 752 | // Done. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 753 | } else if (isHexDigit(*s)) { |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 754 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 755 | diag::err_invalid_binary_digit) << StringRef(s, 1); |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 756 | hadError = true; |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 757 | } |
Chris Lattner | d68c04f | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 758 | // Other suffixes will be diagnosed by the caller. |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 759 | return; |
| 760 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 762 | // For now, the radix is set to 8. If we discover that we have a |
| 763 | // floating point constant, the radix will change to 10. Octal floating |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | // point constants are not permitted (only decimal and hexadecimal). |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 765 | radix = 8; |
| 766 | DigitsBegin = s; |
| 767 | s = SkipOctalDigits(s); |
| 768 | if (s == ThisTokEnd) |
| 769 | return; // Done, simple octal number like 01234 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Chris Lattner | d68c04f | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 771 | // If we have some other non-octal digit that *is* a decimal digit, see if |
| 772 | // this is part of a floating point number like 094.123 or 09e1. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 773 | if (isDigit(*s)) { |
Chris Lattner | d68c04f | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 774 | const char *EndDecimal = SkipDigits(s); |
| 775 | if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') { |
| 776 | s = EndDecimal; |
| 777 | radix = 10; |
| 778 | } |
| 779 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Chris Lattner | d68c04f | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 781 | // If we have a hex digit other than 'e' (which denotes a FP exponent) then |
| 782 | // the code is using an incorrect base. |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 783 | if (isHexDigit(*s) && *s != 'e' && *s != 'E') { |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 784 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 785 | diag::err_invalid_octal_digit) << StringRef(s, 1); |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 786 | hadError = true; |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 787 | return; |
| 788 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 790 | if (*s == '.') { |
| 791 | s++; |
| 792 | radix = 10; |
| 793 | saw_period = true; |
Chris Lattner | d68c04f | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 794 | s = SkipDigits(s); // Skip suffix. |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 795 | } |
| 796 | if (*s == 'e' || *s == 'E') { // exponent |
| 797 | const char *Exponent = s; |
| 798 | s++; |
| 799 | radix = 10; |
| 800 | saw_exponent = true; |
| 801 | if (*s == '+' || *s == '-') s++; // sign |
| 802 | const char *first_non_digit = SkipDigits(s); |
| 803 | if (first_non_digit != s) { |
| 804 | s = first_non_digit; |
| 805 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), |
Chris Lattner | 59acca5 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 807 | diag::err_exponent_has_no_digits); |
| 808 | hadError = true; |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 809 | return; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Jordan Rose | de584de | 2012-09-25 22:32:51 +0000 | [diff] [blame] | 814 | static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) { |
Dmitri Gribenko | 511288b | 2012-09-25 19:09:15 +0000 | [diff] [blame] | 815 | switch (Radix) { |
| 816 | case 2: |
| 817 | return NumDigits <= 64; |
| 818 | case 8: |
| 819 | return NumDigits <= 64 / 3; // Digits are groups of 3 bits. |
| 820 | case 10: |
| 821 | return NumDigits <= 19; // floor(log10(2^64)) |
| 822 | case 16: |
| 823 | return NumDigits <= 64 / 4; // Digits are groups of 4 bits. |
| 824 | default: |
| 825 | llvm_unreachable("impossible Radix"); |
| 826 | } |
| 827 | } |
Chris Lattner | 6016a51 | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 828 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 829 | /// GetIntegerValue - Convert this numeric literal value to an APInt that |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 830 | /// matches Val's input width. If there is an overflow, set Val to the low bits |
| 831 | /// of the result and return true. Otherwise, return false. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 832 | bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { |
Daniel Dunbar | be94708 | 2008-10-16 07:32:01 +0000 | [diff] [blame] | 833 | // Fast path: Compute a conservative bound on the maximum number of |
| 834 | // bits per digit in this radix. If we can't possibly overflow a |
| 835 | // uint64 based on that bound then do the simple conversion to |
| 836 | // integer. This avoids the expensive overflow checking below, and |
| 837 | // handles the common cases that matter (small decimal integers and |
| 838 | // hex/octal values which don't overflow). |
Dmitri Gribenko | 511288b | 2012-09-25 19:09:15 +0000 | [diff] [blame] | 839 | const unsigned NumDigits = SuffixBegin - DigitsBegin; |
Jordan Rose | de584de | 2012-09-25 22:32:51 +0000 | [diff] [blame] | 840 | if (alwaysFitsInto64Bits(radix, NumDigits)) { |
Daniel Dunbar | be94708 | 2008-10-16 07:32:01 +0000 | [diff] [blame] | 841 | uint64_t N = 0; |
Dmitri Gribenko | 511288b | 2012-09-25 19:09:15 +0000 | [diff] [blame] | 842 | for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr) |
Jordan Rose | 78ed86a | 2013-01-18 22:33:58 +0000 | [diff] [blame] | 843 | N = N * radix + llvm::hexDigitValue(*Ptr); |
Daniel Dunbar | be94708 | 2008-10-16 07:32:01 +0000 | [diff] [blame] | 844 | |
| 845 | // This will truncate the value to Val's input width. Simply check |
| 846 | // for overflow by comparing. |
| 847 | Val = N; |
| 848 | return Val.getZExtValue() != N; |
| 849 | } |
| 850 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 851 | Val = 0; |
Dmitri Gribenko | 511288b | 2012-09-25 19:09:15 +0000 | [diff] [blame] | 852 | const char *Ptr = DigitsBegin; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 853 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 854 | llvm::APInt RadixVal(Val.getBitWidth(), radix); |
| 855 | llvm::APInt CharVal(Val.getBitWidth(), 0); |
| 856 | llvm::APInt OldVal = Val; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 858 | bool OverflowOccurred = false; |
Dmitri Gribenko | 511288b | 2012-09-25 19:09:15 +0000 | [diff] [blame] | 859 | while (Ptr < SuffixBegin) { |
Jordan Rose | 78ed86a | 2013-01-18 22:33:58 +0000 | [diff] [blame] | 860 | unsigned C = llvm::hexDigitValue(*Ptr++); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 862 | // If this letter is out of bound for this radix, reject it. |
Chris Lattner | 531efa4 | 2007-04-04 06:49:26 +0000 | [diff] [blame] | 863 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 865 | CharVal = C; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 867 | // Add the digit to the value in the appropriate radix. If adding in digits |
| 868 | // made the value smaller, then this overflowed. |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 869 | OldVal = Val; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 870 | |
| 871 | // Multiply by radix, did overflow occur on the multiply? |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 872 | Val *= RadixVal; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 873 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
| 874 | |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 875 | // Add value, did overflow occur on the value? |
Daniel Dunbar | b1f6442 | 2008-10-16 06:39:30 +0000 | [diff] [blame] | 876 | // (a + b) ult b <=> overflow |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 877 | Val += CharVal; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 878 | OverflowOccurred |= Val.ult(CharVal); |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 879 | } |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 880 | return OverflowOccurred; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 881 | } |
| 882 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 883 | llvm::APFloat::opStatus |
| 884 | NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) { |
Ted Kremenek | fbb08bc | 2007-11-26 23:12:30 +0000 | [diff] [blame] | 885 | using llvm::APFloat; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
Erick Tryzelaar | b907311 | 2009-08-16 23:36:28 +0000 | [diff] [blame] | 887 | unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin); |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 888 | return Result.convertFromString(StringRef(ThisTokBegin, n), |
| 889 | APFloat::rmNearestTiesToEven); |
Steve Naroff | 97b9e91 | 2007-07-09 23:53:58 +0000 | [diff] [blame] | 890 | } |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 892 | |
James Dennett | 1cc2203 | 2012-06-17 03:34:42 +0000 | [diff] [blame] | 893 | /// \verbatim |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 894 | /// user-defined-character-literal: [C++11 lex.ext] |
| 895 | /// character-literal ud-suffix |
| 896 | /// ud-suffix: |
| 897 | /// identifier |
| 898 | /// character-literal: [C++11 lex.ccon] |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 899 | /// ' c-char-sequence ' |
| 900 | /// u' c-char-sequence ' |
| 901 | /// U' c-char-sequence ' |
| 902 | /// L' c-char-sequence ' |
| 903 | /// c-char-sequence: |
| 904 | /// c-char |
| 905 | /// c-char-sequence c-char |
| 906 | /// c-char: |
| 907 | /// any member of the source character set except the single-quote ', |
| 908 | /// backslash \, or new-line character |
| 909 | /// escape-sequence |
| 910 | /// universal-character-name |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 911 | /// escape-sequence: |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 912 | /// simple-escape-sequence |
| 913 | /// octal-escape-sequence |
| 914 | /// hexadecimal-escape-sequence |
| 915 | /// simple-escape-sequence: |
NAKAMURA Takumi | 9f8a02d | 2011-08-12 05:49:51 +0000 | [diff] [blame] | 916 | /// one of \' \" \? \\ \a \b \f \n \r \t \v |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 917 | /// octal-escape-sequence: |
| 918 | /// \ octal-digit |
| 919 | /// \ octal-digit octal-digit |
| 920 | /// \ octal-digit octal-digit octal-digit |
| 921 | /// hexadecimal-escape-sequence: |
| 922 | /// \x hexadecimal-digit |
| 923 | /// hexadecimal-escape-sequence hexadecimal-digit |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 924 | /// universal-character-name: [C++11 lex.charset] |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 925 | /// \u hex-quad |
| 926 | /// \U hex-quad hex-quad |
| 927 | /// hex-quad: |
| 928 | /// hex-digit hex-digit hex-digit hex-digit |
James Dennett | 1cc2203 | 2012-06-17 03:34:42 +0000 | [diff] [blame] | 929 | /// \endverbatim |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 930 | /// |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 931 | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 932 | SourceLocation Loc, Preprocessor &PP, |
| 933 | tok::TokenKind kind) { |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 934 | // At this point we know that the character matches the regex "(L|u|U)?'.*'". |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 935 | HadError = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 937 | Kind = kind; |
| 938 | |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 939 | const char *TokBegin = begin; |
| 940 | |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 941 | // Skip over wide character determinant. |
| 942 | if (Kind != tok::char_constant) { |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 943 | ++begin; |
| 944 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 946 | // Skip over the entry quote. |
| 947 | assert(begin[0] == '\'' && "Invalid token lexed"); |
| 948 | ++begin; |
| 949 | |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 950 | // Remove an optional ud-suffix. |
| 951 | if (end[-1] != '\'') { |
| 952 | const char *UDSuffixEnd = end; |
| 953 | do { |
| 954 | --end; |
| 955 | } while (end[-1] != '\''); |
| 956 | UDSuffixBuf.assign(end, UDSuffixEnd); |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 957 | UDSuffixOffset = end - TokBegin; |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 960 | // Trim the ending quote. |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 961 | assert(end != begin && "Invalid token lexed"); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 962 | --end; |
| 963 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | // FIXME: The "Value" is an uint64_t so we can handle char literals of |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 965 | // up to 64-bits. |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 966 | // FIXME: This extensively assumes that 'char' is 8-bits. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 967 | assert(PP.getTargetInfo().getCharWidth() == 8 && |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 968 | "Assumes char is 8 bits"); |
Chris Lattner | 8577f62 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 969 | assert(PP.getTargetInfo().getIntWidth() <= 64 && |
| 970 | (PP.getTargetInfo().getIntWidth() & 7) == 0 && |
| 971 | "Assumes sizeof(int) on target is <= 64 and a multiple of char"); |
| 972 | assert(PP.getTargetInfo().getWCharWidth() <= 64 && |
| 973 | "Assumes sizeof(wchar) on target is <= 64"); |
Sanjiv Gupta | f09cb95 | 2009-04-21 02:21:29 +0000 | [diff] [blame] | 974 | |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 975 | SmallVector<uint32_t, 4> codepoint_buffer; |
| 976 | codepoint_buffer.resize(end - begin); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 977 | uint32_t *buffer_begin = &codepoint_buffer.front(); |
| 978 | uint32_t *buffer_end = buffer_begin + codepoint_buffer.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 980 | // Unicode escapes representing characters that cannot be correctly |
| 981 | // represented in a single code unit are disallowed in character literals |
| 982 | // by this implementation. |
| 983 | uint32_t largest_character_for_kind; |
| 984 | if (tok::wide_char_constant == Kind) { |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 985 | largest_character_for_kind = |
Nick Lewycky | 8054f1d | 2013-08-21 18:57:51 +0000 | [diff] [blame] | 986 | 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth()); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 987 | } else if (tok::utf16_char_constant == Kind) { |
| 988 | largest_character_for_kind = 0xFFFF; |
| 989 | } else if (tok::utf32_char_constant == Kind) { |
| 990 | largest_character_for_kind = 0x10FFFF; |
| 991 | } else { |
| 992 | largest_character_for_kind = 0x7Fu; |
Chris Lattner | 8577f62 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 995 | while (begin != end) { |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 996 | // Is this a span of non-escape characters? |
| 997 | if (begin[0] != '\\') { |
| 998 | char const *start = begin; |
| 999 | do { |
| 1000 | ++begin; |
| 1001 | } while (begin != end && *begin != '\\'); |
| 1002 | |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1003 | char const *tmp_in_start = start; |
| 1004 | uint32_t *tmp_out_start = buffer_begin; |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1005 | ConversionResult res = |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1006 | ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start), |
| 1007 | reinterpret_cast<UTF8 const *>(begin), |
| 1008 | &buffer_begin, buffer_end, strictConversion); |
| 1009 | if (res != conversionOK) { |
| 1010 | // If we see bad encoding for unprefixed character literals, warn and |
| 1011 | // simply copy the byte values, for compatibility with gcc and |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1012 | // older versions of clang. |
| 1013 | bool NoErrorOnBadEncoding = isAscii(); |
| 1014 | unsigned Msg = diag::err_bad_character_encoding; |
| 1015 | if (NoErrorOnBadEncoding) |
| 1016 | Msg = diag::warn_bad_character_encoding; |
Nick Lewycky | 8054f1d | 2013-08-21 18:57:51 +0000 | [diff] [blame] | 1017 | PP.Diag(Loc, Msg); |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1018 | if (NoErrorOnBadEncoding) { |
| 1019 | start = tmp_in_start; |
| 1020 | buffer_begin = tmp_out_start; |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1021 | for (; start != begin; ++start, ++buffer_begin) |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1022 | *buffer_begin = static_cast<uint8_t>(*start); |
| 1023 | } else { |
| 1024 | HadError = true; |
| 1025 | } |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1026 | } else { |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1027 | for (; tmp_out_start < buffer_begin; ++tmp_out_start) { |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1028 | if (*tmp_out_start > largest_character_for_kind) { |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1029 | HadError = true; |
| 1030 | PP.Diag(Loc, diag::err_character_too_large); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | continue; |
| 1036 | } |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1037 | // Is this a Universal Character Name escape? |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1038 | if (begin[1] == 'u' || begin[1] == 'U') { |
| 1039 | unsigned short UcnLen = 0; |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 1040 | if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen, |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1041 | FullSourceLoc(Loc, PP.getSourceManager()), |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1042 | &PP.getDiagnostics(), PP.getLangOpts(), true)) { |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1043 | HadError = true; |
| 1044 | } else if (*buffer_begin > largest_character_for_kind) { |
| 1045 | HadError = true; |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1046 | PP.Diag(Loc, diag::err_character_too_large); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | ++buffer_begin; |
| 1050 | continue; |
| 1051 | } |
| 1052 | unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo()); |
| 1053 | uint64_t result = |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1054 | ProcessCharEscape(TokBegin, begin, end, HadError, |
Nick Lewycky | 8054f1d | 2013-08-21 18:57:51 +0000 | [diff] [blame] | 1055 | FullSourceLoc(Loc,PP.getSourceManager()), |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1056 | CharWidth, &PP.getDiagnostics(), PP.getLangOpts()); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1057 | *buffer_begin++ = result; |
| 1058 | } |
| 1059 | |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1060 | unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front(); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | 8577f62 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 1062 | if (NumCharsSoFar > 1) { |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1063 | if (isWide()) |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1064 | PP.Diag(Loc, diag::warn_extraneous_char_constant); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1065 | else if (isAscii() && NumCharsSoFar == 4) |
| 1066 | PP.Diag(Loc, diag::ext_four_char_character_literal); |
| 1067 | else if (isAscii()) |
Chris Lattner | 8577f62 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 1068 | PP.Diag(Loc, diag::ext_multichar_character_literal); |
| 1069 | else |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1070 | PP.Diag(Loc, diag::err_multichar_utf_character_literal); |
Eli Friedman | d8cec57 | 2009-06-01 05:25:02 +0000 | [diff] [blame] | 1071 | IsMultiChar = true; |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1072 | } else { |
Daniel Dunbar | a444cc2 | 2009-07-29 01:46:05 +0000 | [diff] [blame] | 1073 | IsMultiChar = false; |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1074 | } |
Sanjiv Gupta | f09cb95 | 2009-04-21 02:21:29 +0000 | [diff] [blame] | 1075 | |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1076 | llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0); |
| 1077 | |
| 1078 | // Narrow character literals act as though their value is concatenated |
| 1079 | // in this implementation, but warn on overflow. |
| 1080 | bool multi_char_too_long = false; |
| 1081 | if (isAscii() && isMultiChar()) { |
| 1082 | LitVal = 0; |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1083 | for (size_t i = 0; i < NumCharsSoFar; ++i) { |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1084 | // check for enough leading zeros to shift into |
| 1085 | multi_char_too_long |= (LitVal.countLeadingZeros() < 8); |
| 1086 | LitVal <<= 8; |
| 1087 | LitVal = LitVal + (codepoint_buffer[i] & 0xFF); |
| 1088 | } |
| 1089 | } else if (NumCharsSoFar > 0) { |
| 1090 | // otherwise just take the last character |
| 1091 | LitVal = buffer_begin[-1]; |
| 1092 | } |
| 1093 | |
| 1094 | if (!HadError && multi_char_too_long) { |
Nick Lewycky | 63cc55b | 2013-08-21 02:40:19 +0000 | [diff] [blame] | 1095 | PP.Diag(Loc, diag::warn_char_constant_too_large); |
Seth Cantrell | 8b2b677 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Sanjiv Gupta | f09cb95 | 2009-04-21 02:21:29 +0000 | [diff] [blame] | 1098 | // Transfer the value from APInt to uint64_t |
| 1099 | Value = LitVal.getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 1101 | // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") |
| 1102 | // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple |
| 1103 | // character constants are not sign extended in the this implementation: |
| 1104 | // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1105 | if (isAscii() && NumCharsSoFar == 1 && (Value & 128) && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1106 | PP.getLangOpts().CharIsSigned) |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 1107 | Value = (signed char)Value; |
| 1108 | } |
| 1109 | |
James Dennett | 99c193b | 2012-06-19 21:04:25 +0000 | [diff] [blame] | 1110 | /// \verbatim |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1111 | /// string-literal: [C++0x lex.string] |
| 1112 | /// encoding-prefix " [s-char-sequence] " |
| 1113 | /// encoding-prefix R raw-string |
| 1114 | /// encoding-prefix: |
| 1115 | /// u8 |
| 1116 | /// u |
| 1117 | /// U |
| 1118 | /// L |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1119 | /// s-char-sequence: |
| 1120 | /// s-char |
| 1121 | /// s-char-sequence s-char |
| 1122 | /// s-char: |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1123 | /// any member of the source character set except the double-quote ", |
| 1124 | /// backslash \, or new-line character |
| 1125 | /// escape-sequence |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1126 | /// universal-character-name |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1127 | /// raw-string: |
| 1128 | /// " d-char-sequence ( r-char-sequence ) d-char-sequence " |
| 1129 | /// r-char-sequence: |
| 1130 | /// r-char |
| 1131 | /// r-char-sequence r-char |
| 1132 | /// r-char: |
| 1133 | /// any member of the source character set, except a right parenthesis ) |
| 1134 | /// followed by the initial d-char-sequence (which may be empty) |
| 1135 | /// followed by a double quote ". |
| 1136 | /// d-char-sequence: |
| 1137 | /// d-char |
| 1138 | /// d-char-sequence d-char |
| 1139 | /// d-char: |
| 1140 | /// any member of the basic source character set except: |
| 1141 | /// space, the left parenthesis (, the right parenthesis ), |
| 1142 | /// the backslash \, and the control characters representing horizontal |
| 1143 | /// tab, vertical tab, form feed, and newline. |
| 1144 | /// escape-sequence: [C++0x lex.ccon] |
| 1145 | /// simple-escape-sequence |
| 1146 | /// octal-escape-sequence |
| 1147 | /// hexadecimal-escape-sequence |
| 1148 | /// simple-escape-sequence: |
NAKAMURA Takumi | 9f8a02d | 2011-08-12 05:49:51 +0000 | [diff] [blame] | 1149 | /// one of \' \" \? \\ \a \b \f \n \r \t \v |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1150 | /// octal-escape-sequence: |
| 1151 | /// \ octal-digit |
| 1152 | /// \ octal-digit octal-digit |
| 1153 | /// \ octal-digit octal-digit octal-digit |
| 1154 | /// hexadecimal-escape-sequence: |
| 1155 | /// \x hexadecimal-digit |
| 1156 | /// hexadecimal-escape-sequence hexadecimal-digit |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1157 | /// universal-character-name: |
| 1158 | /// \u hex-quad |
| 1159 | /// \U hex-quad hex-quad |
| 1160 | /// hex-quad: |
| 1161 | /// hex-digit hex-digit hex-digit hex-digit |
James Dennett | 99c193b | 2012-06-19 21:04:25 +0000 | [diff] [blame] | 1162 | /// \endverbatim |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 1163 | /// |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1164 | StringLiteralParser:: |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1165 | StringLiteralParser(const Token *StringToks, unsigned NumStringToks, |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1166 | Preprocessor &PP, bool Complain) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1167 | : SM(PP.getSourceManager()), Features(PP.getLangOpts()), |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1168 | Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1169 | MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), |
| 1170 | ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) { |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1171 | init(StringToks, NumStringToks); |
| 1172 | } |
| 1173 | |
| 1174 | void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1175 | // The literal token may have come from an invalid source location (e.g. due |
| 1176 | // to a PCH error), in which case the token length will be 0. |
Argyrios Kyrtzidis | 9933e3a | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1177 | if (NumStringToks == 0 || StringToks[0].getLength() < 2) |
| 1178 | return DiagnoseLexingError(SourceLocation()); |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1179 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1180 | // Scan all of the string portions, remember the max individual token length, |
| 1181 | // computing a bound on the concatenated string length, and see whether any |
| 1182 | // piece is a wide-string. If any of the string portions is a wide-string |
| 1183 | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1184 | assert(NumStringToks && "expected at least one token"); |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1185 | MaxTokenLength = StringToks[0].getLength(); |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1186 | assert(StringToks[0].getLength() >= 2 && "literal token is invalid!"); |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1187 | SizeBound = StringToks[0].getLength()-2; // -2 for "". |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1188 | Kind = StringToks[0].getKind(); |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1189 | |
| 1190 | hadError = false; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Implement Translation Phase #6: concatenation of string literals |
| 1193 | /// (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] | 1194 | for (unsigned i = 1; i != NumStringToks; ++i) { |
Argyrios Kyrtzidis | 9933e3a | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1195 | if (StringToks[i].getLength() < 2) |
| 1196 | return DiagnoseLexingError(StringToks[i].getLocation()); |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1197 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1198 | // The string could be shorter than this if it needs cleaning, but this is a |
| 1199 | // reasonable bound, which is all we need. |
Argyrios Kyrtzidis | 8b7252a | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1200 | assert(StringToks[i].getLength() >= 2 && "literal token is invalid!"); |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1201 | SizeBound += StringToks[i].getLength()-2; // -2 for "". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1203 | // Remember maximum string piece length. |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1204 | if (StringToks[i].getLength() > MaxTokenLength) |
| 1205 | MaxTokenLength = StringToks[i].getLength(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1207 | // Remember if we see any wide or utf-8/16/32 strings. |
| 1208 | // Also check for illegal concatenations. |
| 1209 | if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) { |
| 1210 | if (isAscii()) { |
| 1211 | Kind = StringToks[i].getKind(); |
| 1212 | } else { |
| 1213 | if (Diags) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1214 | Diags->Report(StringToks[i].getLocation(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1215 | diag::err_unsupported_string_concat); |
| 1216 | hadError = true; |
| 1217 | } |
| 1218 | } |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1219 | } |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1220 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1221 | // Include space for the null terminator. |
| 1222 | ++SizeBound; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1224 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1226 | // Get the width in bytes of char/wchar_t/char16_t/char32_t |
| 1227 | CharByteWidth = getCharWidth(Kind, Target); |
| 1228 | assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); |
| 1229 | CharByteWidth /= 8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1231 | // The output buffer size needs to be large enough to hold wide characters. |
| 1232 | // This is a worst-case assumption which basically corresponds to L"" "long". |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1233 | SizeBound *= CharByteWidth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1235 | // Size the temporary buffer to hold the result string data. |
| 1236 | ResultBuf.resize(SizeBound); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1238 | // Likewise, but for each string piece. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1239 | SmallString<512> TokenBuf; |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1240 | TokenBuf.resize(MaxTokenLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1242 | // Loop over all the strings, getting their spelling, and expanding them to |
| 1243 | // wide strings as appropriate. |
| 1244 | ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Anders Carlsson | cbfc4b8 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 1246 | Pascal = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 1248 | SourceLocation UDSuffixTokLoc; |
| 1249 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1250 | for (unsigned i = 0, e = NumStringToks; i != e; ++i) { |
| 1251 | const char *ThisTokBuf = &TokenBuf[0]; |
| 1252 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 1253 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 1254 | // and 'spelled' tokens can only shrink. |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1255 | bool StringInvalid = false; |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1256 | unsigned ThisTokLen = |
Chris Lattner | 3972011 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 1257 | Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features, |
| 1258 | &StringInvalid); |
Argyrios Kyrtzidis | 9933e3a | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1259 | if (StringInvalid) |
| 1260 | return DiagnoseLexingError(StringToks[i].getLocation()); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1261 | |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 1262 | const char *ThisTokBegin = ThisTokBuf; |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 1263 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen; |
| 1264 | |
| 1265 | // Remove an optional ud-suffix. |
| 1266 | if (ThisTokEnd[-1] != '"') { |
| 1267 | const char *UDSuffixEnd = ThisTokEnd; |
| 1268 | do { |
| 1269 | --ThisTokEnd; |
| 1270 | } while (ThisTokEnd[-1] != '"'); |
| 1271 | |
| 1272 | StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd); |
| 1273 | |
| 1274 | if (UDSuffixBuf.empty()) { |
| 1275 | UDSuffixBuf.assign(UDSuffix); |
Richard Smith | 75b67d6 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1276 | UDSuffixToken = i; |
| 1277 | UDSuffixOffset = ThisTokEnd - ThisTokBuf; |
Richard Smith | e18f0fa | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 1278 | UDSuffixTokLoc = StringToks[i].getLocation(); |
| 1279 | } else if (!UDSuffixBuf.equals(UDSuffix)) { |
| 1280 | // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the |
| 1281 | // result of a concatenation involving at least one user-defined-string- |
| 1282 | // literal, all the participating user-defined-string-literals shall |
| 1283 | // have the same ud-suffix. |
| 1284 | if (Diags) { |
| 1285 | SourceLocation TokLoc = StringToks[i].getLocation(); |
| 1286 | Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix) |
| 1287 | << UDSuffixBuf << UDSuffix |
| 1288 | << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc) |
| 1289 | << SourceRange(TokLoc, TokLoc); |
| 1290 | } |
| 1291 | hadError = true; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | // Strip the end quote. |
| 1296 | --ThisTokEnd; |
| 1297 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1298 | // TODO: Input character set mapping support. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
Craig Topper | 61147ed | 2011-08-08 06:10:39 +0000 | [diff] [blame] | 1300 | // Skip marker for wide or unicode strings. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1301 | if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') { |
Chris Lattner | c10adde | 2007-05-20 05:00:58 +0000 | [diff] [blame] | 1302 | ++ThisTokBuf; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1303 | // Skip 8 of u8 marker for utf8 strings. |
| 1304 | if (ThisTokBuf[0] == '8') |
| 1305 | ++ThisTokBuf; |
Fariborz Jahanian | abaae2b | 2010-08-31 23:34:27 +0000 | [diff] [blame] | 1306 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1308 | // Check for raw string |
| 1309 | if (ThisTokBuf[0] == 'R') { |
| 1310 | ThisTokBuf += 2; // skip R" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1312 | const char *Prefix = ThisTokBuf; |
| 1313 | while (ThisTokBuf[0] != '(') |
Anders Carlsson | cbfc4b8 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 1314 | ++ThisTokBuf; |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1315 | ++ThisTokBuf; // skip '(' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
Richard Smith | 8129245 | 2012-03-08 21:59:28 +0000 | [diff] [blame] | 1317 | // Remove same number of characters from the end |
| 1318 | ThisTokEnd -= ThisTokBuf - Prefix; |
| 1319 | assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal"); |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1320 | |
| 1321 | // Copy the string over |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1322 | if (CopyStringFragment(StringToks[i], ThisTokBegin, |
| 1323 | StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf))) |
| 1324 | hadError = true; |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1325 | } else { |
Argyrios Kyrtzidis | 4e5b5c3 | 2012-05-03 01:01:56 +0000 | [diff] [blame] | 1326 | if (ThisTokBuf[0] != '"') { |
| 1327 | // The file may have come from PCH and then changed after loading the |
| 1328 | // PCH; Fail gracefully. |
Argyrios Kyrtzidis | 9933e3a | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1329 | return DiagnoseLexingError(StringToks[i].getLocation()); |
Argyrios Kyrtzidis | 4e5b5c3 | 2012-05-03 01:01:56 +0000 | [diff] [blame] | 1330 | } |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1331 | ++ThisTokBuf; // skip " |
| 1332 | |
| 1333 | // Check if this is a pascal string |
| 1334 | if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd && |
| 1335 | ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') { |
| 1336 | |
| 1337 | // If the \p sequence is found in the first token, we have a pascal string |
| 1338 | // Otherwise, if we already have a pascal string, ignore the first \p |
| 1339 | if (i == 0) { |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1340 | ++ThisTokBuf; |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1341 | Pascal = true; |
| 1342 | } else if (Pascal) |
| 1343 | ThisTokBuf += 2; |
| 1344 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1346 | while (ThisTokBuf != ThisTokEnd) { |
| 1347 | // Is this a span of non-escape characters? |
| 1348 | if (ThisTokBuf[0] != '\\') { |
| 1349 | const char *InStart = ThisTokBuf; |
| 1350 | do { |
| 1351 | ++ThisTokBuf; |
| 1352 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 1353 | |
| 1354 | // Copy the character span over. |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1355 | if (CopyStringFragment(StringToks[i], ThisTokBegin, |
| 1356 | StringRef(InStart, ThisTokBuf - InStart))) |
| 1357 | hadError = true; |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1358 | continue; |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1359 | } |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1360 | // Is this a Universal Character Name escape? |
| 1361 | if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') { |
Richard Smith | 2a70e65 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 1362 | EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, |
| 1363 | ResultPtr, hadError, |
| 1364 | FullSourceLoc(StringToks[i].getLocation(), SM), |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1365 | CharByteWidth, Diags, Features); |
| 1366 | continue; |
| 1367 | } |
| 1368 | // Otherwise, this is a non-UCN escape character. Process it. |
| 1369 | unsigned ResultChar = |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1370 | ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError, |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1371 | FullSourceLoc(StringToks[i].getLocation(), SM), |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1372 | CharByteWidth*8, Diags, Features); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 1374 | if (CharByteWidth == 4) { |
| 1375 | // FIXME: Make the type of the result buffer correct instead of |
| 1376 | // using reinterpret_cast. |
| 1377 | UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr); |
Nico Weber | d60b72f | 2011-11-14 05:17:37 +0000 | [diff] [blame] | 1378 | *ResultWidePtr = ResultChar; |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 1379 | ResultPtr += 4; |
| 1380 | } else if (CharByteWidth == 2) { |
| 1381 | // FIXME: Make the type of the result buffer correct instead of |
| 1382 | // using reinterpret_cast. |
| 1383 | UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr); |
Nico Weber | d60b72f | 2011-11-14 05:17:37 +0000 | [diff] [blame] | 1384 | *ResultWidePtr = ResultChar & 0xFFFF; |
Eli Friedman | d137079 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 1385 | ResultPtr += 2; |
| 1386 | } else { |
| 1387 | assert(CharByteWidth == 1 && "Unexpected char width"); |
| 1388 | *ResultPtr++ = ResultChar & 0xFF; |
| 1389 | } |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1390 | } |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1394 | if (Pascal) { |
Eli Friedman | 2055470 | 2011-11-05 00:41:04 +0000 | [diff] [blame] | 1395 | if (CharByteWidth == 4) { |
| 1396 | // FIXME: Make the type of the result buffer correct instead of |
| 1397 | // using reinterpret_cast. |
| 1398 | UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data()); |
| 1399 | ResultWidePtr[0] = GetNumStringChars() - 1; |
| 1400 | } else if (CharByteWidth == 2) { |
| 1401 | // FIXME: Make the type of the result buffer correct instead of |
| 1402 | // using reinterpret_cast. |
| 1403 | UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data()); |
| 1404 | ResultWidePtr[0] = GetNumStringChars() - 1; |
| 1405 | } else { |
| 1406 | assert(CharByteWidth == 1 && "Unexpected char width"); |
| 1407 | ResultBuf[0] = GetNumStringChars() - 1; |
| 1408 | } |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Verify that pascal strings aren't too large. |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1411 | if (GetStringLength() > 256) { |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1412 | if (Diags) |
| 1413 | Diags->Report(StringToks[0].getLocation(), |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1414 | diag::err_pascal_string_too_long) |
| 1415 | << SourceRange(StringToks[0].getLocation(), |
| 1416 | StringToks[NumStringToks-1].getLocation()); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1417 | hadError = true; |
Eli Friedman | 1c3fb22 | 2009-04-01 03:17:08 +0000 | [diff] [blame] | 1418 | return; |
| 1419 | } |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1420 | } else if (Diags) { |
Douglas Gregor | b37b46e | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1421 | // Complain if this string literal has too many characters. |
Chris Lattner | 2be8aa9 | 2010-11-17 07:12:42 +0000 | [diff] [blame] | 1422 | unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509; |
Benjamin Kramer | f23a6e6 | 2012-11-08 19:22:26 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | b37b46e | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1424 | if (GetNumStringChars() > MaxChars) |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1425 | Diags->Report(StringToks[0].getLocation(), |
Chris Lattner | 6bab435 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1426 | diag::ext_string_too_long) |
Douglas Gregor | b37b46e | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1427 | << GetNumStringChars() << MaxChars |
Chris Lattner | 2be8aa9 | 2010-11-17 07:12:42 +0000 | [diff] [blame] | 1428 | << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0) |
Douglas Gregor | b37b46e | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1429 | << SourceRange(StringToks[0].getLocation(), |
| 1430 | StringToks[NumStringToks-1].getLocation()); |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1431 | } |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1432 | } |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1433 | |
Benjamin Kramer | f23a6e6 | 2012-11-08 19:22:26 +0000 | [diff] [blame] | 1434 | static const char *resyncUTF8(const char *Err, const char *End) { |
| 1435 | if (Err == End) |
| 1436 | return End; |
| 1437 | End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err); |
| 1438 | while (++Err != End && (*Err & 0xC0) == 0x80) |
| 1439 | ; |
| 1440 | return Err; |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1443 | /// \brief This function copies from Fragment, which is a sequence of bytes |
| 1444 | /// within Tok's contents (which begin at TokBegin) into ResultPtr. |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1445 | /// Performs widening for multi-byte characters. |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1446 | bool StringLiteralParser::CopyStringFragment(const Token &Tok, |
| 1447 | const char *TokBegin, |
| 1448 | StringRef Fragment) { |
| 1449 | const UTF8 *ErrorPtrTmp; |
| 1450 | if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp)) |
| 1451 | return false; |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1452 | |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1453 | // If we see bad encoding for unprefixed string literals, warn and |
| 1454 | // simply copy the byte values, for compatibility with gcc and older |
| 1455 | // versions of clang. |
| 1456 | bool NoErrorOnBadEncoding = isAscii(); |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1457 | if (NoErrorOnBadEncoding) { |
| 1458 | memcpy(ResultPtr, Fragment.data(), Fragment.size()); |
| 1459 | ResultPtr += Fragment.size(); |
| 1460 | } |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1461 | |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1462 | if (Diags) { |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1463 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
| 1464 | |
| 1465 | FullSourceLoc SourceLoc(Tok.getLocation(), SM); |
| 1466 | const DiagnosticBuilder &Builder = |
| 1467 | Diag(Diags, Features, SourceLoc, TokBegin, |
Benjamin Kramer | f23a6e6 | 2012-11-08 19:22:26 +0000 | [diff] [blame] | 1468 | ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()), |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1469 | NoErrorOnBadEncoding ? diag::warn_bad_string_encoding |
| 1470 | : diag::err_bad_string_encoding); |
| 1471 | |
Benjamin Kramer | f23a6e6 | 2012-11-08 19:22:26 +0000 | [diff] [blame] | 1472 | const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end()); |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1473 | StringRef NextFragment(NextStart, Fragment.end()-NextStart); |
| 1474 | |
Benjamin Kramer | 7d574e2 | 2012-11-08 19:22:31 +0000 | [diff] [blame] | 1475 | // Decode into a dummy buffer. |
| 1476 | SmallString<512> Dummy; |
| 1477 | Dummy.reserve(Fragment.size() * CharByteWidth); |
| 1478 | char *Ptr = Dummy.data(); |
| 1479 | |
David Blaikie | a061317 | 2012-10-30 23:22:22 +0000 | [diff] [blame] | 1480 | while (!Builder.hasMaxRanges() && |
Benjamin Kramer | 7d574e2 | 2012-11-08 19:22:31 +0000 | [diff] [blame] | 1481 | !ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) { |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1482 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
Benjamin Kramer | f23a6e6 | 2012-11-08 19:22:26 +0000 | [diff] [blame] | 1483 | NextStart = resyncUTF8(ErrorPtr, Fragment.end()); |
Seth Cantrell | 4cfc817 | 2012-10-28 18:24:46 +0000 | [diff] [blame] | 1484 | Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin, |
| 1485 | ErrorPtr, NextStart); |
| 1486 | NextFragment = StringRef(NextStart, Fragment.end()-NextStart); |
| 1487 | } |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1488 | } |
Eli Friedman | 9436352 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1489 | return !NoErrorOnBadEncoding; |
| 1490 | } |
Craig Topper | 54edcca | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1491 | |
Argyrios Kyrtzidis | 9933e3a | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1492 | void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) { |
| 1493 | hadError = true; |
| 1494 | if (Diags) |
| 1495 | Diags->Report(Loc, diag::err_lexing_string); |
| 1496 | } |
| 1497 | |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1498 | /// getOffsetOfStringByte - This function returns the offset of the |
| 1499 | /// specified byte of the string data represented by Token. This handles |
| 1500 | /// advancing over escape sequences in the string. |
| 1501 | unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok, |
Chris Lattner | bde1b81 | 2010-11-17 06:46:14 +0000 | [diff] [blame] | 1502 | unsigned ByteNo) const { |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1503 | // Get the spelling of the token. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1504 | SmallString<32> SpellingBuffer; |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1505 | SpellingBuffer.resize(Tok.getLength()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1507 | bool StringInvalid = false; |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1508 | const char *SpellingPtr = &SpellingBuffer[0]; |
Chris Lattner | 3972011 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 1509 | unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features, |
| 1510 | &StringInvalid); |
Chris Lattner | 7a02bfd | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 1511 | if (StringInvalid) |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1512 | return 0; |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1514 | const char *SpellingStart = SpellingPtr; |
| 1515 | const char *SpellingEnd = SpellingPtr+TokLen; |
| 1516 | |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1517 | // Handle UTF-8 strings just like narrow strings. |
| 1518 | if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8') |
| 1519 | SpellingPtr += 2; |
| 1520 | |
| 1521 | assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && |
| 1522 | SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet"); |
| 1523 | |
| 1524 | // For raw string literals, this is easy. |
| 1525 | if (SpellingPtr[0] == 'R') { |
| 1526 | assert(SpellingPtr[1] == '"' && "Should be a raw string literal!"); |
| 1527 | // Skip 'R"'. |
| 1528 | SpellingPtr += 2; |
| 1529 | while (*SpellingPtr != '(') { |
| 1530 | ++SpellingPtr; |
| 1531 | assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal"); |
| 1532 | } |
| 1533 | // Skip '('. |
| 1534 | ++SpellingPtr; |
| 1535 | return SpellingPtr - SpellingStart + ByteNo; |
| 1536 | } |
| 1537 | |
| 1538 | // Skip over the leading quote |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1539 | assert(SpellingPtr[0] == '"' && "Should be a string literal!"); |
| 1540 | ++SpellingPtr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1542 | // Skip over bytes until we find the offset we're looking for. |
| 1543 | while (ByteNo) { |
| 1544 | assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1546 | // Step over non-escapes simply. |
| 1547 | if (*SpellingPtr != '\\') { |
| 1548 | ++SpellingPtr; |
| 1549 | --ByteNo; |
| 1550 | continue; |
| 1551 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1553 | // Otherwise, this is an escape character. Advance over it. |
| 1554 | bool HadError = false; |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1555 | if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') { |
| 1556 | const char *EscapePtr = SpellingPtr; |
| 1557 | unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd, |
| 1558 | 1, Features, HadError); |
| 1559 | if (Len > ByteNo) { |
| 1560 | // ByteNo is somewhere within the escape sequence. |
| 1561 | SpellingPtr = EscapePtr; |
| 1562 | break; |
| 1563 | } |
| 1564 | ByteNo -= Len; |
| 1565 | } else { |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1566 | ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError, |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1567 | FullSourceLoc(Tok.getLocation(), SM), |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1568 | CharByteWidth*8, Diags, Features); |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1569 | --ByteNo; |
| 1570 | } |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1571 | assert(!HadError && "This method isn't valid on erroneous strings"); |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1572 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | ddb7191 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1574 | return SpellingPtr-SpellingStart; |
| 1575 | } |