1 // FormatString.cpp - Common stuff for handling printf/scanf formats -*- C++ -*- 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Shared details for processing format strings of printf and scanf 10 // (and friends). 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "FormatStringParsing.h" 15 #include "clang/Basic/LangOptions.h" 16 #include "clang/Basic/TargetInfo.h" 17 #include "llvm/Support/ConvertUTF.h" 18 #include <optional> 19 20 using clang::analyze_format_string::ArgType; 21 using clang::analyze_format_string::FormatStringHandler; 22 using clang::analyze_format_string::FormatSpecifier; 23 using clang::analyze_format_string::LengthModifier; 24 using clang::analyze_format_string::OptionalAmount; 25 using clang::analyze_format_string::ConversionSpecifier; 26 using namespace clang; 27 28 // Key function to FormatStringHandler. 29 FormatStringHandler::~FormatStringHandler() {} 30 31 //===----------------------------------------------------------------------===// 32 // Functions for parsing format strings components in both printf and 33 // scanf format strings. 34 //===----------------------------------------------------------------------===// 35 36 OptionalAmount 37 clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) { 38 const char *I = Beg; 39 UpdateOnReturn <const char*> UpdateBeg(Beg, I); 40 41 unsigned accumulator = 0; 42 bool hasDigits = false; 43 44 for ( ; I != E; ++I) { 45 char c = *I; 46 if (c >= '0' && c <= '9') { 47 hasDigits = true; 48 accumulator = (accumulator * 10) + (c - '0'); 49 continue; 50 } 51 52 if (hasDigits) 53 return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg, 54 false); 55 56 break; 57 } 58 59 return OptionalAmount(); 60 } 61 62 OptionalAmount 63 clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg, 64 const char *E, 65 unsigned &argIndex) { 66 if (*Beg == '*') { 67 ++Beg; 68 return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false); 69 } 70 71 return ParseAmount(Beg, E); 72 } 73 74 OptionalAmount 75 clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H, 76 const char *Start, 77 const char *&Beg, 78 const char *E, 79 PositionContext p) { 80 if (*Beg == '*') { 81 const char *I = Beg + 1; 82 const OptionalAmount &Amt = ParseAmount(I, E); 83 84 if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) { 85 H.HandleInvalidPosition(Beg, I - Beg, p); 86 return OptionalAmount(false); 87 } 88 89 if (I == E) { 90 // No more characters left? 91 H.HandleIncompleteSpecifier(Start, E - Start); 92 return OptionalAmount(false); 93 } 94 95 assert(Amt.getHowSpecified() == OptionalAmount::Constant); 96 97 if (*I == '$') { 98 // Handle positional arguments 99 100 // Special case: '*0$', since this is an easy mistake. 101 if (Amt.getConstantAmount() == 0) { 102 H.HandleZeroPosition(Beg, I - Beg + 1); 103 return OptionalAmount(false); 104 } 105 106 const char *Tmp = Beg; 107 Beg = ++I; 108 109 return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1, 110 Tmp, 0, true); 111 } 112 113 H.HandleInvalidPosition(Beg, I - Beg, p); 114 return OptionalAmount(false); 115 } 116 117 return ParseAmount(Beg, E); 118 } 119 120 121 bool 122 clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H, 123 FormatSpecifier &CS, 124 const char *Start, 125 const char *&Beg, const char *E, 126 unsigned *argIndex) { 127 // FIXME: Support negative field widths. 128 if (argIndex) { 129 CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex)); 130 } 131 else { 132 const OptionalAmount Amt = 133 ParsePositionAmount(H, Start, Beg, E, 134 analyze_format_string::FieldWidthPos); 135 136 if (Amt.isInvalid()) 137 return true; 138 CS.setFieldWidth(Amt); 139 } 140 return false; 141 } 142 143 bool 144 clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H, 145 FormatSpecifier &FS, 146 const char *Start, 147 const char *&Beg, 148 const char *E) { 149 const char *I = Beg; 150 151 const OptionalAmount &Amt = ParseAmount(I, E); 152 153 if (I == E) { 154 // No more characters left? 155 H.HandleIncompleteSpecifier(Start, E - Start); 156 return true; 157 } 158 159 if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') { 160 // Warn that positional arguments are non-standard. 161 H.HandlePosition(Start, I - Start); 162 163 // Special case: '%0$', since this is an easy mistake. 164 if (Amt.getConstantAmount() == 0) { 165 H.HandleZeroPosition(Start, I - Start); 166 return true; 167 } 168 169 FS.setArgIndex(Amt.getConstantAmount() - 1); 170 FS.setUsesPositionalArg(); 171 // Update the caller's pointer if we decided to consume 172 // these characters. 173 Beg = I; 174 return false; 175 } 176 177 return false; 178 } 179 180 bool 181 clang::analyze_format_string::ParseVectorModifier(FormatStringHandler &H, 182 FormatSpecifier &FS, 183 const char *&I, 184 const char *E, 185 const LangOptions &LO) { 186 if (!LO.OpenCL) 187 return false; 188 189 const char *Start = I; 190 if (*I == 'v') { 191 ++I; 192 193 if (I == E) { 194 H.HandleIncompleteSpecifier(Start, E - Start); 195 return true; 196 } 197 198 OptionalAmount NumElts = ParseAmount(I, E); 199 if (NumElts.getHowSpecified() != OptionalAmount::Constant) { 200 H.HandleIncompleteSpecifier(Start, E - Start); 201 return true; 202 } 203 204 FS.setVectorNumElts(NumElts); 205 } 206 207 return false; 208 } 209 210 bool 211 clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS, 212 const char *&I, 213 const char *E, 214 const LangOptions &LO, 215 bool IsScanf) { 216 LengthModifier::Kind lmKind = LengthModifier::None; 217 const char *lmPosition = I; 218 switch (*I) { 219 default: 220 return false; 221 case 'h': 222 ++I; 223 if (I != E && *I == 'h') { 224 ++I; 225 lmKind = LengthModifier::AsChar; 226 } else if (I != E && *I == 'l' && LO.OpenCL) { 227 ++I; 228 lmKind = LengthModifier::AsShortLong; 229 } else { 230 lmKind = LengthModifier::AsShort; 231 } 232 break; 233 case 'l': 234 ++I; 235 if (I != E && *I == 'l') { 236 ++I; 237 lmKind = LengthModifier::AsLongLong; 238 } else { 239 lmKind = LengthModifier::AsLong; 240 } 241 break; 242 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break; 243 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break; 244 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break; 245 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break; 246 case 'q': lmKind = LengthModifier::AsQuad; ++I; break; 247 case 'a': 248 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) { 249 // For scanf in C90, look at the next character to see if this should 250 // be parsed as the GNU extension 'a' length modifier. If not, this 251 // will be parsed as a conversion specifier. 252 ++I; 253 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) { 254 lmKind = LengthModifier::AsAllocate; 255 break; 256 } 257 --I; 258 } 259 return false; 260 case 'm': 261 if (IsScanf) { 262 lmKind = LengthModifier::AsMAllocate; 263 ++I; 264 break; 265 } 266 return false; 267 // printf: AsInt64, AsInt32, AsInt3264 268 // scanf: AsInt64 269 case 'I': 270 if (I + 1 != E && I + 2 != E) { 271 if (I[1] == '6' && I[2] == '4') { 272 I += 3; 273 lmKind = LengthModifier::AsInt64; 274 break; 275 } 276 if (IsScanf) 277 return false; 278 279 if (I[1] == '3' && I[2] == '2') { 280 I += 3; 281 lmKind = LengthModifier::AsInt32; 282 break; 283 } 284 } 285 ++I; 286 lmKind = LengthModifier::AsInt3264; 287 break; 288 case 'w': 289 lmKind = LengthModifier::AsWide; ++I; break; 290 } 291 LengthModifier lm(lmPosition, lmKind); 292 FS.setLengthModifier(lm); 293 return true; 294 } 295 296 bool clang::analyze_format_string::ParseUTF8InvalidSpecifier( 297 const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len) { 298 if (SpecifierBegin + 1 >= FmtStrEnd) 299 return false; 300 301 const llvm::UTF8 *SB = 302 reinterpret_cast<const llvm::UTF8 *>(SpecifierBegin + 1); 303 const llvm::UTF8 *SE = reinterpret_cast<const llvm::UTF8 *>(FmtStrEnd); 304 const char FirstByte = *SB; 305 306 // If the invalid specifier is a multibyte UTF-8 string, return the 307 // total length accordingly so that the conversion specifier can be 308 // properly updated to reflect a complete UTF-8 specifier. 309 unsigned NumBytes = llvm::getNumBytesForUTF8(FirstByte); 310 if (NumBytes == 1) 311 return false; 312 if (SB + NumBytes > SE) 313 return false; 314 315 Len = NumBytes + 1; 316 return true; 317 } 318 319 //===----------------------------------------------------------------------===// 320 // Methods on ArgType. 321 //===----------------------------------------------------------------------===// 322 323 clang::analyze_format_string::ArgType::MatchKind 324 ArgType::matchesType(ASTContext &C, QualType argTy) const { 325 // When using the format attribute in C++, you can receive a function or an 326 // array that will necessarily decay to a pointer when passed to the final 327 // format consumer. Apply decay before type comparison. 328 if (argTy->canDecayToPointerType()) 329 argTy = C.getDecayedType(argTy); 330 331 if (Ptr) { 332 // It has to be a pointer. 333 const PointerType *PT = argTy->getAs<PointerType>(); 334 if (!PT) 335 return NoMatch; 336 337 // We cannot write through a const qualified pointer. 338 if (PT->getPointeeType().isConstQualified()) 339 return NoMatch; 340 341 argTy = PT->getPointeeType(); 342 } 343 344 switch (K) { 345 case InvalidTy: 346 llvm_unreachable("ArgType must be valid"); 347 348 case UnknownTy: 349 return Match; 350 351 case AnyCharTy: { 352 if (const auto *ETy = argTy->getAs<EnumType>()) { 353 // If the enum is incomplete we know nothing about the underlying type. 354 // Assume that it's 'int'. Do not use the underlying type for a scoped 355 // enumeration. 356 if (!ETy->getDecl()->isComplete()) 357 return NoMatch; 358 if (ETy->isUnscopedEnumerationType()) 359 argTy = ETy->getDecl()->getIntegerType(); 360 } 361 362 if (const auto *BT = argTy->getAs<BuiltinType>()) { 363 // The types are perfectly matched? 364 switch (BT->getKind()) { 365 default: 366 break; 367 case BuiltinType::Char_S: 368 case BuiltinType::SChar: 369 case BuiltinType::UChar: 370 case BuiltinType::Char_U: 371 case BuiltinType::Bool: 372 return Match; 373 } 374 // "Partially matched" because of promotions? 375 if (!Ptr) { 376 switch (BT->getKind()) { 377 default: 378 break; 379 case BuiltinType::Int: 380 case BuiltinType::UInt: 381 return MatchPromotion; 382 case BuiltinType::Short: 383 case BuiltinType::UShort: 384 case BuiltinType::WChar_S: 385 case BuiltinType::WChar_U: 386 return NoMatchPromotionTypeConfusion; 387 } 388 } 389 } 390 return NoMatch; 391 } 392 393 case SpecificTy: { 394 if (const EnumType *ETy = argTy->getAs<EnumType>()) { 395 // If the enum is incomplete we know nothing about the underlying type. 396 // Assume that it's 'int'. Do not use the underlying type for a scoped 397 // enumeration as that needs an exact match. 398 if (!ETy->getDecl()->isComplete()) 399 argTy = C.IntTy; 400 else if (ETy->isUnscopedEnumerationType()) 401 argTy = ETy->getDecl()->getIntegerType(); 402 } 403 argTy = C.getCanonicalType(argTy).getUnqualifiedType(); 404 405 if (T == argTy) 406 return Match; 407 if (const auto *BT = argTy->getAs<BuiltinType>()) { 408 // Check if the only difference between them is signed vs unsigned 409 // if true, we consider they are compatible. 410 switch (BT->getKind()) { 411 default: 412 break; 413 case BuiltinType::Char_S: 414 case BuiltinType::SChar: 415 case BuiltinType::Char_U: 416 case BuiltinType::UChar: 417 case BuiltinType::Bool: 418 if (T == C.UnsignedShortTy || T == C.ShortTy) 419 return NoMatchTypeConfusion; 420 if (T == C.UnsignedCharTy || T == C.SignedCharTy) 421 return Match; 422 break; 423 case BuiltinType::Short: 424 if (T == C.UnsignedShortTy) 425 return Match; 426 break; 427 case BuiltinType::UShort: 428 if (T == C.ShortTy) 429 return Match; 430 break; 431 case BuiltinType::Int: 432 if (T == C.UnsignedIntTy) 433 return Match; 434 break; 435 case BuiltinType::UInt: 436 if (T == C.IntTy) 437 return Match; 438 break; 439 case BuiltinType::Long: 440 if (T == C.UnsignedLongTy) 441 return Match; 442 break; 443 case BuiltinType::ULong: 444 if (T == C.LongTy) 445 return Match; 446 break; 447 case BuiltinType::LongLong: 448 if (T == C.UnsignedLongLongTy) 449 return Match; 450 break; 451 case BuiltinType::ULongLong: 452 if (T == C.LongLongTy) 453 return Match; 454 break; 455 } 456 // "Partially matched" because of promotions? 457 if (!Ptr) { 458 switch (BT->getKind()) { 459 default: 460 break; 461 case BuiltinType::Int: 462 case BuiltinType::UInt: 463 if (T == C.SignedCharTy || T == C.UnsignedCharTy || 464 T == C.ShortTy || T == C.UnsignedShortTy || T == C.WCharTy || 465 T == C.WideCharTy) 466 return MatchPromotion; 467 break; 468 case BuiltinType::Short: 469 case BuiltinType::UShort: 470 if (T == C.SignedCharTy || T == C.UnsignedCharTy) 471 return NoMatchPromotionTypeConfusion; 472 break; 473 case BuiltinType::WChar_U: 474 case BuiltinType::WChar_S: 475 if (T != C.WCharTy && T != C.WideCharTy) 476 return NoMatchPromotionTypeConfusion; 477 } 478 } 479 } 480 return NoMatch; 481 } 482 483 case CStrTy: { 484 const PointerType *PT = argTy->getAs<PointerType>(); 485 if (!PT) 486 return NoMatch; 487 QualType pointeeTy = PT->getPointeeType(); 488 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>()) 489 switch (BT->getKind()) { 490 case BuiltinType::Char_U: 491 case BuiltinType::UChar: 492 case BuiltinType::Char_S: 493 case BuiltinType::SChar: 494 return Match; 495 default: 496 break; 497 } 498 499 return NoMatch; 500 } 501 502 case WCStrTy: { 503 const PointerType *PT = argTy->getAs<PointerType>(); 504 if (!PT) 505 return NoMatch; 506 QualType pointeeTy = 507 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType(); 508 return pointeeTy == C.getWideCharType() ? Match : NoMatch; 509 } 510 511 case WIntTy: { 512 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType(); 513 514 if (C.getCanonicalType(argTy).getUnqualifiedType() == WInt) 515 return Match; 516 517 QualType PromoArg = C.isPromotableIntegerType(argTy) 518 ? C.getPromotedIntegerType(argTy) 519 : argTy; 520 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType(); 521 522 // If the promoted argument is the corresponding signed type of the 523 // wint_t type, then it should match. 524 if (PromoArg->hasSignedIntegerRepresentation() && 525 C.getCorrespondingUnsignedType(PromoArg) == WInt) 526 return Match; 527 528 return WInt == PromoArg ? Match : NoMatch; 529 } 530 531 case CPointerTy: 532 if (argTy->isVoidPointerType()) { 533 return Match; 534 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() || 535 argTy->isBlockPointerType() || argTy->isNullPtrType()) { 536 return NoMatchPedantic; 537 } else { 538 return NoMatch; 539 } 540 541 case ObjCPointerTy: { 542 if (argTy->getAs<ObjCObjectPointerType>() || 543 argTy->getAs<BlockPointerType>()) 544 return Match; 545 546 // Handle implicit toll-free bridging. 547 if (const PointerType *PT = argTy->getAs<PointerType>()) { 548 // Things such as CFTypeRef are really just opaque pointers 549 // to C structs representing CF types that can often be bridged 550 // to Objective-C objects. Since the compiler doesn't know which 551 // structs can be toll-free bridged, we just accept them all. 552 QualType pointee = PT->getPointeeType(); 553 if (pointee->getAsStructureType() || pointee->isVoidType()) 554 return Match; 555 } 556 return NoMatch; 557 } 558 } 559 560 llvm_unreachable("Invalid ArgType Kind!"); 561 } 562 563 ArgType ArgType::makeVectorType(ASTContext &C, unsigned NumElts) const { 564 // Check for valid vector element types. 565 if (T.isNull()) 566 return ArgType::Invalid(); 567 568 QualType Vec = C.getExtVectorType(T, NumElts); 569 return ArgType(Vec, Name); 570 } 571 572 QualType ArgType::getRepresentativeType(ASTContext &C) const { 573 QualType Res; 574 switch (K) { 575 case InvalidTy: 576 llvm_unreachable("No representative type for Invalid ArgType"); 577 case UnknownTy: 578 llvm_unreachable("No representative type for Unknown ArgType"); 579 case AnyCharTy: 580 Res = C.CharTy; 581 break; 582 case SpecificTy: 583 Res = T; 584 break; 585 case CStrTy: 586 Res = C.getPointerType(C.CharTy); 587 break; 588 case WCStrTy: 589 Res = C.getPointerType(C.getWideCharType()); 590 break; 591 case ObjCPointerTy: 592 Res = C.ObjCBuiltinIdTy; 593 break; 594 case CPointerTy: 595 Res = C.VoidPtrTy; 596 break; 597 case WIntTy: { 598 Res = C.getWIntType(); 599 break; 600 } 601 } 602 603 if (Ptr) 604 Res = C.getPointerType(Res); 605 return Res; 606 } 607 608 std::string ArgType::getRepresentativeTypeName(ASTContext &C) const { 609 std::string S = getRepresentativeType(C).getAsString(C.getPrintingPolicy()); 610 611 std::string Alias; 612 if (Name) { 613 // Use a specific name for this type, e.g. "size_t". 614 Alias = Name; 615 if (Ptr) { 616 // If ArgType is actually a pointer to T, append an asterisk. 617 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *"; 618 } 619 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it. 620 if (S == Alias) 621 Alias.clear(); 622 } 623 624 if (!Alias.empty()) 625 return std::string("'") + Alias + "' (aka '" + S + "')"; 626 return std::string("'") + S + "'"; 627 } 628 629 630 //===----------------------------------------------------------------------===// 631 // Methods on OptionalAmount. 632 //===----------------------------------------------------------------------===// 633 634 ArgType 635 analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const { 636 return Ctx.IntTy; 637 } 638 639 //===----------------------------------------------------------------------===// 640 // Methods on LengthModifier. 641 //===----------------------------------------------------------------------===// 642 643 const char * 644 analyze_format_string::LengthModifier::toString() const { 645 switch (kind) { 646 case AsChar: 647 return "hh"; 648 case AsShort: 649 return "h"; 650 case AsShortLong: 651 return "hl"; 652 case AsLong: // or AsWideChar 653 return "l"; 654 case AsLongLong: 655 return "ll"; 656 case AsQuad: 657 return "q"; 658 case AsIntMax: 659 return "j"; 660 case AsSizeT: 661 return "z"; 662 case AsPtrDiff: 663 return "t"; 664 case AsInt32: 665 return "I32"; 666 case AsInt3264: 667 return "I"; 668 case AsInt64: 669 return "I64"; 670 case AsLongDouble: 671 return "L"; 672 case AsAllocate: 673 return "a"; 674 case AsMAllocate: 675 return "m"; 676 case AsWide: 677 return "w"; 678 case None: 679 return ""; 680 } 681 return nullptr; 682 } 683 684 //===----------------------------------------------------------------------===// 685 // Methods on ConversionSpecifier. 686 //===----------------------------------------------------------------------===// 687 688 const char *ConversionSpecifier::toString() const { 689 switch (kind) { 690 case bArg: return "b"; 691 case BArg: return "B"; 692 case dArg: return "d"; 693 case DArg: return "D"; 694 case iArg: return "i"; 695 case oArg: return "o"; 696 case OArg: return "O"; 697 case uArg: return "u"; 698 case UArg: return "U"; 699 case xArg: return "x"; 700 case XArg: return "X"; 701 case fArg: return "f"; 702 case FArg: return "F"; 703 case eArg: return "e"; 704 case EArg: return "E"; 705 case gArg: return "g"; 706 case GArg: return "G"; 707 case aArg: return "a"; 708 case AArg: return "A"; 709 case cArg: return "c"; 710 case sArg: return "s"; 711 case pArg: return "p"; 712 case PArg: 713 return "P"; 714 case nArg: return "n"; 715 case PercentArg: return "%"; 716 case ScanListArg: return "["; 717 case InvalidSpecifier: return nullptr; 718 719 // POSIX unicode extensions. 720 case CArg: return "C"; 721 case SArg: return "S"; 722 723 // Objective-C specific specifiers. 724 case ObjCObjArg: return "@"; 725 726 // FreeBSD kernel specific specifiers. 727 case FreeBSDbArg: return "b"; 728 case FreeBSDDArg: return "D"; 729 case FreeBSDrArg: return "r"; 730 case FreeBSDyArg: return "y"; 731 732 // GlibC specific specifiers. 733 case PrintErrno: return "m"; 734 735 // MS specific specifiers. 736 case ZArg: return "Z"; 737 } 738 return nullptr; 739 } 740 741 std::optional<ConversionSpecifier> 742 ConversionSpecifier::getStandardSpecifier() const { 743 ConversionSpecifier::Kind NewKind; 744 745 switch (getKind()) { 746 default: 747 return std::nullopt; 748 case DArg: 749 NewKind = dArg; 750 break; 751 case UArg: 752 NewKind = uArg; 753 break; 754 case OArg: 755 NewKind = oArg; 756 break; 757 } 758 759 ConversionSpecifier FixedCS(*this); 760 FixedCS.setKind(NewKind); 761 return FixedCS; 762 } 763 764 //===----------------------------------------------------------------------===// 765 // Methods on OptionalAmount. 766 //===----------------------------------------------------------------------===// 767 768 void OptionalAmount::toString(raw_ostream &os) const { 769 switch (hs) { 770 case Invalid: 771 case NotSpecified: 772 return; 773 case Arg: 774 if (UsesDotPrefix) 775 os << "."; 776 if (usesPositionalArg()) 777 os << "*" << getPositionalArgIndex() << "$"; 778 else 779 os << "*"; 780 break; 781 case Constant: 782 if (UsesDotPrefix) 783 os << "."; 784 os << amt; 785 break; 786 } 787 } 788 789 bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target, 790 const LangOptions &LO) const { 791 switch (LM.getKind()) { 792 case LengthModifier::None: 793 return true; 794 795 // Handle most integer flags 796 case LengthModifier::AsShort: 797 // Length modifier only applies to FP vectors. 798 if (LO.OpenCL && CS.isDoubleArg()) 799 return !VectorNumElts.isInvalid(); 800 801 if (Target.getTriple().isOSMSVCRT()) { 802 switch (CS.getKind()) { 803 case ConversionSpecifier::cArg: 804 case ConversionSpecifier::CArg: 805 case ConversionSpecifier::sArg: 806 case ConversionSpecifier::SArg: 807 case ConversionSpecifier::ZArg: 808 return true; 809 default: 810 break; 811 } 812 } 813 [[fallthrough]]; 814 case LengthModifier::AsChar: 815 case LengthModifier::AsLongLong: 816 case LengthModifier::AsQuad: 817 case LengthModifier::AsIntMax: 818 case LengthModifier::AsSizeT: 819 case LengthModifier::AsPtrDiff: 820 switch (CS.getKind()) { 821 case ConversionSpecifier::bArg: 822 case ConversionSpecifier::BArg: 823 case ConversionSpecifier::dArg: 824 case ConversionSpecifier::DArg: 825 case ConversionSpecifier::iArg: 826 case ConversionSpecifier::oArg: 827 case ConversionSpecifier::OArg: 828 case ConversionSpecifier::uArg: 829 case ConversionSpecifier::UArg: 830 case ConversionSpecifier::xArg: 831 case ConversionSpecifier::XArg: 832 case ConversionSpecifier::nArg: 833 return true; 834 case ConversionSpecifier::FreeBSDrArg: 835 case ConversionSpecifier::FreeBSDyArg: 836 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS(); 837 default: 838 return false; 839 } 840 841 case LengthModifier::AsShortLong: 842 return LO.OpenCL && !VectorNumElts.isInvalid(); 843 844 // Handle 'l' flag 845 case LengthModifier::AsLong: // or AsWideChar 846 if (CS.isDoubleArg()) { 847 // Invalid for OpenCL FP scalars. 848 if (LO.OpenCL && VectorNumElts.isInvalid()) 849 return false; 850 return true; 851 } 852 853 switch (CS.getKind()) { 854 case ConversionSpecifier::bArg: 855 case ConversionSpecifier::BArg: 856 case ConversionSpecifier::dArg: 857 case ConversionSpecifier::DArg: 858 case ConversionSpecifier::iArg: 859 case ConversionSpecifier::oArg: 860 case ConversionSpecifier::OArg: 861 case ConversionSpecifier::uArg: 862 case ConversionSpecifier::UArg: 863 case ConversionSpecifier::xArg: 864 case ConversionSpecifier::XArg: 865 case ConversionSpecifier::nArg: 866 case ConversionSpecifier::cArg: 867 case ConversionSpecifier::sArg: 868 case ConversionSpecifier::ScanListArg: 869 case ConversionSpecifier::ZArg: 870 return true; 871 case ConversionSpecifier::FreeBSDrArg: 872 case ConversionSpecifier::FreeBSDyArg: 873 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS(); 874 default: 875 return false; 876 } 877 878 case LengthModifier::AsLongDouble: 879 switch (CS.getKind()) { 880 case ConversionSpecifier::aArg: 881 case ConversionSpecifier::AArg: 882 case ConversionSpecifier::fArg: 883 case ConversionSpecifier::FArg: 884 case ConversionSpecifier::eArg: 885 case ConversionSpecifier::EArg: 886 case ConversionSpecifier::gArg: 887 case ConversionSpecifier::GArg: 888 return true; 889 // GNU libc extension. 890 case ConversionSpecifier::dArg: 891 case ConversionSpecifier::iArg: 892 case ConversionSpecifier::oArg: 893 case ConversionSpecifier::uArg: 894 case ConversionSpecifier::xArg: 895 case ConversionSpecifier::XArg: 896 return !Target.getTriple().isOSDarwin() && 897 !Target.getTriple().isOSWindows(); 898 default: 899 return false; 900 } 901 902 case LengthModifier::AsAllocate: 903 switch (CS.getKind()) { 904 case ConversionSpecifier::sArg: 905 case ConversionSpecifier::SArg: 906 case ConversionSpecifier::ScanListArg: 907 return true; 908 default: 909 return false; 910 } 911 912 case LengthModifier::AsMAllocate: 913 switch (CS.getKind()) { 914 case ConversionSpecifier::cArg: 915 case ConversionSpecifier::CArg: 916 case ConversionSpecifier::sArg: 917 case ConversionSpecifier::SArg: 918 case ConversionSpecifier::ScanListArg: 919 return true; 920 default: 921 return false; 922 } 923 case LengthModifier::AsInt32: 924 case LengthModifier::AsInt3264: 925 case LengthModifier::AsInt64: 926 switch (CS.getKind()) { 927 case ConversionSpecifier::dArg: 928 case ConversionSpecifier::iArg: 929 case ConversionSpecifier::oArg: 930 case ConversionSpecifier::uArg: 931 case ConversionSpecifier::xArg: 932 case ConversionSpecifier::XArg: 933 return Target.getTriple().isOSMSVCRT(); 934 default: 935 return false; 936 } 937 case LengthModifier::AsWide: 938 switch (CS.getKind()) { 939 case ConversionSpecifier::cArg: 940 case ConversionSpecifier::CArg: 941 case ConversionSpecifier::sArg: 942 case ConversionSpecifier::SArg: 943 case ConversionSpecifier::ZArg: 944 return Target.getTriple().isOSMSVCRT(); 945 default: 946 return false; 947 } 948 } 949 llvm_unreachable("Invalid LengthModifier Kind!"); 950 } 951 952 bool FormatSpecifier::hasStandardLengthModifier() const { 953 switch (LM.getKind()) { 954 case LengthModifier::None: 955 case LengthModifier::AsChar: 956 case LengthModifier::AsShort: 957 case LengthModifier::AsLong: 958 case LengthModifier::AsLongLong: 959 case LengthModifier::AsIntMax: 960 case LengthModifier::AsSizeT: 961 case LengthModifier::AsPtrDiff: 962 case LengthModifier::AsLongDouble: 963 return true; 964 case LengthModifier::AsAllocate: 965 case LengthModifier::AsMAllocate: 966 case LengthModifier::AsQuad: 967 case LengthModifier::AsInt32: 968 case LengthModifier::AsInt3264: 969 case LengthModifier::AsInt64: 970 case LengthModifier::AsWide: 971 case LengthModifier::AsShortLong: // ??? 972 return false; 973 } 974 llvm_unreachable("Invalid LengthModifier Kind!"); 975 } 976 977 bool FormatSpecifier::hasStandardConversionSpecifier( 978 const LangOptions &LangOpt) const { 979 switch (CS.getKind()) { 980 case ConversionSpecifier::bArg: 981 case ConversionSpecifier::BArg: 982 case ConversionSpecifier::cArg: 983 case ConversionSpecifier::dArg: 984 case ConversionSpecifier::iArg: 985 case ConversionSpecifier::oArg: 986 case ConversionSpecifier::uArg: 987 case ConversionSpecifier::xArg: 988 case ConversionSpecifier::XArg: 989 case ConversionSpecifier::fArg: 990 case ConversionSpecifier::FArg: 991 case ConversionSpecifier::eArg: 992 case ConversionSpecifier::EArg: 993 case ConversionSpecifier::gArg: 994 case ConversionSpecifier::GArg: 995 case ConversionSpecifier::aArg: 996 case ConversionSpecifier::AArg: 997 case ConversionSpecifier::sArg: 998 case ConversionSpecifier::pArg: 999 case ConversionSpecifier::nArg: 1000 case ConversionSpecifier::ObjCObjArg: 1001 case ConversionSpecifier::ScanListArg: 1002 case ConversionSpecifier::PercentArg: 1003 case ConversionSpecifier::PArg: 1004 return true; 1005 case ConversionSpecifier::CArg: 1006 case ConversionSpecifier::SArg: 1007 return LangOpt.ObjC; 1008 case ConversionSpecifier::InvalidSpecifier: 1009 case ConversionSpecifier::FreeBSDbArg: 1010 case ConversionSpecifier::FreeBSDDArg: 1011 case ConversionSpecifier::FreeBSDrArg: 1012 case ConversionSpecifier::FreeBSDyArg: 1013 case ConversionSpecifier::PrintErrno: 1014 case ConversionSpecifier::DArg: 1015 case ConversionSpecifier::OArg: 1016 case ConversionSpecifier::UArg: 1017 case ConversionSpecifier::ZArg: 1018 return false; 1019 } 1020 llvm_unreachable("Invalid ConversionSpecifier Kind!"); 1021 } 1022 1023 bool FormatSpecifier::hasStandardLengthConversionCombination() const { 1024 if (LM.getKind() == LengthModifier::AsLongDouble) { 1025 switch(CS.getKind()) { 1026 case ConversionSpecifier::dArg: 1027 case ConversionSpecifier::iArg: 1028 case ConversionSpecifier::oArg: 1029 case ConversionSpecifier::uArg: 1030 case ConversionSpecifier::xArg: 1031 case ConversionSpecifier::XArg: 1032 return false; 1033 default: 1034 return true; 1035 } 1036 } 1037 return true; 1038 } 1039 1040 std::optional<LengthModifier> 1041 FormatSpecifier::getCorrectedLengthModifier() const { 1042 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) { 1043 if (LM.getKind() == LengthModifier::AsLongDouble || 1044 LM.getKind() == LengthModifier::AsQuad) { 1045 LengthModifier FixedLM(LM); 1046 FixedLM.setKind(LengthModifier::AsLongLong); 1047 return FixedLM; 1048 } 1049 } 1050 1051 return std::nullopt; 1052 } 1053 1054 bool FormatSpecifier::namedTypeToLengthModifier(QualType QT, 1055 LengthModifier &LM) { 1056 for (/**/; const auto *TT = QT->getAs<TypedefType>(); 1057 QT = TT->getDecl()->getUnderlyingType()) { 1058 const TypedefNameDecl *Typedef = TT->getDecl(); 1059 const IdentifierInfo *Identifier = Typedef->getIdentifier(); 1060 if (Identifier->getName() == "size_t") { 1061 LM.setKind(LengthModifier::AsSizeT); 1062 return true; 1063 } else if (Identifier->getName() == "ssize_t") { 1064 // Not C99, but common in Unix. 1065 LM.setKind(LengthModifier::AsSizeT); 1066 return true; 1067 } else if (Identifier->getName() == "intmax_t") { 1068 LM.setKind(LengthModifier::AsIntMax); 1069 return true; 1070 } else if (Identifier->getName() == "uintmax_t") { 1071 LM.setKind(LengthModifier::AsIntMax); 1072 return true; 1073 } else if (Identifier->getName() == "ptrdiff_t") { 1074 LM.setKind(LengthModifier::AsPtrDiff); 1075 return true; 1076 } 1077 } 1078 return false; 1079 } 1080