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