clang  7.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::Char8: // FIXME: Treat like 'char'?
651  case BuiltinType::Char16:
652  case BuiltinType::Char32:
653  case BuiltinType::UInt128:
654  case BuiltinType::Int128:
655  case BuiltinType::Half:
656  case BuiltinType::Float16:
657  case BuiltinType::Float128:
658  // Various types which are non-trivial to correct.
659  return false;
660 
661 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
662  case BuiltinType::Id:
663 #include "clang/Basic/OpenCLImageTypes.def"
664 #define SIGNED_TYPE(Id, SingletonId)
665 #define UNSIGNED_TYPE(Id, SingletonId)
666 #define FLOATING_TYPE(Id, SingletonId)
667 #define BUILTIN_TYPE(Id, SingletonId) \
668  case BuiltinType::Id:
669 #include "clang/AST/BuiltinTypes.def"
670  // Misc other stuff which doesn't make sense here.
671  return false;
672 
673  case BuiltinType::UInt:
674  case BuiltinType::Int:
675  case BuiltinType::Float:
676  case BuiltinType::Double:
677  LM.setKind(LengthModifier::None);
678  break;
679 
680  case BuiltinType::Char_U:
681  case BuiltinType::UChar:
682  case BuiltinType::Char_S:
683  case BuiltinType::SChar:
684  LM.setKind(LengthModifier::AsChar);
685  break;
686 
687  case BuiltinType::Short:
688  case BuiltinType::UShort:
689  LM.setKind(LengthModifier::AsShort);
690  break;
691 
692  case BuiltinType::Long:
693  case BuiltinType::ULong:
694  LM.setKind(LengthModifier::AsLong);
695  break;
696 
697  case BuiltinType::LongLong:
698  case BuiltinType::ULongLong:
699  LM.setKind(LengthModifier::AsLongLong);
700  break;
701 
702  case BuiltinType::LongDouble:
703  LM.setKind(LengthModifier::AsLongDouble);
704  break;
705  }
706 
707  // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
708  if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus11))
709  namedTypeToLengthModifier(QT, LM);
710 
711  // If fixing the length modifier was enough, we might be done.
712  if (hasValidLengthModifier(Ctx.getTargetInfo())) {
713  // If we're going to offer a fix anyway, make sure the sign matches.
714  switch (CS.getKind()) {
717  if (QT->isSignedIntegerType())
719  break;
723  if (QT->isUnsignedIntegerType() && !HasPlusPrefix)
725  break;
726  default:
727  // Other specifiers do not have signed/unsigned variants.
728  break;
729  }
730 
731  const analyze_printf::ArgType &ATR = getArgType(Ctx, IsObjCLiteral);
732  if (ATR.isValid() && ATR.matchesType(Ctx, QT))
733  return true;
734  }
735 
736  // Set conversion specifier and disable any flags which do not apply to it.
737  // Let typedefs to char fall through to int, as %c is silly for uint8_t.
738  if (!isa<TypedefType>(QT) && QT->isCharType()) {
739  CS.setKind(ConversionSpecifier::cArg);
740  LM.setKind(LengthModifier::None);
741  Precision.setHowSpecified(OptionalAmount::NotSpecified);
742  HasAlternativeForm = 0;
743  HasLeadingZeroes = 0;
744  HasPlusPrefix = 0;
745  }
746  // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
747  else if (QT->isRealFloatingType()) {
748  CS.setKind(ConversionSpecifier::fArg);
749  }
750  else if (QT->isSignedIntegerType()) {
751  CS.setKind(ConversionSpecifier::dArg);
752  HasAlternativeForm = 0;
753  }
754  else if (QT->isUnsignedIntegerType()) {
755  CS.setKind(ConversionSpecifier::uArg);
756  HasAlternativeForm = 0;
757  HasPlusPrefix = 0;
758  } else {
759  llvm_unreachable("Unexpected type");
760  }
761 
762  return true;
763 }
764 
765 void PrintfSpecifier::toString(raw_ostream &os) const {
766  // Whilst some features have no defined order, we are using the order
767  // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
768  os << "%";
769 
770  // Positional args
771  if (usesPositionalArg()) {
772  os << getPositionalArgIndex() << "$";
773  }
774 
775  // Conversion flags
776  if (IsLeftJustified) os << "-";
777  if (HasPlusPrefix) os << "+";
778  if (HasSpacePrefix) os << " ";
779  if (HasAlternativeForm) os << "#";
780  if (HasLeadingZeroes) os << "0";
781 
782  // Minimum field width
783  FieldWidth.toString(os);
784  // Precision
785  Precision.toString(os);
786  // Length modifier
787  os << LM.toString();
788  // Conversion specifier
789  os << CS.toString();
790 }
791 
792 bool PrintfSpecifier::hasValidPlusPrefix() const {
793  if (!HasPlusPrefix)
794  return true;
795 
796  // The plus prefix only makes sense for signed conversions
797  switch (CS.getKind()) {
811  return true;
812 
813  default:
814  return false;
815  }
816 }
817 
818 bool PrintfSpecifier::hasValidAlternativeForm() const {
819  if (!HasAlternativeForm)
820  return true;
821 
822  // Alternate form flag only valid with the oxXaAeEfFgG conversions
823  switch (CS.getKind()) {
838  return true;
839 
840  default:
841  return false;
842  }
843 }
844 
845 bool PrintfSpecifier::hasValidLeadingZeros() const {
846  if (!HasLeadingZeroes)
847  return true;
848 
849  // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
850  switch (CS.getKind()) {
870  return true;
871 
872  default:
873  return false;
874  }
875 }
876 
877 bool PrintfSpecifier::hasValidSpacePrefix() const {
878  if (!HasSpacePrefix)
879  return true;
880 
881  // The space prefix only makes sense for signed conversions
882  switch (CS.getKind()) {
896  return true;
897 
898  default:
899  return false;
900  }
901 }
902 
903 bool PrintfSpecifier::hasValidLeftJustified() const {
904  if (!IsLeftJustified)
905  return true;
906 
907  // The left justified flag is valid for all conversions except n
908  switch (CS.getKind()) {
910  return false;
911 
912  default:
913  return true;
914  }
915 }
916 
917 bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
918  if (!HasThousandsGrouping)
919  return true;
920 
921  switch (CS.getKind()) {
931  return true;
932  default:
933  return false;
934  }
935 }
936 
937 bool PrintfSpecifier::hasValidPrecision() const {
938  if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
939  return true;
940 
941  // Precision is only valid with the diouxXaAeEfFgGsP conversions
942  switch (CS.getKind()) {
964  return true;
965 
966  default:
967  return false;
968  }
969 }
970 bool PrintfSpecifier::hasValidFieldWidth() const {
971  if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
972  return true;
973 
974  // The field width is valid for all conversions except n
975  switch (CS.getKind()) {
977  return false;
978 
979  default:
980  return true;
981  }
982 }
QualType withConst() const
Retrieves a version of this type with const applied.
CanQualType LongLongTy
Definition: ASTContext.h:1006
A (possibly-)qualified type.
Definition: Type.h:654
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:2235
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:460
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:801
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:1900
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:1006
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:672
void setHasLeadingZeros(const char *position)
Definition: FormatString.h:484
CanQualType WideCharTy
Definition: ASTContext.h:1001
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:1847
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6397
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:150
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:50
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:1009
void setIsLeftJustified(const char *position)
Definition: FormatString.h:472
CanQualType UnsignedCharTy
Definition: ASTContext.h:1007
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:3846
CanQualType ShortTy
Definition: ASTContext.h:1006
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:1807
virtual void HandleNullChar(const char *nullCharacter)
Definition: FormatString.h:629
CanQualType SignedCharTy
Definition: ASTContext.h:1006
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:4077
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:1786
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:1007
Dataflow Directional Tag Classes.
CanQualType UnsignedLongLongTy
Definition: ASTContext.h:1008
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:1007
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:2214
Defines the clang::TargetInfo interface.
CanQualType IntTy
Definition: ASTContext.h:1006
bool ParsePrintfString(FormatStringHandler &H, const char *beg, const char *end, const LangOptions &LO, const TargetInfo &Target, bool isFreeBSDKPrintf)
bool isPointerType() const
Definition: Type.h:6028
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:1009
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:1007
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>.