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