clang  6.0.0svn
PrintfFormatString.cpp
Go to the documentation of this file.
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 
16 #include "FormatStringParsing.h"
17 #include "clang/Basic/TargetInfo.h"
18 
25 
26 using namespace clang;
27 
30 
31 //===----------------------------------------------------------------------===//
32 // Methods for parsing format strings.
33 //===----------------------------------------------------------------------===//
34 
36 
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,
45  if (Amt.isInvalid())
46  return true;
47  FS.setPrecision(Amt);
48  }
49  return false;
50 }
51 
53  const char *FlagBeg, const char *E, bool Warn) {
54  StringRef Flag(FlagBeg, E - FlagBeg);
55  // Currently there is only one flag.
56  if (Flag == "tt") {
57  FS.setHasObjCTechnicalTerm(FlagBeg);
58  return false;
59  }
60  // Handle either the case of no flag or an invalid flag.
61  if (Warn) {
62  if (Flag == "")
63  H.HandleEmptyObjCModifierFlag(FlagBeg, E - FlagBeg);
64  else
65  H.HandleInvalidObjCModifierFlag(FlagBeg, E - FlagBeg);
66  }
67  return true;
68 }
69 
71  const char *&Beg,
72  const char *E,
73  unsigned &argIndex,
74  const LangOptions &LO,
75  const TargetInfo &Target,
76  bool Warn,
77  bool isFreeBSDKPrintf) {
78 
79  using namespace clang::analyze_format_string;
80  using namespace clang::analyze_printf;
81 
82  const char *I = Beg;
83  const char *Start = nullptr;
84  UpdateOnReturn <const char*> UpdateBeg(Beg, I);
85 
86  // Look for a '%' character that indicates the start of a format specifier.
87  for ( ; I != E ; ++I) {
88  char c = *I;
89  if (c == '\0') {
90  // Detect spurious null characters, which are likely errors.
91  H.HandleNullChar(I);
92  return true;
93  }
94  if (c == '%') {
95  Start = I++; // Record the start of the format specifier.
96  break;
97  }
98  }
99 
100  // No format specifier found?
101  if (!Start)
102  return false;
103 
104  if (I == E) {
105  // No more characters left?
106  if (Warn)
107  H.HandleIncompleteSpecifier(Start, E - Start);
108  return true;
109  }
110 
111  PrintfSpecifier FS;
112  if (ParseArgPosition(H, FS, Start, I, E))
113  return true;
114 
115  if (I == E) {
116  // No more characters left?
117  if (Warn)
118  H.HandleIncompleteSpecifier(Start, E - Start);
119  return true;
120  }
121 
122  const char *OSLogVisibilityFlagsStart = nullptr,
123  *OSLogVisibilityFlagsEnd = nullptr;
124  if (*I == '{') {
125  OSLogVisibilityFlagsStart = I++;
126  // Find the end of the modifier.
127  while (I != E && *I != '}') {
128  I++;
129  }
130  if (I == E) {
131  if (Warn)
132  H.HandleIncompleteSpecifier(Start, E - Start);
133  return true;
134  }
135  assert(*I == '}');
136  OSLogVisibilityFlagsEnd = I++;
137 
138  // Just see if 'private' or 'public' is the first word. os_log itself will
139  // do any further parsing.
140  const char *P = OSLogVisibilityFlagsStart + 1;
141  while (P < OSLogVisibilityFlagsEnd && isspace(*P))
142  P++;
143  const char *WordStart = P;
144  while (P < OSLogVisibilityFlagsEnd && (isalnum(*P) || *P == '_'))
145  P++;
146  const char *WordEnd = P;
147  StringRef Word(WordStart, WordEnd - WordStart);
148  if (Word == "private") {
149  FS.setIsPrivate(WordStart);
150  } else if (Word == "public") {
151  FS.setIsPublic(WordStart);
152  }
153  }
154 
155  // Look for flags (if any).
156  bool hasMore = true;
157  for ( ; I != E; ++I) {
158  switch (*I) {
159  default: hasMore = false; break;
160  case '\'':
161  // FIXME: POSIX specific. Always accept?
163  break;
164  case '-': FS.setIsLeftJustified(I); break;
165  case '+': FS.setHasPlusPrefix(I); break;
166  case ' ': FS.setHasSpacePrefix(I); break;
167  case '#': FS.setHasAlternativeForm(I); break;
168  case '0': FS.setHasLeadingZeros(I); break;
169  }
170  if (!hasMore)
171  break;
172  }
173 
174  if (I == E) {
175  // No more characters left?
176  if (Warn)
177  H.HandleIncompleteSpecifier(Start, E - Start);
178  return true;
179  }
180 
181  // Look for the field width (if any).
182  if (ParseFieldWidth(H, FS, Start, I, E,
183  FS.usesPositionalArg() ? nullptr : &argIndex))
184  return true;
185 
186  if (I == E) {
187  // No more characters left?
188  if (Warn)
189  H.HandleIncompleteSpecifier(Start, E - Start);
190  return true;
191  }
192 
193  // Look for the precision (if any).
194  if (*I == '.') {
195  ++I;
196  if (I == E) {
197  if (Warn)
198  H.HandleIncompleteSpecifier(Start, E - Start);
199  return true;
200  }
201 
202  if (ParsePrecision(H, FS, Start, I, E,
203  FS.usesPositionalArg() ? nullptr : &argIndex))
204  return true;
205 
206  if (I == E) {
207  // No more characters left?
208  if (Warn)
209  H.HandleIncompleteSpecifier(Start, E - Start);
210  return true;
211  }
212  }
213 
214  // Look for the length modifier.
215  if (ParseLengthModifier(FS, I, E, LO) && I == E) {
216  // No more characters left?
217  if (Warn)
218  H.HandleIncompleteSpecifier(Start, E - Start);
219  return true;
220  }
221 
222  // Look for the Objective-C modifier flags, if any.
223  // We parse these here, even if they don't apply to
224  // the conversion specifier, and then emit an error
225  // later if the conversion specifier isn't '@'. This
226  // enables better recovery, and we don't know if
227  // these flags are applicable until later.
228  const char *ObjCModifierFlagsStart = nullptr,
229  *ObjCModifierFlagsEnd = nullptr;
230  if (*I == '[') {
231  ObjCModifierFlagsStart = I;
232  ++I;
233  auto flagStart = I;
234  for (;; ++I) {
235  ObjCModifierFlagsEnd = I;
236  if (I == E) {
237  if (Warn)
238  H.HandleIncompleteSpecifier(Start, E - Start);
239  return true;
240  }
241  // Did we find the closing ']'?
242  if (*I == ']') {
243  if (ParseObjCFlags(H, FS, flagStart, I, Warn))
244  return true;
245  ++I;
246  break;
247  }
248  // There are no separators defined yet for multiple
249  // Objective-C modifier flags. When those are
250  // defined, this is the place to check.
251  }
252  }
253 
254  if (*I == '\0') {
255  // Detect spurious null characters, which are likely errors.
256  H.HandleNullChar(I);
257  return true;
258  }
259 
260  // Finally, look for the conversion specifier.
261  const char *conversionPosition = I++;
263  switch (*conversionPosition) {
264  default:
265  break;
266  // C99: 7.19.6.1 (section 8).
267  case '%': k = ConversionSpecifier::PercentArg; break;
268  case 'A': k = ConversionSpecifier::AArg; break;
269  case 'E': k = ConversionSpecifier::EArg; break;
270  case 'F': k = ConversionSpecifier::FArg; break;
271  case 'G': k = ConversionSpecifier::GArg; break;
272  case 'X': k = ConversionSpecifier::XArg; break;
273  case 'a': k = ConversionSpecifier::aArg; break;
274  case 'c': k = ConversionSpecifier::cArg; break;
275  case 'd': k = ConversionSpecifier::dArg; break;
276  case 'e': k = ConversionSpecifier::eArg; break;
277  case 'f': k = ConversionSpecifier::fArg; break;
278  case 'g': k = ConversionSpecifier::gArg; break;
279  case 'i': k = ConversionSpecifier::iArg; break;
280  case 'n': k = ConversionSpecifier::nArg; break;
281  case 'o': k = ConversionSpecifier::oArg; break;
282  case 'p': k = ConversionSpecifier::pArg; break;
283  case 's': k = ConversionSpecifier::sArg; break;
284  case 'u': k = ConversionSpecifier::uArg; break;
285  case 'x': k = ConversionSpecifier::xArg; break;
286  // POSIX specific.
287  case 'C': k = ConversionSpecifier::CArg; break;
288  case 'S': k = ConversionSpecifier::SArg; break;
289  // Apple extension for os_log
290  case 'P':
292  break;
293  // Objective-C.
294  case '@': k = ConversionSpecifier::ObjCObjArg; break;
295  // Glibc specific.
296  case 'm': k = ConversionSpecifier::PrintErrno; break;
297  // FreeBSD kernel specific.
298  case 'b':
299  if (isFreeBSDKPrintf)
300  k = ConversionSpecifier::FreeBSDbArg; // int followed by char *
301  break;
302  case 'r':
303  if (isFreeBSDKPrintf)
305  break;
306  case 'y':
307  if (isFreeBSDKPrintf)
309  break;
310  // Apple-specific.
311  case 'D':
312  if (isFreeBSDKPrintf)
313  k = ConversionSpecifier::FreeBSDDArg; // void * followed by char *
314  else if (Target.getTriple().isOSDarwin())
316  break;
317  case 'O':
318  if (Target.getTriple().isOSDarwin())
320  break;
321  case 'U':
322  if (Target.getTriple().isOSDarwin())
324  break;
325  // MS specific.
326  case 'Z':
327  if (Target.getTriple().isOSMSVCRT())
329  }
330 
331  // Check to see if we used the Objective-C modifier flags with
332  // a conversion specifier other than '@'.
335  ObjCModifierFlagsStart) {
336  H.HandleObjCFlagsWithNonObjCConversion(ObjCModifierFlagsStart,
337  ObjCModifierFlagsEnd + 1,
338  conversionPosition);
339  return true;
340  }
341 
342  PrintfConversionSpecifier CS(conversionPosition, k);
343  FS.setConversionSpecifier(CS);
344  if (CS.consumesDataArgument() && !FS.usesPositionalArg())
345  FS.setArgIndex(argIndex++);
346  // FreeBSD kernel specific.
349  argIndex++;
350 
352  unsigned Len = I - Start;
353  if (ParseUTF8InvalidSpecifier(Start, E, Len)) {
354  CS.setEndScanList(Start + Len);
355  FS.setConversionSpecifier(CS);
356  }
357  // Assume the conversion takes one argument.
358  return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, Len);
359  }
360  return PrintfSpecifierResult(Start, FS);
361 }
362 
364  const char *I,
365  const char *E,
366  const LangOptions &LO,
367  const TargetInfo &Target,
368  bool isFreeBSDKPrintf) {
369 
370  unsigned argIndex = 0;
371 
372  // Keep looking for a format specifier until we have exhausted the string.
373  while (I != E) {
374  const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
375  LO, Target, true,
376  isFreeBSDKPrintf);
377  // Did a fail-stop error of any kind occur when parsing the specifier?
378  // If so, don't do any more processing.
379  if (FSR.shouldStop())
380  return true;
381  // Did we exhaust the string or encounter an error that
382  // we can recover from?
383  if (!FSR.hasValue())
384  continue;
385  // We have a format specifier. Pass it to the callback.
386  if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(),
387  I - FSR.getStart()))
388  return true;
389  }
390  assert(I == E && "Format string not exhausted");
391  return false;
392 }
393 
395  const char *E,
396  const LangOptions &LO,
397  const TargetInfo &Target) {
398 
399  unsigned argIndex = 0;
400 
401  // Keep looking for a %s format specifier until we have exhausted the string.
403  while (I != E) {
404  const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
405  LO, Target, false,
406  false);
407  // Did a fail-stop error of any kind occur when parsing the specifier?
408  // If so, don't do any more processing.
409  if (FSR.shouldStop())
410  return false;
411  // Did we exhaust the string or encounter an error that
412  // we can recover from?
413  if (!FSR.hasValue())
414  continue;
415  const analyze_printf::PrintfSpecifier &FS = FSR.getValue();
416  // Return true if this a %s format specifier.
417  if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::Kind::sArg)
418  return true;
419  }
420  return false;
421 }
422 
423 //===----------------------------------------------------------------------===//
424 // Methods on PrintfSpecifier.
425 //===----------------------------------------------------------------------===//
426 
427 ArgType PrintfSpecifier::getArgType(ASTContext &Ctx,
428  bool IsObjCLiteral) const {
429  const PrintfConversionSpecifier &CS = getConversionSpecifier();
430 
431  if (!CS.consumesDataArgument())
432  return ArgType::Invalid();
433 
435  switch (LM.getKind()) {
437  return Ctx.IntTy;
440  return ArgType(ArgType::WIntTy, "wint_t");
442  if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
443  return Ctx.IntTy;
444  LLVM_FALLTHROUGH;
445  default:
446  return ArgType::Invalid();
447  }
448 
449  if (CS.isIntArg())
450  switch (LM.getKind()) {
452  // GNU extension.
453  return Ctx.LongLongTy;
455  return Ctx.IntTy;
457  return ArgType(Ctx.IntTy, "__int32");
459  case LengthModifier::AsShort: return Ctx.ShortTy;
460  case LengthModifier::AsLong: return Ctx.LongTy;
463  return Ctx.LongLongTy;
465  return ArgType(Ctx.LongLongTy, "__int64");
467  return ArgType(Ctx.getIntMaxType(), "intmax_t");
469  return ArgType(Ctx.getSignedSizeType(), "ssize_t");
471  return Ctx.getTargetInfo().getTriple().isArch64Bit()
472  ? ArgType(Ctx.LongLongTy, "__int64")
473  : ArgType(Ctx.IntTy, "__int32");
475  return ArgType(Ctx.getPointerDiffType(), "ptrdiff_t");
479  return ArgType::Invalid();
480  }
481 
482  if (CS.isUIntArg())
483  switch (LM.getKind()) {
485  // GNU extension.
486  return Ctx.UnsignedLongLongTy;
488  return Ctx.UnsignedIntTy;
490  return ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
491  case LengthModifier::AsChar: return Ctx.UnsignedCharTy;
492  case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
493  case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
496  return Ctx.UnsignedLongLongTy;
498  return ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64");
500  return ArgType(Ctx.getUIntMaxType(), "uintmax_t");
502  return ArgType(Ctx.getSizeType(), "size_t");
504  return Ctx.getTargetInfo().getTriple().isArch64Bit()
505  ? ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64")
506  : ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
508  return ArgType(Ctx.getUnsignedPointerDiffType(), "unsigned ptrdiff_t");
512  return ArgType::Invalid();
513  }
514 
515  if (CS.isDoubleArg()) {
516  if (LM.getKind() == LengthModifier::AsLongDouble)
517  return Ctx.LongDoubleTy;
518  return Ctx.DoubleTy;
519  }
520 
521  if (CS.getKind() == ConversionSpecifier::nArg) {
522  switch (LM.getKind()) {
524  return ArgType::PtrTo(Ctx.IntTy);
526  return ArgType::PtrTo(Ctx.SignedCharTy);
528  return ArgType::PtrTo(Ctx.ShortTy);
530  return ArgType::PtrTo(Ctx.LongTy);
533  return ArgType::PtrTo(Ctx.LongLongTy);
535  return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
537  return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
539  return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
541  return ArgType(); // FIXME: Is this a known extension?
548  return ArgType::Invalid();
549  }
550  }
551 
552  switch (CS.getKind()) {
554  if (LM.getKind() == LengthModifier::AsWideChar) {
555  if (IsObjCLiteral)
557  "const unichar *");
558  return ArgType(ArgType::WCStrTy, "wchar_t *");
559  }
560  if (LM.getKind() == LengthModifier::AsWide)
561  return ArgType(ArgType::WCStrTy, "wchar_t *");
562  return ArgType::CStrTy;
564  if (IsObjCLiteral)
566  "const unichar *");
567  if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
568  LM.getKind() == LengthModifier::AsShort)
569  return ArgType::CStrTy;
570  return ArgType(ArgType::WCStrTy, "wchar_t *");
572  if (IsObjCLiteral)
573  return ArgType(Ctx.UnsignedShortTy, "unichar");
574  if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
575  LM.getKind() == LengthModifier::AsShort)
576  return Ctx.IntTy;
577  return ArgType(Ctx.WideCharTy, "wchar_t");
580  return ArgType::CPointerTy;
582  return ArgType::ObjCPointerTy;
583  default:
584  break;
585  }
586 
587  // FIXME: Handle other cases.
588  return ArgType();
589 }
590 
591 bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
592  ASTContext &Ctx, bool IsObjCLiteral) {
593  // %n is different from other conversion specifiers; don't try to fix it.
594  if (CS.getKind() == ConversionSpecifier::nArg)
595  return false;
596 
597  // Handle Objective-C objects first. Note that while the '%@' specifier will
598  // not warn for structure pointer or void pointer arguments (because that's
599  // how CoreFoundation objects are implemented), we only show a fixit for '%@'
600  // if we know it's an object (block, id, class, or __attribute__((NSObject))).
601  if (QT->isObjCRetainableType()) {
602  if (!IsObjCLiteral)
603  return false;
604 
606 
607  // Disable irrelevant flags
608  HasThousandsGrouping = false;
609  HasPlusPrefix = false;
610  HasSpacePrefix = false;
611  HasAlternativeForm = false;
612  HasLeadingZeroes = false;
613  Precision.setHowSpecified(OptionalAmount::NotSpecified);
614  LM.setKind(LengthModifier::None);
615 
616  return true;
617  }
618 
619  // Handle strings next (char *, wchar_t *)
620  if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
621  CS.setKind(ConversionSpecifier::sArg);
622 
623  // Disable irrelevant flags
624  HasAlternativeForm = 0;
625  HasLeadingZeroes = 0;
626 
627  // Set the long length modifier for wide characters
628  if (QT->getPointeeType()->isWideCharType())
629  LM.setKind(LengthModifier::AsWideChar);
630  else
631  LM.setKind(LengthModifier::None);
632 
633  return true;
634  }
635 
636  // If it's an enum, get its underlying type.
637  if (const EnumType *ETy = QT->getAs<EnumType>())
638  QT = ETy->getDecl()->getIntegerType();
639 
640  // We can only work with builtin types.
641  const BuiltinType *BT = QT->getAs<BuiltinType>();
642  if (!BT)
643  return false;
644 
645  // Set length modifier
646  switch (BT->getKind()) {
647  case BuiltinType::Bool:
648  case BuiltinType::WChar_U:
649  case BuiltinType::WChar_S:
650  case BuiltinType::Char16:
651  case BuiltinType::Char32:
652  case BuiltinType::UInt128:
653  case BuiltinType::Int128:
654  case BuiltinType::Half:
655  case BuiltinType::Float16:
656  case BuiltinType::Float128:
657  // Various types which are non-trivial to correct.
658  return false;
659 
660 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
661  case BuiltinType::Id:
662 #include "clang/Basic/OpenCLImageTypes.def"
663 #define SIGNED_TYPE(Id, SingletonId)
664 #define UNSIGNED_TYPE(Id, SingletonId)
665 #define FLOATING_TYPE(Id, SingletonId)
666 #define BUILTIN_TYPE(Id, SingletonId) \
667  case BuiltinType::Id:
668 #include "clang/AST/BuiltinTypes.def"
669  // Misc other stuff which doesn't make sense here.
670  return false;
671 
672  case BuiltinType::UInt:
673  case BuiltinType::Int:
674  case BuiltinType::Float:
675  case BuiltinType::Double:
676  LM.setKind(LengthModifier::None);
677  break;
678 
679  case BuiltinType::Char_U:
680  case BuiltinType::UChar:
681  case BuiltinType::Char_S:
682  case BuiltinType::SChar:
683  LM.setKind(LengthModifier::AsChar);
684  break;
685 
686  case BuiltinType::Short:
687  case BuiltinType::UShort:
688  LM.setKind(LengthModifier::AsShort);
689  break;
690 
691  case BuiltinType::Long:
692  case BuiltinType::ULong:
693  LM.setKind(LengthModifier::AsLong);
694  break;
695 
696  case BuiltinType::LongLong:
697  case BuiltinType::ULongLong:
698  LM.setKind(LengthModifier::AsLongLong);
699  break;
700 
701  case BuiltinType::LongDouble:
702  LM.setKind(LengthModifier::AsLongDouble);
703  break;
704  }
705 
706  // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
707  if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus11))
708  namedTypeToLengthModifier(QT, LM);
709 
710  // If fixing the length modifier was enough, we might be done.
711  if (hasValidLengthModifier(Ctx.getTargetInfo())) {
712  // If we're going to offer a fix anyway, make sure the sign matches.
713  switch (CS.getKind()) {
716  if (QT->isSignedIntegerType())
718  break;
722  if (QT->isUnsignedIntegerType() && !HasPlusPrefix)
724  break;
725  default:
726  // Other specifiers do not have signed/unsigned variants.
727  break;
728  }
729 
730  const analyze_printf::ArgType &ATR = getArgType(Ctx, IsObjCLiteral);
731  if (ATR.isValid() && ATR.matchesType(Ctx, QT))
732  return true;
733  }
734 
735  // Set conversion specifier and disable any flags which do not apply to it.
736  // Let typedefs to char fall through to int, as %c is silly for uint8_t.
737  if (!isa<TypedefType>(QT) && QT->isCharType()) {
738  CS.setKind(ConversionSpecifier::cArg);
739  LM.setKind(LengthModifier::None);
740  Precision.setHowSpecified(OptionalAmount::NotSpecified);
741  HasAlternativeForm = 0;
742  HasLeadingZeroes = 0;
743  HasPlusPrefix = 0;
744  }
745  // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
746  else if (QT->isRealFloatingType()) {
747  CS.setKind(ConversionSpecifier::fArg);
748  }
749  else if (QT->isSignedIntegerType()) {
750  CS.setKind(ConversionSpecifier::dArg);
751  HasAlternativeForm = 0;
752  }
753  else if (QT->isUnsignedIntegerType()) {
754  CS.setKind(ConversionSpecifier::uArg);
755  HasAlternativeForm = 0;
756  HasPlusPrefix = 0;
757  } else {
758  llvm_unreachable("Unexpected type");
759  }
760 
761  return true;
762 }
763 
764 void PrintfSpecifier::toString(raw_ostream &os) const {
765  // Whilst some features have no defined order, we are using the order
766  // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
767  os << "%";
768 
769  // Positional args
770  if (usesPositionalArg()) {
771  os << getPositionalArgIndex() << "$";
772  }
773 
774  // Conversion flags
775  if (IsLeftJustified) os << "-";
776  if (HasPlusPrefix) os << "+";
777  if (HasSpacePrefix) os << " ";
778  if (HasAlternativeForm) os << "#";
779  if (HasLeadingZeroes) os << "0";
780 
781  // Minimum field width
782  FieldWidth.toString(os);
783  // Precision
784  Precision.toString(os);
785  // Length modifier
786  os << LM.toString();
787  // Conversion specifier
788  os << CS.toString();
789 }
790 
791 bool PrintfSpecifier::hasValidPlusPrefix() const {
792  if (!HasPlusPrefix)
793  return true;
794 
795  // The plus prefix only makes sense for signed conversions
796  switch (CS.getKind()) {
810  return true;
811 
812  default:
813  return false;
814  }
815 }
816 
817 bool PrintfSpecifier::hasValidAlternativeForm() const {
818  if (!HasAlternativeForm)
819  return true;
820 
821  // Alternate form flag only valid with the oxXaAeEfFgG conversions
822  switch (CS.getKind()) {
837  return true;
838 
839  default:
840  return false;
841  }
842 }
843 
844 bool PrintfSpecifier::hasValidLeadingZeros() const {
845  if (!HasLeadingZeroes)
846  return true;
847 
848  // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
849  switch (CS.getKind()) {
869  return true;
870 
871  default:
872  return false;
873  }
874 }
875 
876 bool PrintfSpecifier::hasValidSpacePrefix() const {
877  if (!HasSpacePrefix)
878  return true;
879 
880  // The space prefix only makes sense for signed conversions
881  switch (CS.getKind()) {
895  return true;
896 
897  default:
898  return false;
899  }
900 }
901 
902 bool PrintfSpecifier::hasValidLeftJustified() const {
903  if (!IsLeftJustified)
904  return true;
905 
906  // The left justified flag is valid for all conversions except n
907  switch (CS.getKind()) {
909  return false;
910 
911  default:
912  return true;
913  }
914 }
915 
916 bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
917  if (!HasThousandsGrouping)
918  return true;
919 
920  switch (CS.getKind()) {
930  return true;
931  default:
932  return false;
933  }
934 }
935 
936 bool PrintfSpecifier::hasValidPrecision() const {
937  if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
938  return true;
939 
940  // Precision is only valid with the diouxXaAeEfFgGsP conversions
941  switch (CS.getKind()) {
963  return true;
964 
965  default:
966  return false;
967  }
968 }
969 bool PrintfSpecifier::hasValidFieldWidth() const {
970  if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
971  return true;
972 
973  // The field width is valid for all conversions except n
974  switch (CS.getKind()) {
976  return false;
977 
978  default:
979  return true;
980  }
981 }
QualType withConst() const
Retrieves a version of this type with const applied.
CanQualType LongLongTy
Definition: ASTContext.h:1004
A (possibly-)qualified type.
Definition: Type.h:653
void setIsPublic(const char *position)
Definition: FormatString.h:491
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
Kind getKind() const
Definition: Type.h:2166
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:456
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:790
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:1893
virtual void HandleInvalidObjCModifierFlag(const char *startFlag, unsigned flagLen)
Definition: FormatString.h:644
static bool ParseObjCFlags(FormatStringHandler &H, PrintfSpecifier &FS, const char *FlagBeg, const char *E, bool Warn)
StringRef P
static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS, const char *Start, const char *&Beg, const char *E, unsigned *argIndex)
bool ParseArgPosition(FormatStringHandler &H, FormatSpecifier &CS, const char *Start, const char *&Beg, const char *E)
CanQualType LongTy
Definition: ASTContext.h:1004
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:671
void setHasLeadingZeros(const char *position)
Definition: FormatString.h:484
CanQualType WideCharTy
Definition: ASTContext.h:1000
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition: Type.cpp:1840
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6307
virtual void HandleEmptyObjCModifierFlag(const char *startFlags, unsigned flagsLen)
Definition: FormatString.h:641
bool isWideCharType() const
Definition: Type.cpp:1759
void setPrecision(const OptionalAmount &Amt)
Definition: FormatString.h:500
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
Pieces specific to fprintf format strings.
Definition: FormatString.h:420
bool isCharType() const
Definition: Type.cpp:1750
void setHasSpacePrefix(const char *position)
Definition: FormatString.h:478
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
QualType getUnsignedPointerDiffType() const
Return the unique unsigned counterpart of "ptrdiff_t" integer type.
void setConversionSpecifier(const PrintfConversionSpecifier &cs)
Definition: FormatString.h:466
virtual bool HandleInvalidPrintfConversionSpecifier(const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier, unsigned specifierLen)
Definition: FormatString.h:652
MatchKind matchesType(ASTContext &C, QualType argTy) const
Represents the length modifier in a format string in scanf/printf.
Definition: FormatString.h:65
CanQualType LongDoubleTy
Definition: ASTContext.h:1007
void setIsLeftJustified(const char *position)
Definition: FormatString.h:472
CanQualType UnsignedCharTy
Definition: ASTContext.h:1005
static ArgType PtrTo(const ArgType &A)
Create an ArgType which corresponds to the type pointer to A.
Definition: FormatString.h:272
OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E, unsigned &argIndex)
Exposes information about the current target.
Definition: TargetInfo.h:54
bool isObjCRetainableType() const
Definition: Type.cpp:3824
CanQualType ShortTy
Definition: ASTContext.h:1004
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], or an enum decl which has a signed representation.
Definition: Type.cpp:1800
virtual void HandleNullChar(const char *nullCharacter)
Definition: FormatString.h:629
CanQualType SignedCharTy
Definition: ASTContext.h:1004
CanQualType getUIntMaxType() const
Return the unique type for "uintmax_t" (C99 7.18.1.5), defined in <stdint.h>.
bool ParseFormatStringHasSArg(const char *beg, const char *end, const LangOptions &LO, const TargetInfo &Target)
virtual void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, const char *flagsEnd, const char *conversionPosition)
Definition: FormatString.h:647
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums...
Definition: Type.h:4004
void setIsPrivate(const char *position)
Definition: FormatString.h:490
CanQualType getSignedSizeType() const
Return the unique signed counterpart of the integer type corresponding to size_t. ...
void setHasThousandsGrouping(const char *position)
Definition: FormatString.h:469
bool isAnyCharacterType() const
Determine whether this type is any of the built-in character types.
Definition: Type.cpp:1780
bool ParseFieldWidth(FormatStringHandler &H, FormatSpecifier &CS, const char *Start, const char *&Beg, const char *E, unsigned *argIndex)
const PrintfConversionSpecifier & getConversionSpecifier() const
Definition: FormatString.h:496
virtual bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier, unsigned specifierLen)
Definition: FormatString.h:659
CanQualType UnsignedShortTy
Definition: ASTContext.h:1005
Dataflow Directional Tag Classes.
CanQualType UnsignedLongLongTy
Definition: ASTContext.h:1006
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&#39;t go fur...
Common components of both fprintf and fscanf format strings.
Definition: FormatString.h:30
std::string toString(const til::SExpr *E)
void setHasObjCTechnicalTerm(const char *position)
Definition: FormatString.h:487
CanQualType UnsignedLongTy
Definition: ASTContext.h:1005
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...
void setHasPlusPrefix(const char *position)
Definition: FormatString.h:475
virtual void HandleIncompleteSpecifier(const char *startSpecifier, unsigned specifierLen)
Definition: FormatString.h:638
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
This class is used for builtin types like &#39;int&#39;.
Definition: Type.h:2145
Defines the clang::TargetInfo interface.
CanQualType IntTy
Definition: ASTContext.h:1004
bool ParsePrintfString(FormatStringHandler &H, const char *beg, const char *end, const LangOptions &LO, const TargetInfo &Target, bool isFreeBSDKPrintf)
bool isPointerType() const
Definition: Type.h:5944
void setHasAlternativeForm(const char *position)
Definition: FormatString.h:481
clang::analyze_format_string::SpecifierResult< PrintfSpecifier > PrintfSpecifierResult
CanQualType getIntMaxType() const
Return the unique type for "intmax_t" (C99 7.18.1.5), defined in <stdint.h>.
CanQualType DoubleTy
Definition: ASTContext.h:1007
static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H, const char *&Beg, const char *E, unsigned &argIndex, const LangOptions &LO, const TargetInfo &Target, bool Warn, bool isFreeBSDKPrintf)
CanQualType UnsignedIntTy
Definition: ASTContext.h:1005
OptionalAmount ParsePositionAmount(FormatStringHandler &H, const char *Start, const char *&Beg, const char *E, PositionContext p)
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.