clang 23.0.0git
FormatString.cpp
Go to the documentation of this file.
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"
17#include "llvm/Support/ConvertUTF.h"
18#include <optional>
19
26using namespace clang;
27
28// Key function to FormatStringHandler.
30
31//===----------------------------------------------------------------------===//
32// Functions for parsing format strings components in both printf and
33// scanf format strings.
34//===----------------------------------------------------------------------===//
35
37clang::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
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
76 const char *Start,
77 const char *&Beg,
78 const char *E,
80 if (*Beg == '*') {
81 const char *I = Beg + 1;
82 const OptionalAmount &Amt = ParseAmount(I, E);
83
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
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
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
121bool
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,
135
136 if (Amt.isInvalid())
137 return true;
138 CS.setFieldWidth(Amt);
139 }
140 return false;
141}
142
143bool
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);
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
180bool
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
210bool
212 const char *&I,
213 const char *E,
214 const LangOptions &LO,
215 bool IsScanf) {
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;
229 } else {
231 }
232 break;
233 case 'l':
234 ++I;
235 if (I != E && *I == 'l') {
236 ++I;
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 == '[')) {
255 break;
256 }
257 --I;
258 }
259 return false;
260 case 'm':
261 if (IsScanf) {
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;
274 break;
275 }
276 if (IsScanf)
277 return false;
278
279 if (I[1] == '3' && I[2] == '2') {
280 I += 3;
282 break;
283 }
284 }
285 ++I;
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
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
325 if (!Ctx.getLangOpts().C99 && !Ctx.getLangOpts().CPlusPlus)
326 return false;
327 for (/**/; const auto *TT = QT->getAs<TypedefType>(); QT = TT->desugar()) {
328 const auto *TD = TT->getDecl();
329 const auto *DC = TT->getDecl()->getDeclContext();
330 if (DC->isTranslationUnit() || DC->isStdNamespace()) {
331 StringRef Name = TD->getIdentifier()->getName();
332 if (Name == "size_t") {
334 return true;
335 } else if (Name == "ssize_t" /*Not C99, but common in Unix.*/) {
337 return true;
338 } else if (Name == "ptrdiff_t") {
340 return true;
341 } else if (Name == "intmax_t") {
343 return true;
344 } else if (Name == "uintmax_t") {
346 return true;
347 }
348 }
349 }
350 if (const auto *PST = QT->getAs<PredefinedSugarType>()) {
351 using Kind = PredefinedSugarType::Kind;
352 switch (PST->getKind()) {
353 case Kind::SizeT:
354 case Kind::SignedSizeT:
356 return true;
357 case Kind::PtrdiffT:
359 return true;
360 }
361 llvm_unreachable("unexpected kind");
362 }
363 return false;
364}
365
366// Check whether T and E are compatible size_t/ptrdiff_t types. E must be
367// consistent with LE.
368// T is the type of the actual expression in the code to be checked, and E is
369// the expected type parsed from the format string.
373
374 if (!T->isIntegerType() || T->isBooleanType())
375 return MatchKind::NoMatch;
376
377 if (C.hasSameType(T, E))
378 return MatchKind::Match;
379
380 if (C.getCorrespondingSignedType(T.getCanonicalType()) !=
381 C.getCorrespondingSignedType(E.getCanonicalType()))
382 return MatchKind::NoMatch;
383
384 return MatchKind::NoMatchSignedness;
385}
386
389 // When using the format attribute in C++, you can receive a function or an
390 // array that will necessarily decay to a pointer when passed to the final
391 // format consumer. Apply decay before type comparison.
392 if (argTy->canDecayToPointerType())
393 argTy = C.getDecayedType(argTy);
394
395 if (Ptr) {
396 // It has to be a pointer.
397 const PointerType *PT = argTy->getAs<PointerType>();
398 if (!PT)
399 return NoMatch;
400
401 // We cannot write through a const qualified pointer.
403 return NoMatch;
404
405 argTy = PT->getPointeeType();
406 }
407
408 if (const auto *OBT = argTy->getAs<OverflowBehaviorType>())
409 argTy = OBT->getUnderlyingType();
410
411 switch (K) {
412 case InvalidTy:
413 llvm_unreachable("ArgType must be valid");
414
415 case UnknownTy:
416 return Match;
417
418 case AnyCharTy: {
419 if (const auto *ED = argTy->getAsEnumDecl()) {
420 // If the enum is incomplete we know nothing about the underlying type.
421 // Assume that it's 'int'. Do not use the underlying type for a scoped
422 // enumeration.
423 if (!ED->isComplete())
424 return NoMatch;
425 if (!ED->isScoped())
426 argTy = ED->getIntegerType();
427 }
428
429 if (const auto *BT = argTy->getAs<BuiltinType>()) {
430 // The types are perfectly matched?
431 switch (BT->getKind()) {
432 default:
433 break;
434 case BuiltinType::Char_S:
435 case BuiltinType::SChar:
436 case BuiltinType::UChar:
437 case BuiltinType::Char_U:
438 return Match;
439 case BuiltinType::Bool:
440 if (!Ptr)
441 return Match;
442 break;
443 }
444 // "Partially matched" because of promotions?
445 if (!Ptr) {
446 switch (BT->getKind()) {
447 default:
448 break;
449 case BuiltinType::Int:
450 case BuiltinType::UInt:
451 return MatchPromotion;
452 case BuiltinType::Short:
453 case BuiltinType::UShort:
454 case BuiltinType::WChar_S:
455 case BuiltinType::WChar_U:
457 }
458 }
459 }
460 return NoMatch;
461 }
462
463 case SpecificTy: {
464 if (TK != TypeKind::DontCare) {
465 return matchesSizeTPtrdiffT(C, argTy, T);
466 }
467
468 if (const auto *ED = argTy->getAsEnumDecl()) {
469 // If the enum is incomplete we know nothing about the underlying type.
470 // Assume that it's 'int'. Do not use the underlying type for a scoped
471 // enumeration as that needs an exact match.
472 if (!ED->isComplete())
473 argTy = C.IntTy;
474 else if (!ED->isScoped())
475 argTy = ED->getIntegerType();
476 }
477
478 if (argTy->isSaturatedFixedPointType())
479 argTy = C.getCorrespondingUnsaturatedType(argTy);
480
481 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
482
483 if (T == argTy)
484 return Match;
485 if (const auto *BT = argTy->getAs<BuiltinType>()) {
486 // Check if the only difference between them is signed vs unsigned
487 // if true, return match signedness.
488 switch (BT->getKind()) {
489 default:
490 break;
491 case BuiltinType::Bool:
492 if (Ptr && (T == C.UnsignedCharTy || T == C.SignedCharTy))
493 return NoMatch;
494 [[fallthrough]];
495 case BuiltinType::Char_S:
496 case BuiltinType::SChar:
497 if (T == C.UnsignedShortTy || T == C.ShortTy)
499 if (T == C.UnsignedCharTy)
500 return NoMatchSignedness;
501 if (T == C.SignedCharTy)
502 return Match;
503 break;
504 case BuiltinType::Char_U:
505 case BuiltinType::UChar:
506 if (T == C.UnsignedShortTy || T == C.ShortTy)
508 if (T == C.UnsignedCharTy)
509 return Match;
510 if (T == C.SignedCharTy)
511 return NoMatchSignedness;
512 break;
513 case BuiltinType::Short:
514 if (T == C.UnsignedShortTy)
515 return NoMatchSignedness;
516 break;
517 case BuiltinType::UShort:
518 if (T == C.ShortTy)
519 return NoMatchSignedness;
520 break;
521 case BuiltinType::Int:
522 if (T == C.UnsignedIntTy)
523 return NoMatchSignedness;
524 break;
525 case BuiltinType::UInt:
526 if (T == C.IntTy)
527 return NoMatchSignedness;
528 break;
529 case BuiltinType::Long:
530 if (T == C.UnsignedLongTy)
531 return NoMatchSignedness;
532 break;
533 case BuiltinType::ULong:
534 if (T == C.LongTy)
535 return NoMatchSignedness;
536 break;
537 case BuiltinType::LongLong:
538 if (T == C.UnsignedLongLongTy)
539 return NoMatchSignedness;
540 break;
541 case BuiltinType::ULongLong:
542 if (T == C.LongLongTy)
543 return NoMatchSignedness;
544 break;
545 }
546 // "Partially matched" because of promotions?
547 if (!Ptr) {
548 switch (BT->getKind()) {
549 default:
550 break;
551 case BuiltinType::Bool:
552 if (T == C.IntTy || T == C.UnsignedIntTy)
553 return MatchPromotion;
554 break;
555 case BuiltinType::Int:
556 case BuiltinType::UInt:
557 if (T == C.SignedCharTy || T == C.UnsignedCharTy ||
558 T == C.ShortTy || T == C.UnsignedShortTy || T == C.WCharTy ||
559 T == C.WideCharTy)
560 return MatchPromotion;
561 break;
562 case BuiltinType::Char_U:
563 if (T == C.UnsignedIntTy)
564 return MatchPromotion;
565 if (T == C.UnsignedShortTy)
567 break;
568 case BuiltinType::Char_S:
569 if (T == C.IntTy)
570 return MatchPromotion;
571 if (T == C.ShortTy)
573 break;
574 case BuiltinType::Half:
575 case BuiltinType::Float:
576 if (T == C.DoubleTy)
577 return MatchPromotion;
578 break;
579 case BuiltinType::Short:
580 case BuiltinType::UShort:
581 if (T == C.SignedCharTy || T == C.UnsignedCharTy)
583 break;
584 case BuiltinType::WChar_U:
585 case BuiltinType::WChar_S:
586 if (T != C.WCharTy && T != C.WideCharTy)
588 }
589 }
590 }
591 return NoMatch;
592 }
593
594 case CStrTy:
595 if (const auto *PT = argTy->getAs<PointerType>();
596 PT && PT->getPointeeType()->isCharType())
597 return Match;
598 return NoMatch;
599
600 case WCStrTy:
601 if (const auto *PT = argTy->getAs<PointerType>();
602 PT &&
603 C.hasSameUnqualifiedType(PT->getPointeeType(), C.getWideCharType()))
604 return Match;
605 return NoMatch;
606
607 case WIntTy: {
608 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
609
610 if (C.getCanonicalType(argTy).getUnqualifiedType() == WInt)
611 return Match;
612
613 QualType PromoArg = C.isPromotableIntegerType(argTy)
614 ? C.getPromotedIntegerType(argTy)
615 : argTy;
616 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
617
618 // If the promoted argument is the corresponding signed type of the
619 // wint_t type, then it should match.
620 if (PromoArg->hasSignedIntegerRepresentation() &&
621 C.getCorrespondingUnsignedType(PromoArg) == WInt)
622 return Match;
623
624 return WInt == PromoArg ? Match : NoMatch;
625 }
626
627 case CPointerTy:
628 if (const auto *PT = argTy->getAs<PointerType>()) {
629 QualType PointeeTy = PT->getPointeeType();
630 if (PointeeTy->isVoidType() || (!Ptr && PointeeTy->isCharType()))
631 return Match;
632 return NoMatchPedantic;
633 }
634
635 // nullptr_t* is not a double pointer, so reject when something like
636 // void** is expected.
637 // In C++, nullptr is promoted to void*. In C23, va_arg(ap, void*) is not
638 // undefined when the next argument is of type nullptr_t.
639 if (!Ptr && argTy->isNullPtrType())
640 return C.getLangOpts().CPlusPlus ? MatchPromotion : Match;
641
642 if (argTy->isObjCObjectPointerType() || argTy->isBlockPointerType())
643 return NoMatchPedantic;
644
645 return NoMatch;
646
647 case ObjCPointerTy: {
648 if (argTy->getAs<ObjCObjectPointerType>() ||
649 argTy->getAs<BlockPointerType>())
650 return Match;
651
652 // Handle implicit toll-free bridging.
653 if (const PointerType *PT = argTy->getAs<PointerType>()) {
654 // Things such as CFTypeRef are really just opaque pointers
655 // to C structs representing CF types that can often be bridged
656 // to Objective-C objects. Since the compiler doesn't know which
657 // structs can be toll-free bridged, we just accept them all.
658 QualType pointee = PT->getPointeeType();
659 if (pointee->isStructureType() || pointee->isVoidType())
660 return Match;
661 }
662 return NoMatch;
663 }
664 }
665
666 llvm_unreachable("Invalid ArgType Kind!");
667}
668
672
673 uint64_t IntSize = C.getTypeSize(C.IntTy);
674 uint64_t ASize = C.getTypeSize(A);
675 uint64_t BSize = C.getTypeSize(B);
676 if (std::max(ASize, IntSize) != std::max(BSize, IntSize))
677 return MK::NoMatch;
678 if (CheckSign && A->isSignedIntegerType() != B->isSignedIntegerType())
679 return MK::NoMatchSignedness;
680 if (ASize != BSize)
681 return MK::MatchPromotion;
682 return MK::Match;
683}
684
688
689 // Per matchesType.
690 if (K == AK::InvalidTy || Other.K == AK::InvalidTy)
691 return NoMatch;
692 if (K == AK::UnknownTy || Other.K == AK::UnknownTy)
693 return Match;
694
695 // Handle whether either (or both, or neither) sides has Ptr set,
696 // in addition to whether either (or both, or neither) sides is a SpecificTy
697 // that is a pointer.
698 ArgType Left = *this;
699 bool LeftWasPointer = false;
700 ArgType Right = Other;
701 bool RightWasPointer = false;
702 if (Left.Ptr) {
703 Left.Ptr = false;
704 LeftWasPointer = true;
705 } else if (Left.K == AK::SpecificTy && Left.T->isPointerType()) {
706 Left.T = Left.T->getPointeeType();
707 LeftWasPointer = true;
708 }
709 if (Right.Ptr) {
710 Right.Ptr = false;
711 RightWasPointer = true;
712 } else if (Right.K == AK::SpecificTy && Right.T->isPointerType()) {
713 Right.T = Right.T->getPointeeType();
714 RightWasPointer = true;
715 }
716
717 if (LeftWasPointer != RightWasPointer)
718 return NoMatch;
719
720 // Ensure that if at least one side is a SpecificTy, then Left is a
721 // SpecificTy.
722 if (Right.K == AK::SpecificTy)
723 std::swap(Left, Right);
724
725 if (Left.K == AK::SpecificTy) {
726 if (Right.K == AK::SpecificTy) {
727 if (Left.TK != TypeKind::DontCare) {
728 return matchesSizeTPtrdiffT(C, Right.T, Left.T);
729 } else if (Right.TK != TypeKind::DontCare) {
730 return matchesSizeTPtrdiffT(C, Left.T, Right.T);
731 }
732
733 auto Canon1 = C.getCanonicalType(Left.T);
734 auto Canon2 = C.getCanonicalType(Right.T);
735 if (Canon1 == Canon2)
736 return Match;
737
738 auto *BT1 = QualType(Canon1)->getAs<BuiltinType>();
739 auto *BT2 = QualType(Canon2)->getAs<BuiltinType>();
740 if (BT1 == nullptr || BT2 == nullptr)
741 return NoMatch;
742 if (BT1 == BT2)
743 return Match;
744
745 if (!LeftWasPointer && BT1->isInteger() && BT2->isInteger())
746 return integerTypeMatch(C, Canon1, Canon2, true);
747 return NoMatch;
748 } else if (Right.K == AK::AnyCharTy) {
749 if (!LeftWasPointer && Left.T->isIntegerType())
750 return integerTypeMatch(C, Left.T, C.CharTy, false);
751 return NoMatch;
752 } else if (Right.K == AK::WIntTy) {
753 if (!LeftWasPointer && Left.T->isIntegerType())
754 return integerTypeMatch(C, Left.T, C.WIntTy, true);
755 return NoMatch;
756 }
757 // It's hypothetically possible to create an AK::SpecificTy ArgType
758 // that matches another kind of ArgType, but in practice Clang doesn't
759 // do that, so ignore that case.
760 return NoMatch;
761 }
762
763 return Left.K == Right.K ? Match : NoMatch;
764}
765
766ArgType ArgType::makeVectorType(ASTContext &C, unsigned NumElts) const {
767 // Check for valid vector element types.
768 if (T.isNull())
769 return ArgType::Invalid();
770
771 QualType Vec = C.getExtVectorType(T, NumElts);
772 return ArgType(Vec, Name);
773}
774
776 QualType Res;
777 switch (K) {
778 case InvalidTy:
779 llvm_unreachable("No representative type for Invalid ArgType");
780 case UnknownTy:
781 llvm_unreachable("No representative type for Unknown ArgType");
782 case AnyCharTy:
783 Res = C.CharTy;
784 break;
785 case SpecificTy:
786 if (TK == TypeKind::PtrdiffT || TK == TypeKind::SizeT)
787 // Using Name as name, so no need to show the uglified name.
788 Res = T->getCanonicalTypeInternal();
789 else
790 Res = T;
791 break;
792 case CStrTy:
793 Res = C.getPointerType(C.CharTy);
794 break;
795 case WCStrTy:
796 Res = C.getPointerType(C.getWideCharType());
797 break;
798 case ObjCPointerTy:
799 Res = C.ObjCBuiltinIdTy;
800 break;
801 case CPointerTy:
802 Res = C.VoidPtrTy;
803 break;
804 case WIntTy: {
805 Res = C.getWIntType();
806 break;
807 }
808 }
809
810 if (Ptr)
811 Res = C.getPointerType(Res);
812 return Res;
813}
814
816 std::string S = getRepresentativeType(C).getAsString(C.getPrintingPolicy());
817 std::string Alias;
818 if (Name) {
819 // Use a specific name for this type, e.g. "size_t".
820 Alias = Name;
821 if (Ptr) {
822 // If ArgType is actually a pointer to T, append an asterisk.
823 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
824 }
825 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
826 if (S == Alias)
827 Alias.clear();
828 }
829
830 if (!Alias.empty())
831 return std::string("'") + Alias + "' (aka '" + S + "')";
832 return std::string("'") + S + "'";
833}
834
835
836//===----------------------------------------------------------------------===//
837// Methods on OptionalAmount.
838//===----------------------------------------------------------------------===//
839
844
845//===----------------------------------------------------------------------===//
846// Methods on LengthModifier.
847//===----------------------------------------------------------------------===//
848
849const char *
851 switch (kind) {
852 case AsChar:
853 return "hh";
854 case AsShort:
855 return "h";
856 case AsShortLong:
857 return "hl";
858 case AsLong: // or AsWideChar
859 return "l";
860 case AsLongLong:
861 return "ll";
862 case AsQuad:
863 return "q";
864 case AsIntMax:
865 return "j";
866 case AsSizeT:
867 return "z";
868 case AsPtrDiff:
869 return "t";
870 case AsInt32:
871 return "I32";
872 case AsInt3264:
873 return "I";
874 case AsInt64:
875 return "I64";
876 case AsLongDouble:
877 return "L";
878 case AsAllocate:
879 return "a";
880 case AsMAllocate:
881 return "m";
882 case AsWide:
883 return "w";
884 case None:
885 return "";
886 }
887 return nullptr;
888}
889
890//===----------------------------------------------------------------------===//
891// Methods on ConversionSpecifier.
892//===----------------------------------------------------------------------===//
893
894const char *ConversionSpecifier::toString() const {
895 switch (kind) {
896 case bArg: return "b";
897 case BArg: return "B";
898 case dArg: return "d";
899 case DArg: return "D";
900 case iArg: return "i";
901 case oArg: return "o";
902 case OArg: return "O";
903 case uArg: return "u";
904 case UArg: return "U";
905 case xArg: return "x";
906 case XArg: return "X";
907 case fArg: return "f";
908 case FArg: return "F";
909 case eArg: return "e";
910 case EArg: return "E";
911 case gArg: return "g";
912 case GArg: return "G";
913 case aArg: return "a";
914 case AArg: return "A";
915 case cArg: return "c";
916 case sArg: return "s";
917 case pArg: return "p";
918 case PArg:
919 return "P";
920 case nArg: return "n";
921 case PercentArg: return "%";
922 case ScanListArg: return "[";
923 case InvalidSpecifier: return nullptr;
924
925 // POSIX unicode extensions.
926 case CArg: return "C";
927 case SArg: return "S";
928
929 // Objective-C specific specifiers.
930 case ObjCObjArg: return "@";
931
932 // FreeBSD kernel specific specifiers.
933 case FreeBSDbArg: return "b";
934 case FreeBSDDArg: return "D";
935 case FreeBSDrArg: return "r";
936 case FreeBSDyArg: return "y";
937
938 // GlibC specific specifiers.
939 case PrintErrno: return "m";
940
941 // MS specific specifiers.
942 case ZArg: return "Z";
943
944 // ISO/IEC TR 18037 (fixed-point) specific specifiers.
945 case rArg:
946 return "r";
947 case RArg:
948 return "R";
949 case kArg:
950 return "k";
951 case KArg:
952 return "K";
953 }
954 return nullptr;
955}
956
957std::optional<ConversionSpecifier>
960
961 switch (getKind()) {
962 default:
963 return std::nullopt;
964 case DArg:
965 NewKind = dArg;
966 break;
967 case UArg:
968 NewKind = uArg;
969 break;
970 case OArg:
971 NewKind = oArg;
972 break;
973 }
974
975 ConversionSpecifier FixedCS(*this);
976 FixedCS.setKind(NewKind);
977 return FixedCS;
978}
979
980//===----------------------------------------------------------------------===//
981// Methods on OptionalAmount.
982//===----------------------------------------------------------------------===//
983
984void OptionalAmount::toString(raw_ostream &os) const {
985 switch (hs) {
986 case Invalid:
987 case NotSpecified:
988 return;
989 case Arg:
990 if (UsesDotPrefix)
991 os << ".";
992 if (usesPositionalArg())
993 os << "*" << getPositionalArgIndex() << "$";
994 else
995 os << "*";
996 break;
997 case Constant:
998 if (UsesDotPrefix)
999 os << ".";
1000 os << amt;
1001 break;
1002 }
1003}
1004
1006 const LangOptions &LO) const {
1007 switch (LM.getKind()) {
1009 return true;
1010
1011 // Handle most integer flags
1013 // Length modifier only applies to FP vectors.
1014 if (LO.OpenCL && CS.isDoubleArg())
1015 return !VectorNumElts.isInvalid();
1016
1017 if (CS.isFixedPointArg())
1018 return true;
1019
1020 if (Target.getTriple().isOSMSVCRT()) {
1021 switch (CS.getKind()) {
1027 return true;
1028 default:
1029 break;
1030 }
1031 }
1032 [[fallthrough]];
1039 switch (CS.getKind()) {
1052 return true;
1055 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS();
1056 default:
1057 return false;
1058 }
1059
1061 return LO.OpenCL && !VectorNumElts.isInvalid();
1062
1063 // Handle 'l' flag
1064 case LengthModifier::AsLong: // or AsWideChar
1065 if (CS.isDoubleArg()) {
1066 // Invalid for OpenCL FP scalars.
1067 if (LO.OpenCL && VectorNumElts.isInvalid())
1068 return false;
1069 return true;
1070 }
1071
1072 if (CS.isFixedPointArg())
1073 return true;
1074
1075 switch (CS.getKind()) {
1092 return true;
1095 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS();
1096 default:
1097 return false;
1098 }
1099
1101 switch (CS.getKind()) {
1110 return true;
1111 // GNU libc extension.
1118 return !Target.getTriple().isOSDarwin() &&
1119 !Target.getTriple().isOSWindows();
1120 default:
1121 return false;
1122 }
1123
1125 switch (CS.getKind()) {
1129 return true;
1130 default:
1131 return false;
1132 }
1133
1135 switch (CS.getKind()) {
1141 return true;
1142 default:
1143 return false;
1144 }
1148 switch (CS.getKind()) {
1155 return Target.getTriple().isOSMSVCRT();
1156 default:
1157 return false;
1158 }
1160 switch (CS.getKind()) {
1166 return Target.getTriple().isOSMSVCRT();
1167 default:
1168 return false;
1169 }
1170 }
1171 llvm_unreachable("Invalid LengthModifier Kind!");
1172}
1173
1175 switch (LM.getKind()) {
1185 return true;
1193 case LengthModifier::AsShortLong: // ???
1194 return false;
1195 }
1196 llvm_unreachable("Invalid LengthModifier Kind!");
1197}
1198
1200 const LangOptions &LangOpt) const {
1201 switch (CS.getKind()) {
1226 return true;
1229 return LangOpt.ObjC;
1240 return false;
1245 return LangOpt.FixedPoint;
1246 }
1247 llvm_unreachable("Invalid ConversionSpecifier Kind!");
1248}
1249
1251 if (LM.getKind() == LengthModifier::AsLongDouble) {
1252 switch(CS.getKind()) {
1259 return false;
1260 default:
1261 return true;
1262 }
1263 }
1264 return true;
1265}
1266
1267std::optional<LengthModifier>
1269 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
1270 if (LM.getKind() == LengthModifier::AsLongDouble ||
1271 LM.getKind() == LengthModifier::AsQuad) {
1272 LengthModifier FixedLM(LM);
1274 return FixedLM;
1275 }
1276 }
1277
1278 return std::nullopt;
1279}
1280
1282 LengthModifier &LM) {
1284 namedTypeToLengthModifierKind(Ctx, QT, Out)) {
1285 LM.setKind(Out);
1286 return true;
1287 }
1288 return false;
1289}
static clang::analyze_format_string::ArgType::MatchKind matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E)
static analyze_format_string::ArgType::MatchKind integerTypeMatch(ASTContext &C, QualType A, QualType B, bool CheckSign)
static bool namedTypeToLengthModifierKind(ASTContext &Ctx, QualType QT, LengthModifier::Kind &K)
Defines the clang::LangOptions interface.
__device__ __2f16 float c
virtual ~FormatStringHandler()
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
const LangOptions & getLangOpts() const
Definition ASTContext.h:951
CanQualType IntTy
Pointer to a block type.
Definition TypeBase.h:3550
This class is used for builtin types like 'int'.
Definition TypeBase.h:3172
bool isInteger() const
Definition TypeBase.h:3233
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Represents a pointer to an Objective C object.
Definition TypeBase.h:8006
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3336
QualType getPointeeType() const
Definition TypeBase.h:3346
PredefinedSugarKind Kind
Definition TypeBase.h:8299
A (possibly-)qualified type.
Definition TypeBase.h:937
QualType getCanonicalType() const
Definition TypeBase.h:8440
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8461
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition TypeBase.h:1338
Exposes information about the current target.
Definition TargetInfo.h:226
bool isStructureType() const
Definition Type.cpp:679
bool isBlockPointerType() const
Definition TypeBase.h:8645
bool isVoidType() const
Definition TypeBase.h:8991
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2230
bool canDecayToPointerType() const
Determines whether this type can decay to a pointer type.
Definition TypeBase.h:9158
bool isCharType() const
Definition Type.cpp:2157
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:753
bool isSaturatedFixedPointType() const
Return true if this is a saturated fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
Definition TypeBase.h:9067
bool hasSignedIntegerRepresentation() const
Determine whether this type has an signed integer representation of some sort, e.g....
Definition Type.cpp:2274
QualType getCanonicalTypeInternal() const
Definition TypeBase.h:3127
EnumDecl * getAsEnumDecl() const
Retrieves the EnumDecl this type refers to.
Definition Type.h:53
bool isObjCObjectPointerType() const
Definition TypeBase.h:8804
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9218
bool isNullPtrType() const
Definition TypeBase.h:9028
MatchKind
How well a given conversion specifier matches its argument.
@ NoMatch
The conversion specifier and the argument types are incompatible.
@ NoMatchPedantic
The conversion specifier and the argument type are disallowed by the C standard, but are in practice ...
@ Match
The conversion specifier and the argument type are compatible.
@ MatchPromotion
The conversion specifier and the argument type are compatible because of default argument promotions.
@ NoMatchSignedness
The conversion specifier and the argument type have different sign.
@ NoMatchTypeConfusion
The conversion specifier and the argument type are compatible, but still seems likely to be an error.
@ NoMatchPromotionTypeConfusion
The conversion specifier and the argument type are compatible but still seems likely to be an error.
ArgType makeVectorType(ASTContext &C, unsigned NumElts) const
MatchKind matchesArgType(ASTContext &C, const ArgType &other) const
QualType getRepresentativeType(ASTContext &C) const
std::string getRepresentativeTypeName(ASTContext &C) const
ArgType(Kind K=UnknownTy, const char *N=nullptr)
MatchKind matchesType(ASTContext &C, QualType argTy) const
std::optional< ConversionSpecifier > getStandardSpecifier() const
static bool namedTypeToLengthModifier(ASTContext &Ctx, QualType QT, LengthModifier &LM)
For a TypedefType QT, if it is a named integer type such as size_t, assign the appropriate value to L...
void setFieldWidth(const OptionalAmount &Amt)
bool hasStandardConversionSpecifier(const LangOptions &LangOpt) const
bool hasValidLengthModifier(const TargetInfo &Target, const LangOptions &LO) const
std::optional< LengthModifier > getCorrectedLengthModifier() const
void setVectorNumElts(const OptionalAmount &Amt)
virtual void HandlePosition(const char *startPos, unsigned posLen)
virtual void HandleInvalidPosition(const char *startPos, unsigned posLen, PositionContext p)
virtual void HandleZeroPosition(const char *startPos, unsigned posLen)
virtual void HandleIncompleteSpecifier(const char *startSpecifier, unsigned specifierLen)
Represents the length modifier in a format string in scanf/printf.
ArgType getArgType(ASTContext &Ctx) const
Defines the clang::TargetInfo interface.
bool ParseFieldWidth(FormatStringHandler &H, FormatSpecifier &CS, const char *Start, const char *&Beg, const char *E, unsigned *argIndex)
OptionalAmount ParsePositionAmount(FormatStringHandler &H, const char *Start, const char *&Beg, const char *E, PositionContext p)
OptionalAmount ParseAmount(const char *&Beg, const char *E)
OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E, unsigned &argIndex)
bool ParseLengthModifier(FormatSpecifier &FS, const char *&Beg, const char *E, const LangOptions &LO, bool IsScanf=false)
Returns true if a LengthModifier was parsed and installed in the FormatSpecifier& argument,...
bool ParseArgPosition(FormatStringHandler &H, FormatSpecifier &CS, const char *Start, const char *&Beg, const char *E)
bool ParseVectorModifier(FormatStringHandler &H, FormatSpecifier &FS, const char *&Beg, const char *E, const LangOptions &LO)
bool ParseUTF8InvalidSpecifier(const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len)
Returns true if the invalid specifier in SpecifierBegin is a UTF-8 string; check that it won't go fur...
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
Definition Decl.h:1746