PrintfFormatString.cpp revision 276479
1//== PrintfFormatString.cpp - Analysis of printf format strings --*- 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// Handling of format string in printf and friends.  The structure of format
11// strings for fprintf() are described in C99 7.19.6.1.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/Analyses/FormatString.h"
16#include "FormatStringParsing.h"
17#include "clang/Basic/TargetInfo.h"
18
19using clang::analyze_format_string::ArgType;
20using clang::analyze_format_string::FormatStringHandler;
21using clang::analyze_format_string::LengthModifier;
22using clang::analyze_format_string::OptionalAmount;
23using clang::analyze_format_string::ConversionSpecifier;
24using clang::analyze_printf::PrintfSpecifier;
25
26using namespace clang;
27
28typedef clang::analyze_format_string::SpecifierResult<PrintfSpecifier>
29        PrintfSpecifierResult;
30
31//===----------------------------------------------------------------------===//
32// Methods for parsing format strings.
33//===----------------------------------------------------------------------===//
34
35using analyze_format_string::ParseNonPositionAmount;
36
37static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS,
38                           const char *Start, const char *&Beg, const char *E,
39                           unsigned *argIndex) {
40  if (argIndex) {
41    FS.setPrecision(ParseNonPositionAmount(Beg, E, *argIndex));
42  } else {
43    const OptionalAmount Amt = ParsePositionAmount(H, Start, Beg, E,
44                                           analyze_format_string::PrecisionPos);
45    if (Amt.isInvalid())
46      return true;
47    FS.setPrecision(Amt);
48  }
49  return false;
50}
51
52static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
53                                                  const char *&Beg,
54                                                  const char *E,
55                                                  unsigned &argIndex,
56                                                  const LangOptions &LO,
57                                                  const TargetInfo &Target) {
58
59  using namespace clang::analyze_format_string;
60  using namespace clang::analyze_printf;
61
62  const char *I = Beg;
63  const char *Start = nullptr;
64  UpdateOnReturn <const char*> UpdateBeg(Beg, I);
65
66  // Look for a '%' character that indicates the start of a format specifier.
67  for ( ; I != E ; ++I) {
68    char c = *I;
69    if (c == '\0') {
70      // Detect spurious null characters, which are likely errors.
71      H.HandleNullChar(I);
72      return true;
73    }
74    if (c == '%') {
75      Start = I++;  // Record the start of the format specifier.
76      break;
77    }
78  }
79
80  // No format specifier found?
81  if (!Start)
82    return false;
83
84  if (I == E) {
85    // No more characters left?
86    H.HandleIncompleteSpecifier(Start, E - Start);
87    return true;
88  }
89
90  PrintfSpecifier FS;
91  if (ParseArgPosition(H, FS, Start, I, E))
92    return true;
93
94  if (I == E) {
95    // No more characters left?
96    H.HandleIncompleteSpecifier(Start, E - Start);
97    return true;
98  }
99
100  // Look for flags (if any).
101  bool hasMore = true;
102  for ( ; I != E; ++I) {
103    switch (*I) {
104      default: hasMore = false; break;
105      case '\'':
106        // FIXME: POSIX specific.  Always accept?
107        FS.setHasThousandsGrouping(I);
108        break;
109      case '-': FS.setIsLeftJustified(I); break;
110      case '+': FS.setHasPlusPrefix(I); break;
111      case ' ': FS.setHasSpacePrefix(I); break;
112      case '#': FS.setHasAlternativeForm(I); break;
113      case '0': FS.setHasLeadingZeros(I); break;
114    }
115    if (!hasMore)
116      break;
117  }
118
119  if (I == E) {
120    // No more characters left?
121    H.HandleIncompleteSpecifier(Start, E - Start);
122    return true;
123  }
124
125  // Look for the field width (if any).
126  if (ParseFieldWidth(H, FS, Start, I, E,
127                      FS.usesPositionalArg() ? nullptr : &argIndex))
128    return true;
129
130  if (I == E) {
131    // No more characters left?
132    H.HandleIncompleteSpecifier(Start, E - Start);
133    return true;
134  }
135
136  // Look for the precision (if any).
137  if (*I == '.') {
138    ++I;
139    if (I == E) {
140      H.HandleIncompleteSpecifier(Start, E - Start);
141      return true;
142    }
143
144    if (ParsePrecision(H, FS, Start, I, E,
145                       FS.usesPositionalArg() ? nullptr : &argIndex))
146      return true;
147
148    if (I == E) {
149      // No more characters left?
150      H.HandleIncompleteSpecifier(Start, E - Start);
151      return true;
152    }
153  }
154
155  // Look for the length modifier.
156  if (ParseLengthModifier(FS, I, E, LO) && I == E) {
157    // No more characters left?
158    H.HandleIncompleteSpecifier(Start, E - Start);
159    return true;
160  }
161
162  if (*I == '\0') {
163    // Detect spurious null characters, which are likely errors.
164    H.HandleNullChar(I);
165    return true;
166  }
167
168  // Finally, look for the conversion specifier.
169  const char *conversionPosition = I++;
170  ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier;
171  switch (*conversionPosition) {
172    default:
173      break;
174    // C99: 7.19.6.1 (section 8).
175    case '%': k = ConversionSpecifier::PercentArg;   break;
176    case 'A': k = ConversionSpecifier::AArg; break;
177    case 'E': k = ConversionSpecifier::EArg; break;
178    case 'F': k = ConversionSpecifier::FArg; break;
179    case 'G': k = ConversionSpecifier::GArg; break;
180    case 'X': k = ConversionSpecifier::XArg; break;
181    case 'a': k = ConversionSpecifier::aArg; break;
182    case 'c': k = ConversionSpecifier::cArg; break;
183    case 'd': k = ConversionSpecifier::dArg; break;
184    case 'e': k = ConversionSpecifier::eArg; break;
185    case 'f': k = ConversionSpecifier::fArg; break;
186    case 'g': k = ConversionSpecifier::gArg; break;
187    case 'i': k = ConversionSpecifier::iArg; break;
188    case 'n': k = ConversionSpecifier::nArg; break;
189    case 'o': k = ConversionSpecifier::oArg; break;
190    case 'p': k = ConversionSpecifier::pArg; break;
191    case 's': k = ConversionSpecifier::sArg; break;
192    case 'u': k = ConversionSpecifier::uArg; break;
193    case 'x': k = ConversionSpecifier::xArg; break;
194    // POSIX specific.
195    case 'C': k = ConversionSpecifier::CArg; break;
196    case 'S': k = ConversionSpecifier::SArg; break;
197    // Objective-C.
198    case '@': k = ConversionSpecifier::ObjCObjArg; break;
199    // Glibc specific.
200    case 'm': k = ConversionSpecifier::PrintErrno; break;
201    // FreeBSD format extensions
202    case 'b':
203      if (LO.FormatExtensions)
204        k = ConversionSpecifier::FreeBSDbArg; // int followed by char *
205      break;
206    case 'r':
207      if (LO.FormatExtensions)
208        k = ConversionSpecifier::FreeBSDrArg;
209      break;
210    case 'y':
211      if (LO.FormatExtensions)
212        k = ConversionSpecifier::iArg;
213      break;
214    // Apple-specific
215    case 'D':
216      if (Target.getTriple().isOSDarwin())
217        k = ConversionSpecifier::DArg;
218      else if (LO.FormatExtensions)
219        k = ConversionSpecifier::FreeBSDDArg; // u_char * followed by char *
220      break;
221    case 'O':
222      if (Target.getTriple().isOSDarwin())
223        k = ConversionSpecifier::OArg;
224      break;
225    case 'U':
226      if (Target.getTriple().isOSDarwin())
227        k = ConversionSpecifier::UArg;
228      break;
229  }
230  PrintfConversionSpecifier CS(conversionPosition, k);
231  FS.setConversionSpecifier(CS);
232  if (CS.consumesDataArgument() && !FS.usesPositionalArg())
233    FS.setArgIndex(argIndex++);
234  // FreeBSD extension
235  if (k == ConversionSpecifier::FreeBSDbArg ||
236      k == ConversionSpecifier::FreeBSDDArg)
237    argIndex++;
238
239  if (k == ConversionSpecifier::InvalidSpecifier) {
240    // Assume the conversion takes one argument.
241    return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, I - Start);
242  }
243  return PrintfSpecifierResult(Start, FS);
244}
245
246bool clang::analyze_format_string::ParsePrintfString(FormatStringHandler &H,
247                                                     const char *I,
248                                                     const char *E,
249                                                     const LangOptions &LO,
250                                                     const TargetInfo &Target) {
251
252  unsigned argIndex = 0;
253
254  // Keep looking for a format specifier until we have exhausted the string.
255  while (I != E) {
256    const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
257                                                            LO, Target);
258    // Did a fail-stop error of any kind occur when parsing the specifier?
259    // If so, don't do any more processing.
260    if (FSR.shouldStop())
261      return true;
262    // Did we exhaust the string or encounter an error that
263    // we can recover from?
264    if (!FSR.hasValue())
265      continue;
266    // We have a format specifier.  Pass it to the callback.
267    if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(),
268                                 I - FSR.getStart()))
269      return true;
270  }
271  assert(I == E && "Format string not exhausted");
272  return false;
273}
274
275//===----------------------------------------------------------------------===//
276// Methods on PrintfSpecifier.
277//===----------------------------------------------------------------------===//
278
279ArgType PrintfSpecifier::getArgType(ASTContext &Ctx,
280                                    bool IsObjCLiteral) const {
281  const PrintfConversionSpecifier &CS = getConversionSpecifier();
282
283  if (!CS.consumesDataArgument())
284    return ArgType::Invalid();
285
286  if (CS.getKind() == ConversionSpecifier::cArg)
287    switch (LM.getKind()) {
288      case LengthModifier::None: return Ctx.IntTy;
289      case LengthModifier::AsLong:
290        return ArgType(ArgType::WIntTy, "wint_t");
291      default:
292        return ArgType::Invalid();
293    }
294
295  if (CS.isIntArg())
296    switch (LM.getKind()) {
297      case LengthModifier::AsLongDouble:
298        // GNU extension.
299        return Ctx.LongLongTy;
300      case LengthModifier::None:
301        return Ctx.IntTy;
302      case LengthModifier::AsInt32:
303        return ArgType(Ctx.IntTy, "__int32");
304      case LengthModifier::AsChar: return ArgType::AnyCharTy;
305      case LengthModifier::AsShort: return Ctx.ShortTy;
306      case LengthModifier::AsLong: return Ctx.LongTy;
307      case LengthModifier::AsLongLong:
308      case LengthModifier::AsQuad:
309        return Ctx.LongLongTy;
310      case LengthModifier::AsInt64:
311        return ArgType(Ctx.LongLongTy, "__int64");
312      case LengthModifier::AsIntMax:
313        return ArgType(Ctx.getIntMaxType(), "intmax_t");
314      case LengthModifier::AsSizeT:
315        // FIXME: How to get the corresponding signed version of size_t?
316        return ArgType();
317      case LengthModifier::AsInt3264:
318        return Ctx.getTargetInfo().getTriple().isArch64Bit()
319                   ? ArgType(Ctx.LongLongTy, "__int64")
320                   : ArgType(Ctx.IntTy, "__int32");
321      case LengthModifier::AsPtrDiff:
322        return ArgType(Ctx.getPointerDiffType(), "ptrdiff_t");
323      case LengthModifier::AsAllocate:
324      case LengthModifier::AsMAllocate:
325        return ArgType::Invalid();
326    }
327
328  if (CS.isUIntArg())
329    switch (LM.getKind()) {
330      case LengthModifier::AsLongDouble:
331        // GNU extension.
332        return Ctx.UnsignedLongLongTy;
333      case LengthModifier::None:
334        return Ctx.UnsignedIntTy;
335      case LengthModifier::AsInt32:
336        return ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
337      case LengthModifier::AsChar: return Ctx.UnsignedCharTy;
338      case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
339      case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
340      case LengthModifier::AsLongLong:
341      case LengthModifier::AsQuad:
342        return Ctx.UnsignedLongLongTy;
343      case LengthModifier::AsInt64:
344        return ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64");
345      case LengthModifier::AsIntMax:
346        return ArgType(Ctx.getUIntMaxType(), "uintmax_t");
347      case LengthModifier::AsSizeT:
348        return ArgType(Ctx.getSizeType(), "size_t");
349      case LengthModifier::AsInt3264:
350        return Ctx.getTargetInfo().getTriple().isArch64Bit()
351                   ? ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64")
352                   : ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
353      case LengthModifier::AsPtrDiff:
354        // FIXME: How to get the corresponding unsigned
355        // version of ptrdiff_t?
356        return ArgType();
357      case LengthModifier::AsAllocate:
358      case LengthModifier::AsMAllocate:
359        return ArgType::Invalid();
360    }
361
362  if (CS.isDoubleArg()) {
363    if (LM.getKind() == LengthModifier::AsLongDouble)
364      return Ctx.LongDoubleTy;
365    return Ctx.DoubleTy;
366  }
367
368  if (CS.getKind() == ConversionSpecifier::nArg) {
369    switch (LM.getKind()) {
370      case LengthModifier::None:
371        return ArgType::PtrTo(Ctx.IntTy);
372      case LengthModifier::AsChar:
373        return ArgType::PtrTo(Ctx.SignedCharTy);
374      case LengthModifier::AsShort:
375        return ArgType::PtrTo(Ctx.ShortTy);
376      case LengthModifier::AsLong:
377        return ArgType::PtrTo(Ctx.LongTy);
378      case LengthModifier::AsLongLong:
379      case LengthModifier::AsQuad:
380        return ArgType::PtrTo(Ctx.LongLongTy);
381      case LengthModifier::AsIntMax:
382        return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
383      case LengthModifier::AsSizeT:
384        return ArgType(); // FIXME: ssize_t
385      case LengthModifier::AsPtrDiff:
386        return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
387      case LengthModifier::AsLongDouble:
388        return ArgType(); // FIXME: Is this a known extension?
389      case LengthModifier::AsAllocate:
390      case LengthModifier::AsMAllocate:
391      case LengthModifier::AsInt32:
392      case LengthModifier::AsInt3264:
393      case LengthModifier::AsInt64:
394        return ArgType::Invalid();
395    }
396  }
397
398  switch (CS.getKind()) {
399    case ConversionSpecifier::sArg:
400      if (LM.getKind() == LengthModifier::AsWideChar) {
401        if (IsObjCLiteral)
402          return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
403                         "const unichar *");
404        return ArgType(ArgType::WCStrTy, "wchar_t *");
405      }
406      return ArgType::CStrTy;
407    case ConversionSpecifier::SArg:
408      if (IsObjCLiteral)
409        return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
410                       "const unichar *");
411      return ArgType(ArgType::WCStrTy, "wchar_t *");
412    case ConversionSpecifier::CArg:
413      if (IsObjCLiteral)
414        return ArgType(Ctx.UnsignedShortTy, "unichar");
415      return ArgType(Ctx.WideCharTy, "wchar_t");
416    case ConversionSpecifier::pArg:
417      return ArgType::CPointerTy;
418    case ConversionSpecifier::ObjCObjArg:
419      return ArgType::ObjCPointerTy;
420    default:
421      break;
422  }
423
424  // FIXME: Handle other cases.
425  return ArgType();
426}
427
428bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
429                              ASTContext &Ctx, bool IsObjCLiteral) {
430  // %n is different from other conversion specifiers; don't try to fix it.
431  if (CS.getKind() == ConversionSpecifier::nArg)
432    return false;
433
434  // Handle Objective-C objects first. Note that while the '%@' specifier will
435  // not warn for structure pointer or void pointer arguments (because that's
436  // how CoreFoundation objects are implemented), we only show a fixit for '%@'
437  // if we know it's an object (block, id, class, or __attribute__((NSObject))).
438  if (QT->isObjCRetainableType()) {
439    if (!IsObjCLiteral)
440      return false;
441
442    CS.setKind(ConversionSpecifier::ObjCObjArg);
443
444    // Disable irrelevant flags
445    HasThousandsGrouping = false;
446    HasPlusPrefix = false;
447    HasSpacePrefix = false;
448    HasAlternativeForm = false;
449    HasLeadingZeroes = false;
450    Precision.setHowSpecified(OptionalAmount::NotSpecified);
451    LM.setKind(LengthModifier::None);
452
453    return true;
454  }
455
456  // Handle strings next (char *, wchar_t *)
457  if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
458    CS.setKind(ConversionSpecifier::sArg);
459
460    // Disable irrelevant flags
461    HasAlternativeForm = 0;
462    HasLeadingZeroes = 0;
463
464    // Set the long length modifier for wide characters
465    if (QT->getPointeeType()->isWideCharType())
466      LM.setKind(LengthModifier::AsWideChar);
467    else
468      LM.setKind(LengthModifier::None);
469
470    return true;
471  }
472
473  // If it's an enum, get its underlying type.
474  if (const EnumType *ETy = QT->getAs<EnumType>())
475    QT = ETy->getDecl()->getIntegerType();
476
477  // We can only work with builtin types.
478  const BuiltinType *BT = QT->getAs<BuiltinType>();
479  if (!BT)
480    return false;
481
482  // Set length modifier
483  switch (BT->getKind()) {
484  case BuiltinType::Bool:
485  case BuiltinType::WChar_U:
486  case BuiltinType::WChar_S:
487  case BuiltinType::Char16:
488  case BuiltinType::Char32:
489  case BuiltinType::UInt128:
490  case BuiltinType::Int128:
491  case BuiltinType::Half:
492    // Various types which are non-trivial to correct.
493    return false;
494
495#define SIGNED_TYPE(Id, SingletonId)
496#define UNSIGNED_TYPE(Id, SingletonId)
497#define FLOATING_TYPE(Id, SingletonId)
498#define BUILTIN_TYPE(Id, SingletonId) \
499  case BuiltinType::Id:
500#include "clang/AST/BuiltinTypes.def"
501    // Misc other stuff which doesn't make sense here.
502    return false;
503
504  case BuiltinType::UInt:
505  case BuiltinType::Int:
506  case BuiltinType::Float:
507  case BuiltinType::Double:
508    LM.setKind(LengthModifier::None);
509    break;
510
511  case BuiltinType::Char_U:
512  case BuiltinType::UChar:
513  case BuiltinType::Char_S:
514  case BuiltinType::SChar:
515    LM.setKind(LengthModifier::AsChar);
516    break;
517
518  case BuiltinType::Short:
519  case BuiltinType::UShort:
520    LM.setKind(LengthModifier::AsShort);
521    break;
522
523  case BuiltinType::Long:
524  case BuiltinType::ULong:
525    LM.setKind(LengthModifier::AsLong);
526    break;
527
528  case BuiltinType::LongLong:
529  case BuiltinType::ULongLong:
530    LM.setKind(LengthModifier::AsLongLong);
531    break;
532
533  case BuiltinType::LongDouble:
534    LM.setKind(LengthModifier::AsLongDouble);
535    break;
536  }
537
538  // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
539  if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus11))
540    namedTypeToLengthModifier(QT, LM);
541
542  // If fixing the length modifier was enough, we might be done.
543  if (hasValidLengthModifier(Ctx.getTargetInfo())) {
544    // If we're going to offer a fix anyway, make sure the sign matches.
545    switch (CS.getKind()) {
546    case ConversionSpecifier::uArg:
547    case ConversionSpecifier::UArg:
548      if (QT->isSignedIntegerType())
549        CS.setKind(clang::analyze_format_string::ConversionSpecifier::dArg);
550      break;
551    case ConversionSpecifier::dArg:
552    case ConversionSpecifier::DArg:
553    case ConversionSpecifier::iArg:
554      if (QT->isUnsignedIntegerType() && !HasPlusPrefix)
555        CS.setKind(clang::analyze_format_string::ConversionSpecifier::uArg);
556      break;
557    default:
558      // Other specifiers do not have signed/unsigned variants.
559      break;
560    }
561
562    const analyze_printf::ArgType &ATR = getArgType(Ctx, IsObjCLiteral);
563    if (ATR.isValid() && ATR.matchesType(Ctx, QT))
564      return true;
565  }
566
567  // Set conversion specifier and disable any flags which do not apply to it.
568  // Let typedefs to char fall through to int, as %c is silly for uint8_t.
569  if (!isa<TypedefType>(QT) && QT->isCharType()) {
570    CS.setKind(ConversionSpecifier::cArg);
571    LM.setKind(LengthModifier::None);
572    Precision.setHowSpecified(OptionalAmount::NotSpecified);
573    HasAlternativeForm = 0;
574    HasLeadingZeroes = 0;
575    HasPlusPrefix = 0;
576  }
577  // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
578  else if (QT->isRealFloatingType()) {
579    CS.setKind(ConversionSpecifier::fArg);
580  }
581  else if (QT->isSignedIntegerType()) {
582    CS.setKind(ConversionSpecifier::dArg);
583    HasAlternativeForm = 0;
584  }
585  else if (QT->isUnsignedIntegerType()) {
586    CS.setKind(ConversionSpecifier::uArg);
587    HasAlternativeForm = 0;
588    HasPlusPrefix = 0;
589  } else {
590    llvm_unreachable("Unexpected type");
591  }
592
593  return true;
594}
595
596void PrintfSpecifier::toString(raw_ostream &os) const {
597  // Whilst some features have no defined order, we are using the order
598  // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
599  os << "%";
600
601  // Positional args
602  if (usesPositionalArg()) {
603    os << getPositionalArgIndex() << "$";
604  }
605
606  // Conversion flags
607  if (IsLeftJustified)    os << "-";
608  if (HasPlusPrefix)      os << "+";
609  if (HasSpacePrefix)     os << " ";
610  if (HasAlternativeForm) os << "#";
611  if (HasLeadingZeroes)   os << "0";
612
613  // Minimum field width
614  FieldWidth.toString(os);
615  // Precision
616  Precision.toString(os);
617  // Length modifier
618  os << LM.toString();
619  // Conversion specifier
620  os << CS.toString();
621}
622
623bool PrintfSpecifier::hasValidPlusPrefix() const {
624  if (!HasPlusPrefix)
625    return true;
626
627  // The plus prefix only makes sense for signed conversions
628  switch (CS.getKind()) {
629  case ConversionSpecifier::dArg:
630  case ConversionSpecifier::DArg:
631  case ConversionSpecifier::iArg:
632  case ConversionSpecifier::fArg:
633  case ConversionSpecifier::FArg:
634  case ConversionSpecifier::eArg:
635  case ConversionSpecifier::EArg:
636  case ConversionSpecifier::gArg:
637  case ConversionSpecifier::GArg:
638  case ConversionSpecifier::aArg:
639  case ConversionSpecifier::AArg:
640  case ConversionSpecifier::FreeBSDrArg:
641    return true;
642
643  default:
644    return false;
645  }
646}
647
648bool PrintfSpecifier::hasValidAlternativeForm() const {
649  if (!HasAlternativeForm)
650    return true;
651
652  // Alternate form flag only valid with the oxXaAeEfFgG conversions
653  switch (CS.getKind()) {
654  case ConversionSpecifier::oArg:
655  case ConversionSpecifier::OArg:
656  case ConversionSpecifier::xArg:
657  case ConversionSpecifier::XArg:
658  case ConversionSpecifier::aArg:
659  case ConversionSpecifier::AArg:
660  case ConversionSpecifier::eArg:
661  case ConversionSpecifier::EArg:
662  case ConversionSpecifier::fArg:
663  case ConversionSpecifier::FArg:
664  case ConversionSpecifier::gArg:
665  case ConversionSpecifier::GArg:
666  case ConversionSpecifier::FreeBSDrArg:
667    return true;
668
669  default:
670    return false;
671  }
672}
673
674bool PrintfSpecifier::hasValidLeadingZeros() const {
675  if (!HasLeadingZeroes)
676    return true;
677
678  // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
679  switch (CS.getKind()) {
680  case ConversionSpecifier::dArg:
681  case ConversionSpecifier::DArg:
682  case ConversionSpecifier::iArg:
683  case ConversionSpecifier::oArg:
684  case ConversionSpecifier::OArg:
685  case ConversionSpecifier::uArg:
686  case ConversionSpecifier::UArg:
687  case ConversionSpecifier::xArg:
688  case ConversionSpecifier::XArg:
689  case ConversionSpecifier::aArg:
690  case ConversionSpecifier::AArg:
691  case ConversionSpecifier::eArg:
692  case ConversionSpecifier::EArg:
693  case ConversionSpecifier::fArg:
694  case ConversionSpecifier::FArg:
695  case ConversionSpecifier::gArg:
696  case ConversionSpecifier::GArg:
697    return true;
698
699  default:
700    return false;
701  }
702}
703
704bool PrintfSpecifier::hasValidSpacePrefix() const {
705  if (!HasSpacePrefix)
706    return true;
707
708  // The space prefix only makes sense for signed conversions
709  switch (CS.getKind()) {
710  case ConversionSpecifier::dArg:
711  case ConversionSpecifier::DArg:
712  case ConversionSpecifier::iArg:
713  case ConversionSpecifier::fArg:
714  case ConversionSpecifier::FArg:
715  case ConversionSpecifier::eArg:
716  case ConversionSpecifier::EArg:
717  case ConversionSpecifier::gArg:
718  case ConversionSpecifier::GArg:
719  case ConversionSpecifier::aArg:
720  case ConversionSpecifier::AArg:
721    return true;
722
723  default:
724    return false;
725  }
726}
727
728bool PrintfSpecifier::hasValidLeftJustified() const {
729  if (!IsLeftJustified)
730    return true;
731
732  // The left justified flag is valid for all conversions except n
733  switch (CS.getKind()) {
734  case ConversionSpecifier::nArg:
735    return false;
736
737  default:
738    return true;
739  }
740}
741
742bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
743  if (!HasThousandsGrouping)
744    return true;
745
746  switch (CS.getKind()) {
747    case ConversionSpecifier::dArg:
748    case ConversionSpecifier::DArg:
749    case ConversionSpecifier::iArg:
750    case ConversionSpecifier::uArg:
751    case ConversionSpecifier::UArg:
752    case ConversionSpecifier::fArg:
753    case ConversionSpecifier::FArg:
754    case ConversionSpecifier::gArg:
755    case ConversionSpecifier::GArg:
756      return true;
757    default:
758      return false;
759  }
760}
761
762bool PrintfSpecifier::hasValidPrecision() const {
763  if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
764    return true;
765
766  // Precision is only valid with the diouxXaAeEfFgGs conversions
767  switch (CS.getKind()) {
768  case ConversionSpecifier::dArg:
769  case ConversionSpecifier::DArg:
770  case ConversionSpecifier::iArg:
771  case ConversionSpecifier::oArg:
772  case ConversionSpecifier::OArg:
773  case ConversionSpecifier::uArg:
774  case ConversionSpecifier::UArg:
775  case ConversionSpecifier::xArg:
776  case ConversionSpecifier::XArg:
777  case ConversionSpecifier::aArg:
778  case ConversionSpecifier::AArg:
779  case ConversionSpecifier::eArg:
780  case ConversionSpecifier::EArg:
781  case ConversionSpecifier::fArg:
782  case ConversionSpecifier::FArg:
783  case ConversionSpecifier::gArg:
784  case ConversionSpecifier::GArg:
785  case ConversionSpecifier::sArg:
786    return true;
787
788  default:
789    return false;
790  }
791}
792bool PrintfSpecifier::hasValidFieldWidth() const {
793  if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
794      return true;
795
796  // The field width is valid for all conversions except n
797  switch (CS.getKind()) {
798  case ConversionSpecifier::nArg:
799    return false;
800
801  default:
802    return true;
803  }
804}
805