| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "clang/Lex/LiteralSupport.h" |
| 15 | #include "clang/Basic/CharInfo.h" |
| 16 | #include "clang/Basic/LangOptions.h" |
| 17 | #include "clang/Basic/SourceLocation.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/Lex/LexDiagnostic.h" |
| 20 | #include "clang/Lex/Lexer.h" |
| 21 | #include "clang/Lex/Preprocessor.h" |
| 22 | #include "clang/Lex/Token.h" |
| 23 | #include "llvm/ADT/APInt.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | #include "llvm/ADT/StringExtras.h" |
| 26 | #include "llvm/ADT/StringSwitch.h" |
| 27 | #include "llvm/Support/ConvertUTF.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <cstddef> |
| 32 | #include <cstdint> |
| 33 | #include <cstring> |
| 34 | #include <string> |
| 35 | |
| 36 | using namespace clang; |
| 37 | |
| 38 | static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) { |
| 39 | switch (kind) { |
| 40 | default: llvm_unreachable("Unknown token type!"); |
| 41 | case tok::char_constant: |
| 42 | case tok::string_literal: |
| 43 | case tok::utf8_char_constant: |
| 44 | case tok::utf8_string_literal: |
| 45 | return Target.getCharWidth(); |
| 46 | case tok::wide_char_constant: |
| 47 | case tok::wide_string_literal: |
| 48 | return Target.getWCharWidth(); |
| 49 | case tok::utf16_char_constant: |
| 50 | case tok::utf16_string_literal: |
| 51 | return Target.getChar16Width(); |
| 52 | case tok::utf32_char_constant: |
| 53 | case tok::utf32_string_literal: |
| 54 | return Target.getChar32Width(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static CharSourceRange MakeCharSourceRange(const LangOptions &Features, |
| 59 | FullSourceLoc TokLoc, |
| 60 | const char *TokBegin, |
| 61 | const char *TokRangeBegin, |
| 62 | const char *TokRangeEnd) { |
| 63 | SourceLocation Begin = |
| 64 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
| 65 | TokLoc.getManager(), Features); |
| 66 | SourceLocation End = |
| 67 | Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin, |
| 68 | TokLoc.getManager(), Features); |
| 69 | return CharSourceRange::getCharRange(Begin, End); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, |
| 78 | const LangOptions &Features, FullSourceLoc TokLoc, |
| 79 | const char *TokBegin, const char *TokRangeBegin, |
| 80 | const char *TokRangeEnd, unsigned DiagID) { |
| 81 | SourceLocation Begin = |
| 82 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
| 83 | TokLoc.getManager(), Features); |
| 84 | return Diags->Report(Begin, DiagID) << |
| 85 | MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | static unsigned ProcessCharEscape(const char *ThisTokBegin, |
| 91 | const char *&ThisTokBuf, |
| 92 | const char *ThisTokEnd, bool &HadError, |
| 93 | FullSourceLoc Loc, unsigned CharWidth, |
| 94 | DiagnosticsEngine *Diags, |
| 95 | const LangOptions &Features) { |
| 96 | const char *EscapeBegin = ThisTokBuf; |
| 97 | |
| 98 | |
| 99 | ++ThisTokBuf; |
| 100 | |
| 101 | |
| 102 | |
| 103 | unsigned ResultChar = *ThisTokBuf++; |
| 104 | switch (ResultChar) { |
| 105 | |
| 106 | case '\\': case '\'': case '"': case '?': break; |
| 107 | |
| 108 | |
| 109 | case 'a': |
| 110 | |
| 111 | ResultChar = 7; |
| 112 | break; |
| 113 | case 'b': |
| 114 | ResultChar = 8; |
| 115 | break; |
| 116 | case 'e': |
| 117 | if (Diags) |
| 118 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 119 | diag::ext_nonstandard_escape) << "e"; |
| 120 | ResultChar = 27; |
| 121 | break; |
| 122 | case 'E': |
| 123 | if (Diags) |
| 124 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 125 | diag::ext_nonstandard_escape) << "E"; |
| 126 | ResultChar = 27; |
| 127 | break; |
| 128 | case 'f': |
| 129 | ResultChar = 12; |
| 130 | break; |
| 131 | case 'n': |
| 132 | ResultChar = 10; |
| 133 | break; |
| 134 | case 'r': |
| 135 | ResultChar = 13; |
| 136 | break; |
| 137 | case 't': |
| 138 | ResultChar = 9; |
| 139 | break; |
| 140 | case 'v': |
| 141 | ResultChar = 11; |
| 142 | break; |
| 143 | case 'x': { |
| 144 | ResultChar = 0; |
| 145 | if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { |
| 146 | if (Diags) |
| 147 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 148 | diag::err_hex_escape_no_digits) << "x"; |
| 149 | HadError = true; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | bool Overflow = false; |
| 155 | for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { |
| 156 | int CharVal = llvm::hexDigitValue(ThisTokBuf[0]); |
| 157 | if (CharVal == -1) break; |
| 158 | |
| 159 | if (ResultChar & 0xF0000000) |
| 160 | Overflow = true; |
| 161 | ResultChar <<= 4; |
| 162 | ResultChar |= CharVal; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 167 | Overflow = true; |
| 168 | ResultChar &= ~0U >> (32-CharWidth); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | if (Overflow && Diags) |
| 173 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 174 | diag::err_escape_too_large) << 0; |
| 175 | break; |
| 176 | } |
| 177 | case '0': case '1': case '2': case '3': |
| 178 | case '4': case '5': case '6': case '7': { |
| 179 | |
| 180 | --ThisTokBuf; |
| 181 | ResultChar = 0; |
| 182 | |
| 183 | |
| 184 | |
| 185 | unsigned NumDigits = 0; |
| 186 | do { |
| 187 | ResultChar <<= 3; |
| 188 | ResultChar |= *ThisTokBuf++ - '0'; |
| 189 | ++NumDigits; |
| 190 | } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && |
| 191 | ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); |
| 192 | |
| 193 | |
| 194 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 195 | if (Diags) |
| 196 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 197 | diag::err_escape_too_large) << 1; |
| 198 | ResultChar &= ~0U >> (32-CharWidth); |
| 199 | } |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | case '(': case '{': case '[': case '%': |
| 205 | |
| 206 | if (Diags) |
| 207 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 208 | diag::ext_nonstandard_escape) |
| 209 | << std::string(1, ResultChar); |
| 210 | break; |
| 211 | default: |
| 212 | if (!Diags) |
| 213 | break; |
| 214 | |
| 215 | if (isPrintable(ResultChar)) |
| 216 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 217 | diag::ext_unknown_escape) |
| 218 | << std::string(1, ResultChar); |
| 219 | else |
| 220 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 221 | diag::ext_unknown_escape) |
| 222 | << "x" + llvm::utohexstr(ResultChar); |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | return ResultChar; |
| 227 | } |
| 228 | |
| 229 | static void appendCodePoint(unsigned Codepoint, |
| 230 | llvm::SmallVectorImpl<char> &Str) { |
| 231 | char ResultBuf[4]; |
| 232 | char *ResultPtr = ResultBuf; |
| 233 | bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr); |
| 234 | (void)Res; |
| 235 | (0) . __assert_fail ("Res && \"Unexpected conversion failure\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 235, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Res && "Unexpected conversion failure"); |
| 236 | Str.append(ResultBuf, ResultPtr); |
| 237 | } |
| 238 | |
| 239 | void clang::expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) { |
| 240 | for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) { |
| 241 | if (*I != '\\') { |
| 242 | Buf.push_back(*I); |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | ++I; |
| 247 | assert(*I == 'u' || *I == 'U'); |
| 248 | |
| 249 | unsigned NumHexDigits; |
| 250 | if (*I == 'u') |
| 251 | NumHexDigits = 4; |
| 252 | else |
| 253 | NumHexDigits = 8; |
| 254 | |
| 255 | assert(I + NumHexDigits <= E); |
| 256 | |
| 257 | uint32_t CodePoint = 0; |
| 258 | for (++I; NumHexDigits != 0; ++I, --NumHexDigits) { |
| 259 | unsigned Value = llvm::hexDigitValue(*I); |
| 260 | assert(Value != -1U); |
| 261 | |
| 262 | CodePoint <<= 4; |
| 263 | CodePoint += Value; |
| 264 | } |
| 265 | |
| 266 | appendCodePoint(CodePoint, Buf); |
| 267 | --I; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 274 | const char *ThisTokEnd, |
| 275 | uint32_t &UcnVal, unsigned short &UcnLen, |
| 276 | FullSourceLoc Loc, DiagnosticsEngine *Diags, |
| 277 | const LangOptions &Features, |
| 278 | bool in_char_string_literal = false) { |
| 279 | const char *UcnBegin = ThisTokBuf; |
| 280 | |
| 281 | |
| 282 | ThisTokBuf += 2; |
| 283 | |
| 284 | if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { |
| 285 | if (Diags) |
| 286 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 287 | diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1); |
| 288 | return false; |
| 289 | } |
| 290 | UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8); |
| 291 | unsigned short UcnLenSave = UcnLen; |
| 292 | for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) { |
| 293 | int CharVal = llvm::hexDigitValue(ThisTokBuf[0]); |
| 294 | if (CharVal == -1) break; |
| 295 | UcnVal <<= 4; |
| 296 | UcnVal |= CharVal; |
| 297 | } |
| 298 | |
| 299 | if (UcnLenSave) { |
| 300 | if (Diags) |
| 301 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 302 | diag::err_ucn_escape_incomplete); |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || |
| 308 | UcnVal > 0x10FFFF) { |
| 309 | if (Diags) |
| 310 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 311 | diag::err_ucn_escape_invalid); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | |
| 317 | if (UcnVal < 0xa0 && |
| 318 | (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { |
| 319 | bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal); |
| 320 | if (Diags) { |
| 321 | char BasicSCSChar = UcnVal; |
| 322 | if (UcnVal >= 0x20 && UcnVal < 0x7f) |
| 323 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 324 | IsError ? diag::err_ucn_escape_basic_scs : |
| 325 | diag::warn_cxx98_compat_literal_ucn_escape_basic_scs) |
| 326 | << StringRef(&BasicSCSChar, 1); |
| 327 | else |
| 328 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 329 | IsError ? diag::err_ucn_control_character : |
| 330 | diag::warn_cxx98_compat_literal_ucn_control_character); |
| 331 | } |
| 332 | if (IsError) |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | if (!Features.CPlusPlus && !Features.C99 && Diags) |
| 337 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 338 | diag::warn_ucn_not_valid_in_c89_literal); |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 346 | const char *ThisTokEnd, unsigned CharByteWidth, |
| 347 | const LangOptions &Features, bool &HadError) { |
| 348 | |
| 349 | if (CharByteWidth == 4) |
| 350 | return 4; |
| 351 | |
| 352 | uint32_t UcnVal = 0; |
| 353 | unsigned short UcnLen = 0; |
| 354 | FullSourceLoc Loc; |
| 355 | |
| 356 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, |
| 357 | UcnLen, Loc, nullptr, Features, true)) { |
| 358 | HadError = true; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | if (CharByteWidth == 2) |
| 364 | return UcnVal <= 0xFFFF ? 2 : 4; |
| 365 | |
| 366 | |
| 367 | if (UcnVal < 0x80) |
| 368 | return 1; |
| 369 | if (UcnVal < 0x800) |
| 370 | return 2; |
| 371 | if (UcnVal < 0x10000) |
| 372 | return 3; |
| 373 | return 4; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 381 | const char *ThisTokEnd, |
| 382 | char *&ResultBuf, bool &HadError, |
| 383 | FullSourceLoc Loc, unsigned CharByteWidth, |
| 384 | DiagnosticsEngine *Diags, |
| 385 | const LangOptions &Features) { |
| 386 | typedef uint32_t UTF32; |
| 387 | UTF32 UcnVal = 0; |
| 388 | unsigned short UcnLen = 0; |
| 389 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, |
| 390 | Loc, Diags, Features, true)) { |
| 391 | HadError = true; |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | (0) . __assert_fail ("(CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && \"only character widths of 1, 2, or 4 bytes supported\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && |
| 396 | (0) . __assert_fail ("(CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && \"only character widths of 1, 2, or 4 bytes supported\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "only character widths of 1, 2, or 4 bytes supported"); |
| 397 | |
| 398 | (void)UcnLen; |
| 399 | (0) . __assert_fail ("(UcnLen== 4 || UcnLen== 8) && \"only ucn length of 4 or 8 supported\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 399, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported"); |
| 400 | |
| 401 | if (CharByteWidth == 4) { |
| 402 | |
| 403 | |
| 404 | llvm::UTF32 *ResultPtr = reinterpret_cast<llvm::UTF32*>(ResultBuf); |
| 405 | *ResultPtr = UcnVal; |
| 406 | ResultBuf += 4; |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | if (CharByteWidth == 2) { |
| 411 | |
| 412 | |
| 413 | llvm::UTF16 *ResultPtr = reinterpret_cast<llvm::UTF16*>(ResultBuf); |
| 414 | |
| 415 | if (UcnVal <= (UTF32)0xFFFF) { |
| 416 | *ResultPtr = UcnVal; |
| 417 | ResultBuf += 2; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | UcnVal -= 0x10000; |
| 423 | *ResultPtr = 0xD800 + (UcnVal >> 10); |
| 424 | *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF); |
| 425 | ResultBuf += 4; |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | (0) . __assert_fail ("CharByteWidth == 1 && \"UTF-8 encoding is only for 1 byte characters\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 429, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters"); |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | typedef uint8_t UTF8; |
| 436 | |
| 437 | unsigned short bytesToWrite = 0; |
| 438 | if (UcnVal < (UTF32)0x80) |
| 439 | bytesToWrite = 1; |
| 440 | else if (UcnVal < (UTF32)0x800) |
| 441 | bytesToWrite = 2; |
| 442 | else if (UcnVal < (UTF32)0x10000) |
| 443 | bytesToWrite = 3; |
| 444 | else |
| 445 | bytesToWrite = 4; |
| 446 | |
| 447 | const unsigned byteMask = 0xBF; |
| 448 | const unsigned byteMark = 0x80; |
| 449 | |
| 450 | |
| 451 | |
| 452 | static const UTF8 firstByteMark[5] = { |
| 453 | 0x00, 0x00, 0xC0, 0xE0, 0xF0 |
| 454 | }; |
| 455 | |
| 456 | ResultBuf += bytesToWrite; |
| 457 | switch (bytesToWrite) { |
| 458 | case 4: |
| 459 | *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 460 | LLVM_FALLTHROUGH; |
| 461 | case 3: |
| 462 | *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 463 | LLVM_FALLTHROUGH; |
| 464 | case 2: |
| 465 | *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 466 | LLVM_FALLTHROUGH; |
| 467 | case 1: |
| 468 | *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]); |
| 469 | } |
| 470 | |
| 471 | ResultBuf += bytesToWrite; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, |
| 526 | SourceLocation TokLoc, |
| 527 | Preprocessor &PP) |
| 528 | : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) { |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | (0) . __assert_fail ("!isPreprocessingNumberBody(*ThisTokEnd) && \"didn't maximally munch?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 534, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isPreprocessingNumberBody(*ThisTokEnd) && "didn't maximally munch?"); |
| 535 | |
| 536 | s = DigitsBegin = ThisTokBegin; |
| 537 | saw_exponent = false; |
| 538 | saw_period = false; |
| 539 | saw_ud_suffix = false; |
| 540 | saw_fixed_point_suffix = false; |
| 541 | isLong = false; |
| 542 | isUnsigned = false; |
| 543 | isLongLong = false; |
| 544 | isHalf = false; |
| 545 | isFloat = false; |
| 546 | isImaginary = false; |
| 547 | isFloat16 = false; |
| 548 | isFloat128 = false; |
| 549 | MicrosoftInteger = 0; |
| 550 | isFract = false; |
| 551 | isAccum = false; |
| 552 | hadError = false; |
| 553 | |
| 554 | if (*s == '0') { |
| 555 | ParseNumberStartingWithZero(TokLoc); |
| 556 | if (hadError) |
| 557 | return; |
| 558 | } else { |
| 559 | radix = 10; |
| 560 | s = SkipDigits(s); |
| 561 | if (s == ThisTokEnd) { |
| 562 | |
| 563 | } else { |
| 564 | ParseDecimalOrOctalCommon(TokLoc); |
| 565 | if (hadError) |
| 566 | return; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | SuffixBegin = s; |
| 571 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
| 572 | |
| 573 | |
| 574 | if (PP.getLangOpts().FixedPoint) { |
| 575 | for (const char *c = s; c != ThisTokEnd; ++c) { |
| 576 | if (*c == 'r' || *c == 'k' || *c == 'R' || *c == 'K') { |
| 577 | saw_fixed_point_suffix = true; |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | |
| 585 | bool isFPConstant = isFloatingLiteral(); |
| 586 | |
| 587 | |
| 588 | |
| 589 | for (; s != ThisTokEnd; ++s) { |
| 590 | switch (*s) { |
| 591 | case 'R': |
| 592 | case 'r': |
| 593 | if (!PP.getLangOpts().FixedPoint) break; |
| 594 | if (isFract || isAccum) break; |
| 595 | if (!(saw_period || saw_exponent)) break; |
| 596 | isFract = true; |
| 597 | continue; |
| 598 | case 'K': |
| 599 | case 'k': |
| 600 | if (!PP.getLangOpts().FixedPoint) break; |
| 601 | if (isFract || isAccum) break; |
| 602 | if (!(saw_period || saw_exponent)) break; |
| 603 | isAccum = true; |
| 604 | continue; |
| 605 | case 'h': |
| 606 | case 'H': |
| 607 | |
| 608 | if (!(PP.getLangOpts().Half || PP.getLangOpts().FixedPoint)) break; |
| 609 | if (isIntegerLiteral()) break; |
| 610 | if (isHalf || isFloat || isLong) break; |
| 611 | isHalf = true; |
| 612 | continue; |
| 613 | case 'f': |
| 614 | case 'F': |
| 615 | if (!isFPConstant) break; |
| 616 | if (isHalf || isFloat || isLong || isFloat128) |
| 617 | break; |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | if ((PP.getTargetInfo().hasFloat16Type() || PP.getLangOpts().CUDA) && |
| 623 | s + 2 < ThisTokEnd && s[1] == '1' && s[2] == '6') { |
| 624 | s += 2; |
| 625 | isFloat16 = true; |
| 626 | continue; |
| 627 | } |
| 628 | |
| 629 | isFloat = true; |
| 630 | continue; |
| 631 | case 'q': |
| 632 | case 'Q': |
| 633 | if (!isFPConstant) break; |
| 634 | if (isHalf || isFloat || isLong || isFloat128) |
| 635 | break; |
| 636 | isFloat128 = true; |
| 637 | continue; |
| 638 | case 'u': |
| 639 | case 'U': |
| 640 | if (isFPConstant) break; |
| 641 | if (isUnsigned) break; |
| 642 | isUnsigned = true; |
| 643 | continue; |
| 644 | case 'l': |
| 645 | case 'L': |
| 646 | if (isLong || isLongLong) break; |
| 647 | if (isHalf || isFloat || isFloat128) break; |
| 648 | |
| 649 | |
| 650 | if (s[1] == s[0]) { |
| 651 | (0) . __assert_fail ("s + 1 < ThisTokEnd && \"didn't maximally munch?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 651, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(s + 1 < ThisTokEnd && "didn't maximally munch?"); |
| 652 | if (isFPConstant) break; |
| 653 | isLongLong = true; |
| 654 | ++s; |
| 655 | } else { |
| 656 | isLong = true; |
| 657 | } |
| 658 | continue; |
| 659 | case 'i': |
| 660 | case 'I': |
| 661 | if (PP.getLangOpts().MicrosoftExt) { |
| 662 | if (isLong || isLongLong || MicrosoftInteger) |
| 663 | break; |
| 664 | |
| 665 | if (!isFPConstant) { |
| 666 | |
| 667 | switch (s[1]) { |
| 668 | case '8': |
| 669 | s += 2; |
| 670 | MicrosoftInteger = 8; |
| 671 | break; |
| 672 | case '1': |
| 673 | if (s[2] == '6') { |
| 674 | s += 3; |
| 675 | MicrosoftInteger = 16; |
| 676 | } |
| 677 | break; |
| 678 | case '3': |
| 679 | if (s[2] == '2') { |
| 680 | s += 3; |
| 681 | MicrosoftInteger = 32; |
| 682 | } |
| 683 | break; |
| 684 | case '6': |
| 685 | if (s[2] == '4') { |
| 686 | s += 3; |
| 687 | MicrosoftInteger = 64; |
| 688 | } |
| 689 | break; |
| 690 | default: |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | if (MicrosoftInteger) { |
| 695 | (0) . __assert_fail ("s <= ThisTokEnd && \"didn't maximally munch?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 695, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(s <= ThisTokEnd && "didn't maximally munch?"); |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | LLVM_FALLTHROUGH; |
| 700 | case 'j': |
| 701 | case 'J': |
| 702 | if (isImaginary) break; |
| 703 | isImaginary = true; |
| 704 | continue; |
| 705 | } |
| 706 | |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | |
| 711 | if (s != ThisTokEnd || isImaginary) { |
| 712 | |
| 713 | expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin)); |
| 714 | if (isValidUDSuffix(PP.getLangOpts(), UDSuffixBuf)) { |
| 715 | if (!isImaginary) { |
| 716 | |
| 717 | |
| 718 | isLong = false; |
| 719 | isUnsigned = false; |
| 720 | isLongLong = false; |
| 721 | isFloat = false; |
| 722 | isFloat16 = false; |
| 723 | isHalf = false; |
| 724 | isImaginary = false; |
| 725 | MicrosoftInteger = 0; |
| 726 | saw_fixed_point_suffix = false; |
| 727 | isFract = false; |
| 728 | isAccum = false; |
| 729 | } |
| 730 | |
| 731 | saw_ud_suffix = true; |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | if (s != ThisTokEnd) { |
| 736 | |
| 737 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin), |
| 738 | diag::err_invalid_suffix_constant) |
| 739 | << StringRef(SuffixBegin, ThisTokEnd - SuffixBegin) << isFPConstant; |
| 740 | hadError = true; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | if (!hadError && saw_fixed_point_suffix) { |
| 745 | assert(isFract || isAccum); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc){ |
| 753 | (0) . __assert_fail ("(radix == 8 || radix == 10) && \"Unexpected radix\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 753, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((radix == 8 || radix == 10) && "Unexpected radix"); |
| 754 | |
| 755 | |
| 756 | |
| 757 | if (isHexDigit(*s) && *s != 'e' && *s != 'E' && |
| 758 | !isValidUDSuffix(PP.getLangOpts(), StringRef(s, ThisTokEnd - s))) { |
| 759 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
| 760 | diag::err_invalid_digit) << StringRef(s, 1) << (radix == 8 ? 1 : 0); |
| 761 | hadError = true; |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | if (*s == '.') { |
| 766 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
| 767 | s++; |
| 768 | radix = 10; |
| 769 | saw_period = true; |
| 770 | checkSeparator(TokLoc, s, CSK_BeforeDigits); |
| 771 | s = SkipDigits(s); |
| 772 | } |
| 773 | if (*s == 'e' || *s == 'E') { |
| 774 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
| 775 | const char *Exponent = s; |
| 776 | s++; |
| 777 | radix = 10; |
| 778 | saw_exponent = true; |
| 779 | if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; |
| 780 | const char *first_non_digit = SkipDigits(s); |
| 781 | if (containsDigits(s, first_non_digit)) { |
| 782 | checkSeparator(TokLoc, s, CSK_BeforeDigits); |
| 783 | s = first_non_digit; |
| 784 | } else { |
| 785 | if (!hadError) { |
| 786 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), |
| 787 | diag::err_exponent_has_no_digits); |
| 788 | hadError = true; |
| 789 | } |
| 790 | return; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, |
| 799 | StringRef Suffix) { |
| 800 | if (!LangOpts.CPlusPlus11 || Suffix.empty()) |
| 801 | return false; |
| 802 | |
| 803 | |
| 804 | if (Suffix[0] == '_') |
| 805 | return true; |
| 806 | |
| 807 | |
| 808 | if (!LangOpts.CPlusPlus14) |
| 809 | return false; |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | return llvm::StringSwitch<bool>(Suffix) |
| 815 | .Cases("h", "min", "s", true) |
| 816 | .Cases("ms", "us", "ns", true) |
| 817 | .Cases("il", "i", "if", true) |
| 818 | .Cases("d", "y", LangOpts.CPlusPlus2a) |
| 819 | .Default(false); |
| 820 | } |
| 821 | |
| 822 | void NumericLiteralParser::checkSeparator(SourceLocation TokLoc, |
| 823 | const char *Pos, |
| 824 | CheckSeparatorKind IsAfterDigits) { |
| 825 | if (IsAfterDigits == CSK_AfterDigits) { |
| 826 | if (Pos == ThisTokBegin) |
| 827 | return; |
| 828 | --Pos; |
| 829 | } else if (Pos == ThisTokEnd) |
| 830 | return; |
| 831 | |
| 832 | if (isDigitSeparator(*Pos)) { |
| 833 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Pos - ThisTokBegin), |
| 834 | diag::err_digit_separator_not_between_digits) |
| 835 | << IsAfterDigits; |
| 836 | hadError = true; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { |
| 846 | (0) . __assert_fail ("s[0] == '0' && \"Invalid method call\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 846, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(s[0] == '0' && "Invalid method call"); |
| 847 | s++; |
| 848 | |
| 849 | int c1 = s[0]; |
| 850 | |
| 851 | |
| 852 | if ((c1 == 'x' || c1 == 'X') && (isHexDigit(s[1]) || s[1] == '.')) { |
| 853 | s++; |
| 854 | (0) . __assert_fail ("s < ThisTokEnd && \"didn't maximally munch?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 854, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(s < ThisTokEnd && "didn't maximally munch?"); |
| 855 | radix = 16; |
| 856 | DigitsBegin = s; |
| 857 | s = SkipHexDigits(s); |
| 858 | bool HasSignificandDigits = containsDigits(DigitsBegin, s); |
| 859 | if (s == ThisTokEnd) { |
| 860 | |
| 861 | } else if (*s == '.') { |
| 862 | s++; |
| 863 | saw_period = true; |
| 864 | const char *floatDigitsBegin = s; |
| 865 | s = SkipHexDigits(s); |
| 866 | if (containsDigits(floatDigitsBegin, s)) |
| 867 | HasSignificandDigits = true; |
| 868 | if (HasSignificandDigits) |
| 869 | checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits); |
| 870 | } |
| 871 | |
| 872 | if (!HasSignificandDigits) { |
| 873 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
| 874 | diag::err_hex_constant_requires) |
| 875 | << PP.getLangOpts().CPlusPlus << 1; |
| 876 | hadError = true; |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | |
| 882 | if (*s == 'p' || *s == 'P') { |
| 883 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
| 884 | const char *Exponent = s; |
| 885 | s++; |
| 886 | saw_exponent = true; |
| 887 | if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; |
| 888 | const char *first_non_digit = SkipDigits(s); |
| 889 | if (!containsDigits(s, first_non_digit)) { |
| 890 | if (!hadError) { |
| 891 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), |
| 892 | diag::err_exponent_has_no_digits); |
| 893 | hadError = true; |
| 894 | } |
| 895 | return; |
| 896 | } |
| 897 | checkSeparator(TokLoc, s, CSK_BeforeDigits); |
| 898 | s = first_non_digit; |
| 899 | |
| 900 | if (!PP.getLangOpts().HexFloats) |
| 901 | PP.Diag(TokLoc, PP.getLangOpts().CPlusPlus |
| 902 | ? diag::ext_hex_literal_invalid |
| 903 | : diag::ext_hex_constant_invalid); |
| 904 | else if (PP.getLangOpts().CPlusPlus17) |
| 905 | PP.Diag(TokLoc, diag::warn_cxx17_hex_literal); |
| 906 | } else if (saw_period) { |
| 907 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
| 908 | diag::err_hex_constant_requires) |
| 909 | << PP.getLangOpts().CPlusPlus << 0; |
| 910 | hadError = true; |
| 911 | } |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | |
| 916 | if ((c1 == 'b' || c1 == 'B') && (s[1] == '0' || s[1] == '1')) { |
| 917 | |
| 918 | PP.Diag(TokLoc, |
| 919 | PP.getLangOpts().CPlusPlus14 |
| 920 | ? diag::warn_cxx11_compat_binary_literal |
| 921 | : PP.getLangOpts().CPlusPlus |
| 922 | ? diag::ext_binary_literal_cxx14 |
| 923 | : diag::ext_binary_literal); |
| 924 | ++s; |
| 925 | (0) . __assert_fail ("s < ThisTokEnd && \"didn't maximally munch?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 925, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(s < ThisTokEnd && "didn't maximally munch?"); |
| 926 | radix = 2; |
| 927 | DigitsBegin = s; |
| 928 | s = SkipBinaryDigits(s); |
| 929 | if (s == ThisTokEnd) { |
| 930 | |
| 931 | } else if (isHexDigit(*s) && |
| 932 | !isValidUDSuffix(PP.getLangOpts(), |
| 933 | StringRef(s, ThisTokEnd - s))) { |
| 934 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
| 935 | diag::err_invalid_digit) << StringRef(s, 1) << 2; |
| 936 | hadError = true; |
| 937 | } |
| 938 | |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | radix = 8; |
| 946 | DigitsBegin = s; |
| 947 | s = SkipOctalDigits(s); |
| 948 | if (s == ThisTokEnd) |
| 949 | return; |
| 950 | |
| 951 | |
| 952 | |
| 953 | if (isDigit(*s)) { |
| 954 | const char *EndDecimal = SkipDigits(s); |
| 955 | if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') { |
| 956 | s = EndDecimal; |
| 957 | radix = 10; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | ParseDecimalOrOctalCommon(TokLoc); |
| 962 | } |
| 963 | |
| 964 | static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) { |
| 965 | switch (Radix) { |
| 966 | case 2: |
| 967 | return NumDigits <= 64; |
| 968 | case 8: |
| 969 | return NumDigits <= 64 / 3; |
| 970 | case 10: |
| 971 | return NumDigits <= 19; |
| 972 | case 16: |
| 973 | return NumDigits <= 64 / 4; |
| 974 | default: |
| 975 | llvm_unreachable("impossible Radix"); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | const unsigned NumDigits = SuffixBegin - DigitsBegin; |
| 990 | if (alwaysFitsInto64Bits(radix, NumDigits)) { |
| 991 | uint64_t N = 0; |
| 992 | for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr) |
| 993 | if (!isDigitSeparator(*Ptr)) |
| 994 | N = N * radix + llvm::hexDigitValue(*Ptr); |
| 995 | |
| 996 | |
| 997 | |
| 998 | Val = N; |
| 999 | return Val.getZExtValue() != N; |
| 1000 | } |
| 1001 | |
| 1002 | Val = 0; |
| 1003 | const char *Ptr = DigitsBegin; |
| 1004 | |
| 1005 | llvm::APInt RadixVal(Val.getBitWidth(), radix); |
| 1006 | llvm::APInt CharVal(Val.getBitWidth(), 0); |
| 1007 | llvm::APInt OldVal = Val; |
| 1008 | |
| 1009 | bool OverflowOccurred = false; |
| 1010 | while (Ptr < SuffixBegin) { |
| 1011 | if (isDigitSeparator(*Ptr)) { |
| 1012 | ++Ptr; |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| 1016 | unsigned C = llvm::hexDigitValue(*Ptr++); |
| 1017 | |
| 1018 | |
| 1019 | (0) . __assert_fail ("C < radix && \"NumericLiteralParser ctor should have rejected this\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1019, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
| 1020 | |
| 1021 | CharVal = C; |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | OldVal = Val; |
| 1026 | |
| 1027 | |
| 1028 | Val *= RadixVal; |
| 1029 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | Val += CharVal; |
| 1034 | OverflowOccurred |= Val.ult(CharVal); |
| 1035 | } |
| 1036 | return OverflowOccurred; |
| 1037 | } |
| 1038 | |
| 1039 | llvm::APFloat::opStatus |
| 1040 | NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) { |
| 1041 | using llvm::APFloat; |
| 1042 | |
| 1043 | unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin); |
| 1044 | |
| 1045 | llvm::SmallString<16> Buffer; |
| 1046 | StringRef Str(ThisTokBegin, n); |
| 1047 | if (Str.find('\'') != StringRef::npos) { |
| 1048 | Buffer.reserve(n); |
| 1049 | std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer), |
| 1050 | &isDigitSeparator); |
| 1051 | Str = Buffer; |
| 1052 | } |
| 1053 | |
| 1054 | return Result.convertFromString(Str, APFloat::rmNearestTiesToEven); |
| 1055 | } |
| 1056 | |
| 1057 | static inline bool IsExponentPart(char c) { |
| 1058 | return c == 'p' || c == 'P' || c == 'e' || c == 'E'; |
| 1059 | } |
| 1060 | |
| 1061 | bool NumericLiteralParser::GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale) { |
| 1062 | assert(radix == 16 || radix == 10); |
| 1063 | |
| 1064 | |
| 1065 | unsigned NumDigits = SuffixBegin - DigitsBegin; |
| 1066 | if (saw_period) --NumDigits; |
| 1067 | |
| 1068 | |
| 1069 | bool ExpOverflowOccurred = false; |
| 1070 | bool NegativeExponent = false; |
| 1071 | const char *ExponentBegin; |
| 1072 | uint64_t Exponent = 0; |
| 1073 | int64_t BaseShift = 0; |
| 1074 | if (saw_exponent) { |
| 1075 | const char *Ptr = DigitsBegin; |
| 1076 | |
| 1077 | while (!IsExponentPart(*Ptr)) ++Ptr; |
| 1078 | ExponentBegin = Ptr; |
| 1079 | ++Ptr; |
| 1080 | NegativeExponent = *Ptr == '-'; |
| 1081 | if (NegativeExponent) ++Ptr; |
| 1082 | |
| 1083 | unsigned NumExpDigits = SuffixBegin - Ptr; |
| 1084 | if (alwaysFitsInto64Bits(radix, NumExpDigits)) { |
| 1085 | llvm::StringRef ExpStr(Ptr, NumExpDigits); |
| 1086 | llvm::APInt ExpInt(, ExpStr, ); |
| 1087 | Exponent = ExpInt.getZExtValue(); |
| 1088 | } else { |
| 1089 | ExpOverflowOccurred = true; |
| 1090 | } |
| 1091 | |
| 1092 | if (NegativeExponent) BaseShift -= Exponent; |
| 1093 | else BaseShift += Exponent; |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | uint64_t NumBitsNeeded; |
| 1114 | if (radix == 10) |
| 1115 | NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale; |
| 1116 | else |
| 1117 | NumBitsNeeded = 4 * NumDigits + Exponent + Scale; |
| 1118 | |
| 1119 | if (NumBitsNeeded > std::numeric_limits<unsigned>::max()) |
| 1120 | ExpOverflowOccurred = true; |
| 1121 | llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0, ); |
| 1122 | |
| 1123 | bool FoundDecimal = false; |
| 1124 | |
| 1125 | int64_t FractBaseShift = 0; |
| 1126 | const char *End = saw_exponent ? ExponentBegin : SuffixBegin; |
| 1127 | for (const char *Ptr = DigitsBegin; Ptr < End; ++Ptr) { |
| 1128 | if (*Ptr == '.') { |
| 1129 | FoundDecimal = true; |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | unsigned C = llvm::hexDigitValue(*Ptr); |
| 1135 | (0) . __assert_fail ("C < radix && \"NumericLiteralParser ctor should have rejected this\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1135, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
| 1136 | |
| 1137 | Val *= radix; |
| 1138 | Val += C; |
| 1139 | |
| 1140 | if (FoundDecimal) |
| 1141 | |
| 1142 | |
| 1143 | --FractBaseShift; |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | if (radix == 16) FractBaseShift *= 4; |
| 1148 | BaseShift += FractBaseShift; |
| 1149 | |
| 1150 | Val <<= Scale; |
| 1151 | |
| 1152 | uint64_t Base = (radix == 16) ? 2 : 10; |
| 1153 | if (BaseShift > 0) { |
| 1154 | for (int64_t i = 0; i < BaseShift; ++i) { |
| 1155 | Val *= Base; |
| 1156 | } |
| 1157 | } else if (BaseShift < 0) { |
| 1158 | for (int64_t i = BaseShift; i < 0 && !Val.isNullValue(); ++i) |
| 1159 | Val = Val.udiv(Base); |
| 1160 | } |
| 1161 | |
| 1162 | bool IntOverflowOccurred = false; |
| 1163 | auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth()); |
| 1164 | if (Val.getBitWidth() > StoreVal.getBitWidth()) { |
| 1165 | IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth())); |
| 1166 | StoreVal = Val.trunc(StoreVal.getBitWidth()); |
| 1167 | } else if (Val.getBitWidth() < StoreVal.getBitWidth()) { |
| 1168 | IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal); |
| 1169 | StoreVal = Val.zext(StoreVal.getBitWidth()); |
| 1170 | } else { |
| 1171 | StoreVal = Val; |
| 1172 | } |
| 1173 | |
| 1174 | return IntOverflowOccurred || ExpOverflowOccurred; |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
| 1217 | SourceLocation Loc, Preprocessor &PP, |
| 1218 | tok::TokenKind kind) { |
| 1219 | |
| 1220 | HadError = false; |
| 1221 | |
| 1222 | Kind = kind; |
| 1223 | |
| 1224 | const char *TokBegin = begin; |
| 1225 | |
| 1226 | |
| 1227 | if (Kind != tok::char_constant) |
| 1228 | ++begin; |
| 1229 | if (Kind == tok::utf8_char_constant) |
| 1230 | ++begin; |
| 1231 | |
| 1232 | |
| 1233 | (0) . __assert_fail ("begin[0] == '\\'' && \"Invalid token lexed\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1233, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(begin[0] == '\'' && "Invalid token lexed"); |
| 1234 | ++begin; |
| 1235 | |
| 1236 | |
| 1237 | if (end[-1] != '\'') { |
| 1238 | const char *UDSuffixEnd = end; |
| 1239 | do { |
| 1240 | --end; |
| 1241 | } while (end[-1] != '\''); |
| 1242 | |
| 1243 | expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end)); |
| 1244 | UDSuffixOffset = end - TokBegin; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | (0) . __assert_fail ("end != begin && \"Invalid token lexed\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1248, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(end != begin && "Invalid token lexed"); |
| 1249 | --end; |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | (0) . __assert_fail ("PP.getTargetInfo().getCharWidth() == 8 && \"Assumes char is 8 bits\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PP.getTargetInfo().getCharWidth() == 8 && |
| 1255 | (0) . __assert_fail ("PP.getTargetInfo().getCharWidth() == 8 && \"Assumes char is 8 bits\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Assumes char is 8 bits"); |
| 1256 | (0) . __assert_fail ("PP.getTargetInfo().getIntWidth() <= 64 && (PP.getTargetInfo().getIntWidth() & 7) == 0 && \"Assumes sizeof(int) on target is <= 64 and a multiple of char\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1258, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PP.getTargetInfo().getIntWidth() <= 64 && |
| 1257 | (0) . __assert_fail ("PP.getTargetInfo().getIntWidth() <= 64 && (PP.getTargetInfo().getIntWidth() & 7) == 0 && \"Assumes sizeof(int) on target is <= 64 and a multiple of char\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1258, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (PP.getTargetInfo().getIntWidth() & 7) == 0 && |
| 1258 | (0) . __assert_fail ("PP.getTargetInfo().getIntWidth() <= 64 && (PP.getTargetInfo().getIntWidth() & 7) == 0 && \"Assumes sizeof(int) on target is <= 64 and a multiple of char\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1258, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Assumes sizeof(int) on target is <= 64 and a multiple of char"); |
| 1259 | (0) . __assert_fail ("PP.getTargetInfo().getWCharWidth() <= 64 && \"Assumes sizeof(wchar) on target is <= 64\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1260, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PP.getTargetInfo().getWCharWidth() <= 64 && |
| 1260 | (0) . __assert_fail ("PP.getTargetInfo().getWCharWidth() <= 64 && \"Assumes sizeof(wchar) on target is <= 64\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1260, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Assumes sizeof(wchar) on target is <= 64"); |
| 1261 | |
| 1262 | SmallVector<uint32_t, 4> codepoint_buffer; |
| 1263 | codepoint_buffer.resize(end - begin); |
| 1264 | uint32_t *buffer_begin = &codepoint_buffer.front(); |
| 1265 | uint32_t *buffer_end = buffer_begin + codepoint_buffer.size(); |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | uint32_t largest_character_for_kind; |
| 1271 | if (tok::wide_char_constant == Kind) { |
| 1272 | largest_character_for_kind = |
| 1273 | 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth()); |
| 1274 | } else if (tok::utf8_char_constant == Kind) { |
| 1275 | largest_character_for_kind = 0x7F; |
| 1276 | } else if (tok::utf16_char_constant == Kind) { |
| 1277 | largest_character_for_kind = 0xFFFF; |
| 1278 | } else if (tok::utf32_char_constant == Kind) { |
| 1279 | largest_character_for_kind = 0x10FFFF; |
| 1280 | } else { |
| 1281 | largest_character_for_kind = 0x7Fu; |
| 1282 | } |
| 1283 | |
| 1284 | while (begin != end) { |
| 1285 | |
| 1286 | if (begin[0] != '\\') { |
| 1287 | char const *start = begin; |
| 1288 | do { |
| 1289 | ++begin; |
| 1290 | } while (begin != end && *begin != '\\'); |
| 1291 | |
| 1292 | char const *tmp_in_start = start; |
| 1293 | uint32_t *tmp_out_start = buffer_begin; |
| 1294 | llvm::ConversionResult res = |
| 1295 | llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start), |
| 1296 | reinterpret_cast<llvm::UTF8 const *>(begin), |
| 1297 | &buffer_begin, buffer_end, llvm::strictConversion); |
| 1298 | if (res != llvm::conversionOK) { |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | bool NoErrorOnBadEncoding = isAscii(); |
| 1303 | unsigned Msg = diag::err_bad_character_encoding; |
| 1304 | if (NoErrorOnBadEncoding) |
| 1305 | Msg = diag::warn_bad_character_encoding; |
| 1306 | PP.Diag(Loc, Msg); |
| 1307 | if (NoErrorOnBadEncoding) { |
| 1308 | start = tmp_in_start; |
| 1309 | buffer_begin = tmp_out_start; |
| 1310 | for (; start != begin; ++start, ++buffer_begin) |
| 1311 | *buffer_begin = static_cast<uint8_t>(*start); |
| 1312 | } else { |
| 1313 | HadError = true; |
| 1314 | } |
| 1315 | } else { |
| 1316 | for (; tmp_out_start < buffer_begin; ++tmp_out_start) { |
| 1317 | if (*tmp_out_start > largest_character_for_kind) { |
| 1318 | HadError = true; |
| 1319 | PP.Diag(Loc, diag::err_character_too_large); |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
| 1327 | if (begin[1] == 'u' || begin[1] == 'U') { |
| 1328 | unsigned short UcnLen = 0; |
| 1329 | if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen, |
| 1330 | FullSourceLoc(Loc, PP.getSourceManager()), |
| 1331 | &PP.getDiagnostics(), PP.getLangOpts(), true)) { |
| 1332 | HadError = true; |
| 1333 | } else if (*buffer_begin > largest_character_for_kind) { |
| 1334 | HadError = true; |
| 1335 | PP.Diag(Loc, diag::err_character_too_large); |
| 1336 | } |
| 1337 | |
| 1338 | ++buffer_begin; |
| 1339 | continue; |
| 1340 | } |
| 1341 | unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo()); |
| 1342 | uint64_t result = |
| 1343 | ProcessCharEscape(TokBegin, begin, end, HadError, |
| 1344 | FullSourceLoc(Loc,PP.getSourceManager()), |
| 1345 | CharWidth, &PP.getDiagnostics(), PP.getLangOpts()); |
| 1346 | *buffer_begin++ = result; |
| 1347 | } |
| 1348 | |
| 1349 | unsigned = buffer_begin - &codepoint_buffer.front(); |
| 1350 | |
| 1351 | if (NumCharsSoFar > 1) { |
| 1352 | if (isWide()) |
| 1353 | PP.Diag(Loc, diag::warn_extraneous_char_constant); |
| 1354 | else if (isAscii() && NumCharsSoFar == 4) |
| 1355 | PP.Diag(Loc, diag::ext_four_char_character_literal); |
| 1356 | else if (isAscii()) |
| 1357 | PP.Diag(Loc, diag::ext_multichar_character_literal); |
| 1358 | else |
| 1359 | PP.Diag(Loc, diag::err_multichar_utf_character_literal); |
| 1360 | IsMultiChar = true; |
| 1361 | } else { |
| 1362 | IsMultiChar = false; |
| 1363 | } |
| 1364 | |
| 1365 | llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0); |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | bool multi_char_too_long = false; |
| 1370 | if (isAscii() && isMultiChar()) { |
| 1371 | LitVal = 0; |
| 1372 | for (size_t i = 0; i < NumCharsSoFar; ++i) { |
| 1373 | |
| 1374 | multi_char_too_long |= (LitVal.countLeadingZeros() < 8); |
| 1375 | LitVal <<= 8; |
| 1376 | LitVal = LitVal + (codepoint_buffer[i] & 0xFF); |
| 1377 | } |
| 1378 | } else if (NumCharsSoFar > 0) { |
| 1379 | |
| 1380 | LitVal = buffer_begin[-1]; |
| 1381 | } |
| 1382 | |
| 1383 | if (!HadError && multi_char_too_long) { |
| 1384 | PP.Diag(Loc, diag::warn_char_constant_too_large); |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | Value = LitVal.getZExtValue(); |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | if (isAscii() && NumCharsSoFar == 1 && (Value & 128) && |
| 1395 | PP.getLangOpts().CharIsSigned) |
| 1396 | Value = (signed char)Value; |
| 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | StringLiteralParser:: |
| 1454 | StringLiteralParser(ArrayRef<Token> StringToks, |
| 1455 | Preprocessor &PP, bool Complain) |
| 1456 | : SM(PP.getSourceManager()), Features(PP.getLangOpts()), |
| 1457 | Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr), |
| 1458 | MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), |
| 1459 | ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) { |
| 1460 | init(StringToks); |
| 1461 | } |
| 1462 | |
| 1463 | void StringLiteralParser::init(ArrayRef<Token> StringToks){ |
| 1464 | |
| 1465 | |
| 1466 | if (StringToks.empty() || StringToks[0].getLength() < 2) |
| 1467 | return DiagnoseLexingError(SourceLocation()); |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | (0) . __assert_fail ("!StringToks.empty() && \"expected at least one token\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1473, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!StringToks.empty() && "expected at least one token"); |
| 1474 | MaxTokenLength = StringToks[0].getLength(); |
| 1475 | (0) . __assert_fail ("StringToks[0].getLength() >= 2 && \"literal token is invalid!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1475, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StringToks[0].getLength() >= 2 && "literal token is invalid!"); |
| 1476 | SizeBound = StringToks[0].getLength()-2; |
| 1477 | Kind = StringToks[0].getKind(); |
| 1478 | |
| 1479 | hadError = false; |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | for (unsigned i = 1; i != StringToks.size(); ++i) { |
| 1484 | if (StringToks[i].getLength() < 2) |
| 1485 | return DiagnoseLexingError(StringToks[i].getLocation()); |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | (0) . __assert_fail ("StringToks[i].getLength() >= 2 && \"literal token is invalid!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1489, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StringToks[i].getLength() >= 2 && "literal token is invalid!"); |
| 1490 | SizeBound += StringToks[i].getLength()-2; |
| 1491 | |
| 1492 | |
| 1493 | if (StringToks[i].getLength() > MaxTokenLength) |
| 1494 | MaxTokenLength = StringToks[i].getLength(); |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) { |
| 1499 | if (isAscii()) { |
| 1500 | Kind = StringToks[i].getKind(); |
| 1501 | } else { |
| 1502 | if (Diags) |
| 1503 | Diags->Report(StringToks[i].getLocation(), |
| 1504 | diag::err_unsupported_string_concat); |
| 1505 | hadError = true; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | |
| 1511 | ++SizeBound; |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | CharByteWidth = getCharWidth(Kind, Target); |
| 1517 | (0) . __assert_fail ("(CharByteWidth & 7) == 0 && \"Assumes character size is byte multiple\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1517, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); |
| 1518 | CharByteWidth /= 8; |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | SizeBound *= CharByteWidth; |
| 1523 | |
| 1524 | |
| 1525 | ResultBuf.resize(SizeBound); |
| 1526 | |
| 1527 | |
| 1528 | SmallString<512> TokenBuf; |
| 1529 | TokenBuf.resize(MaxTokenLength); |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | ResultPtr = &ResultBuf[0]; |
| 1534 | |
| 1535 | Pascal = false; |
| 1536 | |
| 1537 | SourceLocation UDSuffixTokLoc; |
| 1538 | |
| 1539 | for (unsigned i = 0, e = StringToks.size(); i != e; ++i) { |
| 1540 | const char *ThisTokBuf = &TokenBuf[0]; |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | bool StringInvalid = false; |
| 1545 | unsigned ThisTokLen = |
| 1546 | Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features, |
| 1547 | &StringInvalid); |
| 1548 | if (StringInvalid) |
| 1549 | return DiagnoseLexingError(StringToks[i].getLocation()); |
| 1550 | |
| 1551 | const char *ThisTokBegin = ThisTokBuf; |
| 1552 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen; |
| 1553 | |
| 1554 | |
| 1555 | if (ThisTokEnd[-1] != '"') { |
| 1556 | const char *UDSuffixEnd = ThisTokEnd; |
| 1557 | do { |
| 1558 | --ThisTokEnd; |
| 1559 | } while (ThisTokEnd[-1] != '"'); |
| 1560 | |
| 1561 | StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd); |
| 1562 | |
| 1563 | if (UDSuffixBuf.empty()) { |
| 1564 | if (StringToks[i].hasUCN()) |
| 1565 | expandUCNs(UDSuffixBuf, UDSuffix); |
| 1566 | else |
| 1567 | UDSuffixBuf.assign(UDSuffix); |
| 1568 | UDSuffixToken = i; |
| 1569 | UDSuffixOffset = ThisTokEnd - ThisTokBuf; |
| 1570 | UDSuffixTokLoc = StringToks[i].getLocation(); |
| 1571 | } else { |
| 1572 | SmallString<32> ExpandedUDSuffix; |
| 1573 | if (StringToks[i].hasUCN()) { |
| 1574 | expandUCNs(ExpandedUDSuffix, UDSuffix); |
| 1575 | UDSuffix = ExpandedUDSuffix; |
| 1576 | } |
| 1577 | |
| 1578 | |
| 1579 | |
| 1580 | |
| 1581 | |
| 1582 | if (UDSuffixBuf != UDSuffix) { |
| 1583 | if (Diags) { |
| 1584 | SourceLocation TokLoc = StringToks[i].getLocation(); |
| 1585 | Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix) |
| 1586 | << UDSuffixBuf << UDSuffix |
| 1587 | << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc) |
| 1588 | << SourceRange(TokLoc, TokLoc); |
| 1589 | } |
| 1590 | hadError = true; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | --ThisTokEnd; |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') { |
| 1602 | ++ThisTokBuf; |
| 1603 | |
| 1604 | if (ThisTokBuf[0] == '8') |
| 1605 | ++ThisTokBuf; |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | if (ThisTokBuf[0] == 'R') { |
| 1610 | ThisTokBuf += 2; |
| 1611 | |
| 1612 | const char *Prefix = ThisTokBuf; |
| 1613 | while (ThisTokBuf[0] != '(') |
| 1614 | ++ThisTokBuf; |
| 1615 | ++ThisTokBuf; |
| 1616 | |
| 1617 | |
| 1618 | ThisTokEnd -= ThisTokBuf - Prefix; |
| 1619 | (0) . __assert_fail ("ThisTokEnd >= ThisTokBuf && \"malformed raw string literal\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1619, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal"); |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf); |
| 1624 | while (!RemainingTokenSpan.empty()) { |
| 1625 | |
| 1626 | size_t CRLFPos = RemainingTokenSpan.find("\r\n"); |
| 1627 | StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos); |
| 1628 | StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos); |
| 1629 | |
| 1630 | |
| 1631 | if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF)) |
| 1632 | hadError = true; |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | RemainingTokenSpan = AfterCRLF.substr(1); |
| 1637 | } |
| 1638 | } else { |
| 1639 | if (ThisTokBuf[0] != '"') { |
| 1640 | |
| 1641 | |
| 1642 | return DiagnoseLexingError(StringToks[i].getLocation()); |
| 1643 | } |
| 1644 | ++ThisTokBuf; |
| 1645 | |
| 1646 | |
| 1647 | if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd && |
| 1648 | ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') { |
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | if (i == 0) { |
| 1653 | ++ThisTokBuf; |
| 1654 | Pascal = true; |
| 1655 | } else if (Pascal) |
| 1656 | ThisTokBuf += 2; |
| 1657 | } |
| 1658 | |
| 1659 | while (ThisTokBuf != ThisTokEnd) { |
| 1660 | |
| 1661 | if (ThisTokBuf[0] != '\\') { |
| 1662 | const char *InStart = ThisTokBuf; |
| 1663 | do { |
| 1664 | ++ThisTokBuf; |
| 1665 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 1666 | |
| 1667 | |
| 1668 | if (CopyStringFragment(StringToks[i], ThisTokBegin, |
| 1669 | StringRef(InStart, ThisTokBuf - InStart))) |
| 1670 | hadError = true; |
| 1671 | continue; |
| 1672 | } |
| 1673 | |
| 1674 | if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') { |
| 1675 | EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, |
| 1676 | ResultPtr, hadError, |
| 1677 | FullSourceLoc(StringToks[i].getLocation(), SM), |
| 1678 | CharByteWidth, Diags, Features); |
| 1679 | continue; |
| 1680 | } |
| 1681 | |
| 1682 | unsigned ResultChar = |
| 1683 | ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError, |
| 1684 | FullSourceLoc(StringToks[i].getLocation(), SM), |
| 1685 | CharByteWidth*8, Diags, Features); |
| 1686 | |
| 1687 | if (CharByteWidth == 4) { |
| 1688 | |
| 1689 | |
| 1690 | llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultPtr); |
| 1691 | *ResultWidePtr = ResultChar; |
| 1692 | ResultPtr += 4; |
| 1693 | } else if (CharByteWidth == 2) { |
| 1694 | |
| 1695 | |
| 1696 | llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultPtr); |
| 1697 | *ResultWidePtr = ResultChar & 0xFFFF; |
| 1698 | ResultPtr += 2; |
| 1699 | } else { |
| 1700 | (0) . __assert_fail ("CharByteWidth == 1 && \"Unexpected char width\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CharByteWidth == 1 && "Unexpected char width"); |
| 1701 | *ResultPtr++ = ResultChar & 0xFF; |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | if (Pascal) { |
| 1708 | if (CharByteWidth == 4) { |
| 1709 | |
| 1710 | |
| 1711 | llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultBuf.data()); |
| 1712 | ResultWidePtr[0] = GetNumStringChars() - 1; |
| 1713 | } else if (CharByteWidth == 2) { |
| 1714 | |
| 1715 | |
| 1716 | llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultBuf.data()); |
| 1717 | ResultWidePtr[0] = GetNumStringChars() - 1; |
| 1718 | } else { |
| 1719 | (0) . __assert_fail ("CharByteWidth == 1 && \"Unexpected char width\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1719, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CharByteWidth == 1 && "Unexpected char width"); |
| 1720 | ResultBuf[0] = GetNumStringChars() - 1; |
| 1721 | } |
| 1722 | |
| 1723 | |
| 1724 | if (GetStringLength() > 256) { |
| 1725 | if (Diags) |
| 1726 | Diags->Report(StringToks.front().getLocation(), |
| 1727 | diag::err_pascal_string_too_long) |
| 1728 | << SourceRange(StringToks.front().getLocation(), |
| 1729 | StringToks.back().getLocation()); |
| 1730 | hadError = true; |
| 1731 | return; |
| 1732 | } |
| 1733 | } else if (Diags) { |
| 1734 | |
| 1735 | unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509; |
| 1736 | |
| 1737 | if (GetNumStringChars() > MaxChars) |
| 1738 | Diags->Report(StringToks.front().getLocation(), |
| 1739 | diag::ext_string_too_long) |
| 1740 | << GetNumStringChars() << MaxChars |
| 1741 | << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0) |
| 1742 | << SourceRange(StringToks.front().getLocation(), |
| 1743 | StringToks.back().getLocation()); |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | static const char *resyncUTF8(const char *Err, const char *End) { |
| 1748 | if (Err == End) |
| 1749 | return End; |
| 1750 | End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err); |
| 1751 | while (++Err != End && (*Err & 0xC0) == 0x80) |
| 1752 | ; |
| 1753 | return Err; |
| 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | |
| 1758 | |
| 1759 | bool StringLiteralParser::CopyStringFragment(const Token &Tok, |
| 1760 | const char *TokBegin, |
| 1761 | StringRef Fragment) { |
| 1762 | const llvm::UTF8 *ErrorPtrTmp; |
| 1763 | if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp)) |
| 1764 | return false; |
| 1765 | |
| 1766 | |
| 1767 | |
| 1768 | |
| 1769 | bool NoErrorOnBadEncoding = isAscii(); |
| 1770 | if (NoErrorOnBadEncoding) { |
| 1771 | memcpy(ResultPtr, Fragment.data(), Fragment.size()); |
| 1772 | ResultPtr += Fragment.size(); |
| 1773 | } |
| 1774 | |
| 1775 | if (Diags) { |
| 1776 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
| 1777 | |
| 1778 | FullSourceLoc SourceLoc(Tok.getLocation(), SM); |
| 1779 | const DiagnosticBuilder &Builder = |
| 1780 | Diag(Diags, Features, SourceLoc, TokBegin, |
| 1781 | ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()), |
| 1782 | NoErrorOnBadEncoding ? diag::warn_bad_string_encoding |
| 1783 | : diag::err_bad_string_encoding); |
| 1784 | |
| 1785 | const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end()); |
| 1786 | StringRef NextFragment(NextStart, Fragment.end()-NextStart); |
| 1787 | |
| 1788 | |
| 1789 | SmallString<512> Dummy; |
| 1790 | Dummy.reserve(Fragment.size() * CharByteWidth); |
| 1791 | char *Ptr = Dummy.data(); |
| 1792 | |
| 1793 | while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) { |
| 1794 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
| 1795 | NextStart = resyncUTF8(ErrorPtr, Fragment.end()); |
| 1796 | Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin, |
| 1797 | ErrorPtr, NextStart); |
| 1798 | NextFragment = StringRef(NextStart, Fragment.end()-NextStart); |
| 1799 | } |
| 1800 | } |
| 1801 | return !NoErrorOnBadEncoding; |
| 1802 | } |
| 1803 | |
| 1804 | void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) { |
| 1805 | hadError = true; |
| 1806 | if (Diags) |
| 1807 | Diags->Report(Loc, diag::err_lexing_string); |
| 1808 | } |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | |
| 1813 | unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok, |
| 1814 | unsigned ByteNo) const { |
| 1815 | |
| 1816 | SmallString<32> SpellingBuffer; |
| 1817 | SpellingBuffer.resize(Tok.getLength()); |
| 1818 | |
| 1819 | bool StringInvalid = false; |
| 1820 | const char *SpellingPtr = &SpellingBuffer[0]; |
| 1821 | unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features, |
| 1822 | &StringInvalid); |
| 1823 | if (StringInvalid) |
| 1824 | return 0; |
| 1825 | |
| 1826 | const char *SpellingStart = SpellingPtr; |
| 1827 | const char *SpellingEnd = SpellingPtr+TokLen; |
| 1828 | |
| 1829 | |
| 1830 | if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8') |
| 1831 | SpellingPtr += 2; |
| 1832 | |
| 1833 | (0) . __assert_fail ("SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && SpellingPtr[0] != 'U' && \"Doesn't handle wide or utf strings yet\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1834, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && |
| 1834 | (0) . __assert_fail ("SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && SpellingPtr[0] != 'U' && \"Doesn't handle wide or utf strings yet\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1834, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet"); |
| 1835 | |
| 1836 | |
| 1837 | if (SpellingPtr[0] == 'R') { |
| 1838 | (0) . __assert_fail ("SpellingPtr[1] == '\"' && \"Should be a raw string literal!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1838, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SpellingPtr[1] == '"' && "Should be a raw string literal!"); |
| 1839 | |
| 1840 | SpellingPtr += 2; |
| 1841 | while (*SpellingPtr != '(') { |
| 1842 | ++SpellingPtr; |
| 1843 | (0) . __assert_fail ("SpellingPtr < SpellingEnd && \"Missing ( for raw string literal\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1843, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal"); |
| 1844 | } |
| 1845 | |
| 1846 | ++SpellingPtr; |
| 1847 | return SpellingPtr - SpellingStart + ByteNo; |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | (0) . __assert_fail ("SpellingPtr[0] == '\"' && \"Should be a string literal!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1851, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SpellingPtr[0] == '"' && "Should be a string literal!"); |
| 1852 | ++SpellingPtr; |
| 1853 | |
| 1854 | |
| 1855 | while (ByteNo) { |
| 1856 | (0) . __assert_fail ("SpellingPtr < SpellingEnd && \"Didn't find byte offset!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1856, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!"); |
| 1857 | |
| 1858 | |
| 1859 | if (*SpellingPtr != '\\') { |
| 1860 | ++SpellingPtr; |
| 1861 | --ByteNo; |
| 1862 | continue; |
| 1863 | } |
| 1864 | |
| 1865 | |
| 1866 | bool HadError = false; |
| 1867 | if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') { |
| 1868 | const char *EscapePtr = SpellingPtr; |
| 1869 | unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd, |
| 1870 | 1, Features, HadError); |
| 1871 | if (Len > ByteNo) { |
| 1872 | |
| 1873 | SpellingPtr = EscapePtr; |
| 1874 | break; |
| 1875 | } |
| 1876 | ByteNo -= Len; |
| 1877 | } else { |
| 1878 | ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError, |
| 1879 | FullSourceLoc(Tok.getLocation(), SM), |
| 1880 | CharByteWidth*8, Diags, Features); |
| 1881 | --ByteNo; |
| 1882 | } |
| 1883 | (0) . __assert_fail ("!HadError && \"This method isn't valid on erroneous strings\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/LiteralSupport.cpp", 1883, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!HadError && "This method isn't valid on erroneous strings"); |
| 1884 | } |
| 1885 | |
| 1886 | return SpellingPtr-SpellingStart; |
| 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | bool StringLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, |
| 1893 | StringRef Suffix) { |
| 1894 | return NumericLiteralParser::isValidUDSuffix(LangOpts, Suffix) || |
| 1895 | Suffix == "sv"; |
| 1896 | } |
| 1897 | |