]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/Analysis/FormatString.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / clang / lib / Analysis / FormatString.cpp
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
19 using clang::analyze_format_string::ArgType;
20 using clang::analyze_format_string::FormatStringHandler;
21 using clang::analyze_format_string::FormatSpecifier;
22 using clang::analyze_format_string::LengthModifier;
23 using clang::analyze_format_string::OptionalAmount;
24 using clang::analyze_format_string::PositionContext;
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::ParseLengthModifier(FormatSpecifier &FS,
182                                                   const char *&I,
183                                                   const char *E,
184                                                   const LangOptions &LO,
185                                                   bool IsScanf) {
186   LengthModifier::Kind lmKind = LengthModifier::None;
187   const char *lmPosition = I;
188   switch (*I) {
189     default:
190       return false;
191     case 'h':
192       ++I;
193       lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
194                                      : LengthModifier::AsShort;
195       break;
196     case 'l':
197       ++I;
198       lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
199                                      : LengthModifier::AsLong;
200       break;
201     case 'j': lmKind = LengthModifier::AsIntMax;     ++I; break;
202     case 'z': lmKind = LengthModifier::AsSizeT;      ++I; break;
203     case 't': lmKind = LengthModifier::AsPtrDiff;    ++I; break;
204     case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
205     case 'q': lmKind = LengthModifier::AsQuad;       ++I; break;
206     case 'a':
207       if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
208         // For scanf in C90, look at the next character to see if this should
209         // be parsed as the GNU extension 'a' length modifier. If not, this
210         // will be parsed as a conversion specifier.
211         ++I;
212         if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
213           lmKind = LengthModifier::AsAllocate;
214           break;
215         }
216         --I;
217       }
218       return false;
219     case 'm':
220       if (IsScanf) {
221         lmKind = LengthModifier::AsMAllocate;
222         ++I;
223         break;
224       }
225       return false;
226     // printf: AsInt64, AsInt32, AsInt3264
227     // scanf:  AsInt64
228     case 'I':
229       if (I + 1 != E && I + 2 != E) {
230         if (I[1] == '6' && I[2] == '4') {
231           I += 3;
232           lmKind = LengthModifier::AsInt64;
233           break;
234         }
235         if (IsScanf)
236           return false;
237
238         if (I[1] == '3' && I[2] == '2') {
239           I += 3;
240           lmKind = LengthModifier::AsInt32;
241           break;
242         }
243       }
244       ++I;
245       lmKind = LengthModifier::AsInt3264;
246       break;
247   }
248   LengthModifier lm(lmPosition, lmKind);
249   FS.setLengthModifier(lm);
250   return true;
251 }
252
253 //===----------------------------------------------------------------------===//
254 // Methods on ArgType.
255 //===----------------------------------------------------------------------===//
256
257 bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
258   if (Ptr) {
259     // It has to be a pointer.
260     const PointerType *PT = argTy->getAs<PointerType>();
261     if (!PT)
262       return false;
263
264     // We cannot write through a const qualified pointer.
265     if (PT->getPointeeType().isConstQualified())
266       return false;
267
268     argTy = PT->getPointeeType();
269   }
270
271   switch (K) {
272     case InvalidTy:
273       llvm_unreachable("ArgType must be valid");
274
275     case UnknownTy:
276       return true;
277       
278     case AnyCharTy: {
279       if (const EnumType *ETy = argTy->getAs<EnumType>())
280         argTy = ETy->getDecl()->getIntegerType();
281
282       if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
283         switch (BT->getKind()) {
284           default:
285             break;
286           case BuiltinType::Char_S:
287           case BuiltinType::SChar:
288           case BuiltinType::UChar:
289           case BuiltinType::Char_U:
290             return true;            
291         }
292       return false;
293     }
294       
295     case SpecificTy: {
296       if (const EnumType *ETy = argTy->getAs<EnumType>())
297         argTy = ETy->getDecl()->getIntegerType();
298       argTy = C.getCanonicalType(argTy).getUnqualifiedType();
299
300       if (T == argTy)
301         return true;
302       // Check for "compatible types".
303       if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
304         switch (BT->getKind()) {
305           default:
306             break;
307           case BuiltinType::Char_S:
308           case BuiltinType::SChar:
309           case BuiltinType::Char_U:
310           case BuiltinType::UChar:                    
311             return T == C.UnsignedCharTy || T == C.SignedCharTy;
312           case BuiltinType::Short:
313             return T == C.UnsignedShortTy;
314           case BuiltinType::UShort:
315             return T == C.ShortTy;
316           case BuiltinType::Int:
317             return T == C.UnsignedIntTy;
318           case BuiltinType::UInt:
319             return T == C.IntTy;
320           case BuiltinType::Long:
321             return T == C.UnsignedLongTy;
322           case BuiltinType::ULong:
323             return T == C.LongTy;
324           case BuiltinType::LongLong:
325             return T == C.UnsignedLongLongTy;
326           case BuiltinType::ULongLong:
327             return T == C.LongLongTy;
328         }
329       return false;
330     }
331
332     case CStrTy: {
333       const PointerType *PT = argTy->getAs<PointerType>();
334       if (!PT)
335         return false;
336       QualType pointeeTy = PT->getPointeeType();
337       if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
338         switch (BT->getKind()) {
339           case BuiltinType::Void:
340           case BuiltinType::Char_U:
341           case BuiltinType::UChar:
342           case BuiltinType::Char_S:
343           case BuiltinType::SChar:
344             return true;
345           default:
346             break;
347         }
348
349       return false;
350     }
351
352     case WCStrTy: {
353       const PointerType *PT = argTy->getAs<PointerType>();
354       if (!PT)
355         return false;
356       QualType pointeeTy =
357         C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
358       return pointeeTy == C.getWideCharType();
359     }
360     
361     case WIntTy: {
362       
363       QualType PromoArg = 
364         argTy->isPromotableIntegerType()
365           ? C.getPromotedIntegerType(argTy) : argTy;
366       
367       QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
368       PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
369       
370       // If the promoted argument is the corresponding signed type of the
371       // wint_t type, then it should match.
372       if (PromoArg->hasSignedIntegerRepresentation() &&
373           C.getCorrespondingUnsignedType(PromoArg) == WInt)
374         return true;
375
376       return WInt == PromoArg;
377     }
378
379     case CPointerTy:
380       return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
381              argTy->isBlockPointerType() || argTy->isNullPtrType();
382
383     case ObjCPointerTy: {
384       if (argTy->getAs<ObjCObjectPointerType>() ||
385           argTy->getAs<BlockPointerType>())
386         return true;
387       
388       // Handle implicit toll-free bridging.
389       if (const PointerType *PT = argTy->getAs<PointerType>()) {
390         // Things such as CFTypeRef are really just opaque pointers
391         // to C structs representing CF types that can often be bridged
392         // to Objective-C objects.  Since the compiler doesn't know which
393         // structs can be toll-free bridged, we just accept them all.
394         QualType pointee = PT->getPointeeType();
395         if (pointee->getAsStructureType() || pointee->isVoidType())
396           return true;
397       }
398       return false;      
399     }
400   }
401
402   llvm_unreachable("Invalid ArgType Kind!");
403 }
404
405 QualType ArgType::getRepresentativeType(ASTContext &C) const {
406   QualType Res;
407   switch (K) {
408     case InvalidTy:
409       llvm_unreachable("No representative type for Invalid ArgType");
410     case UnknownTy:
411       llvm_unreachable("No representative type for Unknown ArgType");
412     case AnyCharTy:
413       Res = C.CharTy;
414       break;
415     case SpecificTy:
416       Res = T;
417       break;
418     case CStrTy:
419       Res = C.getPointerType(C.CharTy);
420       break;
421     case WCStrTy:
422       Res = C.getPointerType(C.getWideCharType());
423       break;
424     case ObjCPointerTy:
425       Res = C.ObjCBuiltinIdTy;
426       break;
427     case CPointerTy:
428       Res = C.VoidPtrTy;
429       break;
430     case WIntTy: {
431       Res = C.getWIntType();
432       break;
433     }
434   }
435
436   if (Ptr)
437     Res = C.getPointerType(Res);
438   return Res;
439 }
440
441 std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
442   std::string S = getRepresentativeType(C).getAsString();
443
444   std::string Alias;
445   if (Name) {
446     // Use a specific name for this type, e.g. "size_t".
447     Alias = Name;
448     if (Ptr) {
449       // If ArgType is actually a pointer to T, append an asterisk.
450       Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
451     }
452     // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
453     if (S == Alias)
454       Alias.clear();
455   }
456
457   if (!Alias.empty())
458     return std::string("'") + Alias + "' (aka '" + S + "')";
459   return std::string("'") + S + "'";
460 }
461
462
463 //===----------------------------------------------------------------------===//
464 // Methods on OptionalAmount.
465 //===----------------------------------------------------------------------===//
466
467 ArgType
468 analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
469   return Ctx.IntTy;
470 }
471
472 //===----------------------------------------------------------------------===//
473 // Methods on LengthModifier.
474 //===----------------------------------------------------------------------===//
475
476 const char *
477 analyze_format_string::LengthModifier::toString() const {
478   switch (kind) {
479   case AsChar:
480     return "hh";
481   case AsShort:
482     return "h";
483   case AsLong: // or AsWideChar
484     return "l";
485   case AsLongLong:
486     return "ll";
487   case AsQuad:
488     return "q";
489   case AsIntMax:
490     return "j";
491   case AsSizeT:
492     return "z";
493   case AsPtrDiff:
494     return "t";
495   case AsInt32:
496     return "I32";
497   case AsInt3264:
498     return "I";
499   case AsInt64:
500     return "I64";
501   case AsLongDouble:
502     return "L";
503   case AsAllocate:
504     return "a";
505   case AsMAllocate:
506     return "m";
507   case None:
508     return "";
509   }
510   return NULL;
511 }
512
513 //===----------------------------------------------------------------------===//
514 // Methods on ConversionSpecifier.
515 //===----------------------------------------------------------------------===//
516
517 const char *ConversionSpecifier::toString() const {
518   switch (kind) {
519   case dArg: return "d";
520   case DArg: return "D";
521   case iArg: return "i";
522   case oArg: return "o";
523   case OArg: return "O";
524   case uArg: return "u";
525   case UArg: return "U";
526   case xArg: return "x";
527   case XArg: return "X";
528   case fArg: return "f";
529   case FArg: return "F";
530   case eArg: return "e";
531   case EArg: return "E";
532   case gArg: return "g";
533   case GArg: return "G";
534   case aArg: return "a";
535   case AArg: return "A";
536   case cArg: return "c";
537   case sArg: return "s";
538   case pArg: return "p";
539   case nArg: return "n";
540   case PercentArg:  return "%";
541   case ScanListArg: return "[";
542   case InvalidSpecifier: return NULL;
543
544   // POSIX unicode extensions.
545   case CArg: return "C";
546   case SArg: return "S";
547
548   // Objective-C specific specifiers.
549   case ObjCObjArg: return "@";
550
551   // FreeBSD specific specifiers.
552   case FreeBSDbArg: return "b";
553   case FreeBSDDArg: return "D";
554   case FreeBSDrArg: return "r";
555
556   // GlibC specific specifiers.
557   case PrintErrno: return "m";
558   }
559   return NULL;
560 }
561
562 Optional<ConversionSpecifier>
563 ConversionSpecifier::getStandardSpecifier() const {
564   ConversionSpecifier::Kind NewKind;
565   
566   switch (getKind()) {
567   default:
568     return None;
569   case DArg:
570     NewKind = dArg;
571     break;
572   case UArg:
573     NewKind = uArg;
574     break;
575   case OArg:
576     NewKind = oArg;
577     break;
578   }
579
580   ConversionSpecifier FixedCS(*this);
581   FixedCS.setKind(NewKind);
582   return FixedCS;
583 }
584
585 //===----------------------------------------------------------------------===//
586 // Methods on OptionalAmount.
587 //===----------------------------------------------------------------------===//
588
589 void OptionalAmount::toString(raw_ostream &os) const {
590   switch (hs) {
591   case Invalid:
592   case NotSpecified:
593     return;
594   case Arg:
595     if (UsesDotPrefix)
596         os << ".";
597     if (usesPositionalArg())
598       os << "*" << getPositionalArgIndex() << "$";
599     else
600       os << "*";
601     break;
602   case Constant:
603     if (UsesDotPrefix)
604         os << ".";
605     os << amt;
606     break;
607   }
608 }
609
610 bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
611   switch (LM.getKind()) {
612     case LengthModifier::None:
613       return true;
614       
615     // Handle most integer flags
616     case LengthModifier::AsChar:
617     case LengthModifier::AsShort:
618     case LengthModifier::AsLongLong:
619     case LengthModifier::AsQuad:
620     case LengthModifier::AsIntMax:
621     case LengthModifier::AsSizeT:
622     case LengthModifier::AsPtrDiff:
623       switch (CS.getKind()) {
624         case ConversionSpecifier::dArg:
625         case ConversionSpecifier::DArg:
626         case ConversionSpecifier::iArg:
627         case ConversionSpecifier::oArg:
628         case ConversionSpecifier::OArg:
629         case ConversionSpecifier::uArg:
630         case ConversionSpecifier::UArg:
631         case ConversionSpecifier::xArg:
632         case ConversionSpecifier::XArg:
633         case ConversionSpecifier::nArg:
634         case ConversionSpecifier::FreeBSDrArg:
635           return true;
636         default:
637           return false;
638       }
639       
640     // Handle 'l' flag
641     case LengthModifier::AsLong:
642       switch (CS.getKind()) {
643         case ConversionSpecifier::dArg:
644         case ConversionSpecifier::DArg:
645         case ConversionSpecifier::iArg:
646         case ConversionSpecifier::oArg:
647         case ConversionSpecifier::OArg:
648         case ConversionSpecifier::uArg:
649         case ConversionSpecifier::UArg:
650         case ConversionSpecifier::xArg:
651         case ConversionSpecifier::XArg:
652         case ConversionSpecifier::aArg:
653         case ConversionSpecifier::AArg:
654         case ConversionSpecifier::fArg:
655         case ConversionSpecifier::FArg:
656         case ConversionSpecifier::eArg:
657         case ConversionSpecifier::EArg:
658         case ConversionSpecifier::gArg:
659         case ConversionSpecifier::GArg:
660         case ConversionSpecifier::nArg:
661         case ConversionSpecifier::cArg:
662         case ConversionSpecifier::sArg:
663         case ConversionSpecifier::FreeBSDrArg:
664         case ConversionSpecifier::ScanListArg:
665           return true;
666         default:
667           return false;
668       }
669       
670     case LengthModifier::AsLongDouble:
671       switch (CS.getKind()) {
672         case ConversionSpecifier::aArg:
673         case ConversionSpecifier::AArg:
674         case ConversionSpecifier::fArg:
675         case ConversionSpecifier::FArg:
676         case ConversionSpecifier::eArg:
677         case ConversionSpecifier::EArg:
678         case ConversionSpecifier::gArg:
679         case ConversionSpecifier::GArg:
680           return true;
681         // GNU libc extension.
682         case ConversionSpecifier::dArg:
683         case ConversionSpecifier::iArg:
684         case ConversionSpecifier::oArg:
685         case ConversionSpecifier::uArg:
686         case ConversionSpecifier::xArg:
687         case ConversionSpecifier::XArg:
688           return !Target.getTriple().isOSDarwin() &&
689                  !Target.getTriple().isOSWindows();
690         default:
691           return false;
692       }
693
694     case LengthModifier::AsAllocate:
695       switch (CS.getKind()) {
696         case ConversionSpecifier::sArg:
697         case ConversionSpecifier::SArg:
698         case ConversionSpecifier::ScanListArg:
699           return true;
700         default:
701           return false;
702       }
703
704     case LengthModifier::AsMAllocate:
705       switch (CS.getKind()) {
706         case ConversionSpecifier::cArg:
707         case ConversionSpecifier::CArg:
708         case ConversionSpecifier::sArg:
709         case ConversionSpecifier::SArg:
710         case ConversionSpecifier::ScanListArg:
711           return true;
712         default:
713           return false;
714       }
715     case LengthModifier::AsInt32:
716     case LengthModifier::AsInt3264:
717     case LengthModifier::AsInt64:
718       switch (CS.getKind()) {
719         case ConversionSpecifier::dArg:
720         case ConversionSpecifier::iArg:
721         case ConversionSpecifier::oArg:
722         case ConversionSpecifier::uArg:
723         case ConversionSpecifier::xArg:
724         case ConversionSpecifier::XArg:
725           return Target.getTriple().isOSMSVCRT();
726         default:
727           return false;
728       }
729   }
730   llvm_unreachable("Invalid LengthModifier Kind!");
731 }
732
733 bool FormatSpecifier::hasStandardLengthModifier() const {
734   switch (LM.getKind()) {
735     case LengthModifier::None:
736     case LengthModifier::AsChar:
737     case LengthModifier::AsShort:
738     case LengthModifier::AsLong:
739     case LengthModifier::AsLongLong:
740     case LengthModifier::AsIntMax:
741     case LengthModifier::AsSizeT:
742     case LengthModifier::AsPtrDiff:
743     case LengthModifier::AsLongDouble:
744       return true;
745     case LengthModifier::AsAllocate:
746     case LengthModifier::AsMAllocate:
747     case LengthModifier::AsQuad:
748     case LengthModifier::AsInt32:
749     case LengthModifier::AsInt3264:
750     case LengthModifier::AsInt64:
751       return false;
752   }
753   llvm_unreachable("Invalid LengthModifier Kind!");
754 }
755
756 bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
757   switch (CS.getKind()) {
758     case ConversionSpecifier::cArg:
759     case ConversionSpecifier::dArg:
760     case ConversionSpecifier::iArg:
761     case ConversionSpecifier::oArg:
762     case ConversionSpecifier::uArg:
763     case ConversionSpecifier::xArg:
764     case ConversionSpecifier::XArg:
765     case ConversionSpecifier::fArg:
766     case ConversionSpecifier::FArg:
767     case ConversionSpecifier::eArg:
768     case ConversionSpecifier::EArg:
769     case ConversionSpecifier::gArg:
770     case ConversionSpecifier::GArg:
771     case ConversionSpecifier::aArg:
772     case ConversionSpecifier::AArg:
773     case ConversionSpecifier::sArg:
774     case ConversionSpecifier::pArg:
775     case ConversionSpecifier::nArg:
776     case ConversionSpecifier::ObjCObjArg:
777     case ConversionSpecifier::ScanListArg:
778     case ConversionSpecifier::PercentArg:
779       return true;
780     case ConversionSpecifier::CArg:
781     case ConversionSpecifier::SArg:
782       return LangOpt.ObjC1 || LangOpt.ObjC2;
783     case ConversionSpecifier::InvalidSpecifier:
784     case ConversionSpecifier::FreeBSDbArg:
785     case ConversionSpecifier::FreeBSDDArg:
786     case ConversionSpecifier::FreeBSDrArg:
787     case ConversionSpecifier::PrintErrno:
788     case ConversionSpecifier::DArg:
789     case ConversionSpecifier::OArg:
790     case ConversionSpecifier::UArg:
791       return false;
792   }
793   llvm_unreachable("Invalid ConversionSpecifier Kind!");
794 }
795
796 bool FormatSpecifier::hasStandardLengthConversionCombination() const {
797   if (LM.getKind() == LengthModifier::AsLongDouble) {
798     switch(CS.getKind()) {
799         case ConversionSpecifier::dArg:
800         case ConversionSpecifier::iArg:
801         case ConversionSpecifier::oArg:
802         case ConversionSpecifier::uArg:
803         case ConversionSpecifier::xArg:
804         case ConversionSpecifier::XArg:
805           return false;
806         default:
807           return true;
808     }
809   }
810   return true;
811 }
812
813 Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
814   if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
815     if (LM.getKind() == LengthModifier::AsLongDouble ||
816         LM.getKind() == LengthModifier::AsQuad) {
817       LengthModifier FixedLM(LM);
818       FixedLM.setKind(LengthModifier::AsLongLong);
819       return FixedLM;
820     }
821   }
822
823   return None;
824 }
825
826 bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
827                                                 LengthModifier &LM) {
828   assert(isa<TypedefType>(QT) && "Expected a TypedefType");
829   const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
830
831   for (;;) {
832     const IdentifierInfo *Identifier = Typedef->getIdentifier();
833     if (Identifier->getName() == "size_t") {
834       LM.setKind(LengthModifier::AsSizeT);
835       return true;
836     } else if (Identifier->getName() == "ssize_t") {
837       // Not C99, but common in Unix.
838       LM.setKind(LengthModifier::AsSizeT);
839       return true;
840     } else if (Identifier->getName() == "intmax_t") {
841       LM.setKind(LengthModifier::AsIntMax);
842       return true;
843     } else if (Identifier->getName() == "uintmax_t") {
844       LM.setKind(LengthModifier::AsIntMax);
845       return true;
846     } else if (Identifier->getName() == "ptrdiff_t") {
847       LM.setKind(LengthModifier::AsPtrDiff);
848       return true;
849     }
850
851     QualType T = Typedef->getUnderlyingType();
852     if (!isa<TypedefType>(T))
853       break;
854
855     Typedef = cast<TypedefType>(T)->getDecl();
856   }
857   return false;
858 }