blob: caceeac113bf7ece4682d02f1c1d372046c5f9cd [file] [log] [blame]
Ted Kremenek826a3452010-07-16 02:11:22 +00001// 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"
Hans Wennborgd02deeb2011-12-15 10:25:47 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenek826a3452010-07-16 02:11:22 +000017
18using clang::analyze_format_string::ArgTypeResult;
19using clang::analyze_format_string::FormatStringHandler;
20using clang::analyze_format_string::FormatSpecifier;
21using clang::analyze_format_string::LengthModifier;
22using clang::analyze_format_string::OptionalAmount;
23using clang::analyze_format_string::PositionContext;
Ted Kremeneka412a492010-07-20 20:04:42 +000024using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek826a3452010-07-16 02:11:22 +000025using namespace clang;
26
27// Key function to FormatStringHandler.
28FormatStringHandler::~FormatStringHandler() {}
29
30//===----------------------------------------------------------------------===//
31// Functions for parsing format strings components in both printf and
32// scanf format strings.
33//===----------------------------------------------------------------------===//
34
35OptionalAmount
36clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
37 const char *I = Beg;
38 UpdateOnReturn <const char*> UpdateBeg(Beg, I);
39
40 unsigned accumulator = 0;
41 bool hasDigits = false;
42
43 for ( ; I != E; ++I) {
44 char c = *I;
45 if (c >= '0' && c <= '9') {
46 hasDigits = true;
47 accumulator = (accumulator * 10) + (c - '0');
48 continue;
49 }
50
51 if (hasDigits)
52 return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
53 false);
54
55 break;
56 }
57
58 return OptionalAmount();
59}
60
61OptionalAmount
62clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
63 const char *E,
64 unsigned &argIndex) {
65 if (*Beg == '*') {
66 ++Beg;
67 return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
68 }
69
70 return ParseAmount(Beg, E);
71}
72
73OptionalAmount
74clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
75 const char *Start,
76 const char *&Beg,
77 const char *E,
78 PositionContext p) {
79 if (*Beg == '*') {
80 const char *I = Beg + 1;
81 const OptionalAmount &Amt = ParseAmount(I, E);
82
83 if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
84 H.HandleInvalidPosition(Beg, I - Beg, p);
85 return OptionalAmount(false);
86 }
87
88 if (I == E) {
89 // No more characters left?
90 H.HandleIncompleteSpecifier(Start, E - Start);
91 return OptionalAmount(false);
92 }
93
94 assert(Amt.getHowSpecified() == OptionalAmount::Constant);
95
96 if (*I == '$') {
97 // Handle positional arguments
98
99 // Special case: '*0$', since this is an easy mistake.
100 if (Amt.getConstantAmount() == 0) {
101 H.HandleZeroPosition(Beg, I - Beg + 1);
102 return OptionalAmount(false);
103 }
104
105 const char *Tmp = Beg;
106 Beg = ++I;
107
108 return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
109 Tmp, 0, true);
110 }
111
112 H.HandleInvalidPosition(Beg, I - Beg, p);
113 return OptionalAmount(false);
114 }
115
116 return ParseAmount(Beg, E);
117}
118
119
120bool
121clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
122 FormatSpecifier &CS,
123 const char *Start,
124 const char *&Beg, const char *E,
125 unsigned *argIndex) {
126 // FIXME: Support negative field widths.
127 if (argIndex) {
128 CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
129 }
130 else {
131 const OptionalAmount Amt =
132 ParsePositionAmount(H, Start, Beg, E,
133 analyze_format_string::FieldWidthPos);
134
135 if (Amt.isInvalid())
136 return true;
137 CS.setFieldWidth(Amt);
138 }
139 return false;
140}
141
142bool
143clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
144 FormatSpecifier &FS,
145 const char *Start,
146 const char *&Beg,
147 const char *E) {
148 const char *I = Beg;
149
150 const OptionalAmount &Amt = ParseAmount(I, E);
151
152 if (I == E) {
153 // No more characters left?
154 H.HandleIncompleteSpecifier(Start, E - Start);
155 return true;
156 }
157
158 if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
Hans Wennborgf8562642012-03-09 10:10:54 +0000159 // Warn that positional arguments are non-standard.
160 H.HandlePosition(Start, I - Start);
161
Ted Kremenek826a3452010-07-16 02:11:22 +0000162 // Special case: '%0$', since this is an easy mistake.
163 if (Amt.getConstantAmount() == 0) {
164 H.HandleZeroPosition(Start, I - Start);
165 return true;
166 }
167
168 FS.setArgIndex(Amt.getConstantAmount() - 1);
169 FS.setUsesPositionalArg();
170 // Update the caller's pointer if we decided to consume
171 // these characters.
172 Beg = I;
173 return false;
174 }
175
176 return false;
177}
178
179bool
180clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
181 const char *&I,
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000182 const char *E,
183 const LangOptions &LO,
184 bool IsScanf) {
Ted Kremenek826a3452010-07-16 02:11:22 +0000185 LengthModifier::Kind lmKind = LengthModifier::None;
186 const char *lmPosition = I;
187 switch (*I) {
188 default:
189 return false;
190 case 'h':
191 ++I;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000192 lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
193 : LengthModifier::AsShort;
Ted Kremenek826a3452010-07-16 02:11:22 +0000194 break;
195 case 'l':
196 ++I;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000197 lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
198 : LengthModifier::AsLong;
Ted Kremenek826a3452010-07-16 02:11:22 +0000199 break;
200 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
201 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
202 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
203 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
Hans Wennborg32addd52012-02-16 16:34:54 +0000204 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000205 case 'a':
Hans Wennborg5294c792011-12-28 13:10:50 +0000206 if (IsScanf && !LO.C99 && !LO.CPlusPlus0x) {
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000207 // For scanf in C90, look at the next character to see if this should
208 // be parsed as the GNU extension 'a' length modifier. If not, this
209 // will be parsed as a conversion specifier.
210 ++I;
211 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
212 lmKind = LengthModifier::AsAllocate;
213 break;
214 }
215 --I;
216 }
217 return false;
Hans Wennborg37969b72012-01-12 17:11:12 +0000218 case 'm':
219 if (IsScanf) {
220 lmKind = LengthModifier::AsMAllocate;
221 ++I;
222 break;
223 }
224 return false;
Ted Kremenek826a3452010-07-16 02:11:22 +0000225 }
226 LengthModifier lm(lmPosition, lmKind);
227 FS.setLengthModifier(lm);
228 return true;
229}
230
231//===----------------------------------------------------------------------===//
232// Methods on ArgTypeResult.
233//===----------------------------------------------------------------------===//
234
235bool ArgTypeResult::matchesType(ASTContext &C, QualType argTy) const {
236 switch (K) {
237 case InvalidTy:
David Blaikieb219cfc2011-09-23 05:06:16 +0000238 llvm_unreachable("ArgTypeResult must be valid");
Ted Kremenek826a3452010-07-16 02:11:22 +0000239
240 case UnknownTy:
241 return true;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000242
243 case AnyCharTy: {
Jordan Roseee0259d2012-06-04 22:48:57 +0000244 if (const EnumType *ETy = argTy->getAs<EnumType>())
245 argTy = ETy->getDecl()->getIntegerType();
246
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000247 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
248 switch (BT->getKind()) {
249 default:
250 break;
251 case BuiltinType::Char_S:
252 case BuiltinType::SChar:
253 case BuiltinType::UChar:
254 case BuiltinType::Char_U:
255 return true;
256 }
257 return false;
258 }
259
Ted Kremenek826a3452010-07-16 02:11:22 +0000260 case SpecificTy: {
Jordan Roseee0259d2012-06-04 22:48:57 +0000261 if (const EnumType *ETy = argTy->getAs<EnumType>())
262 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000263 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Roseee0259d2012-06-04 22:48:57 +0000264
Nick Lewycky687b5df2011-12-02 23:21:43 +0000265 if (T == argTy)
Ted Kremenek826a3452010-07-16 02:11:22 +0000266 return true;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000267 // Check for "compatible types".
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000268 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek826a3452010-07-16 02:11:22 +0000269 switch (BT->getKind()) {
270 default:
271 break;
272 case BuiltinType::Char_S:
273 case BuiltinType::SChar:
Ted Kremenekdc00d812011-07-13 17:35:14 +0000274 case BuiltinType::Char_U:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000275 case BuiltinType::UChar:
Hans Wennborg101d4e02012-05-08 17:21:31 +0000276 return T == C.UnsignedCharTy || T == C.SignedCharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000277 case BuiltinType::Short:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000278 return T == C.UnsignedShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000279 case BuiltinType::UShort:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000280 return T == C.ShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000281 case BuiltinType::Int:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000282 return T == C.UnsignedIntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000283 case BuiltinType::UInt:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000284 return T == C.IntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000285 case BuiltinType::Long:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000286 return T == C.UnsignedLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000287 case BuiltinType::ULong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000288 return T == C.LongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000289 case BuiltinType::LongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000290 return T == C.UnsignedLongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000291 case BuiltinType::ULongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000292 return T == C.LongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000293 }
294 return false;
295 }
296
297 case CStrTy: {
298 const PointerType *PT = argTy->getAs<PointerType>();
299 if (!PT)
300 return false;
301 QualType pointeeTy = PT->getPointeeType();
302 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
303 switch (BT->getKind()) {
304 case BuiltinType::Void:
305 case BuiltinType::Char_U:
306 case BuiltinType::UChar:
307 case BuiltinType::Char_S:
308 case BuiltinType::SChar:
309 return true;
310 default:
311 break;
312 }
313
314 return false;
315 }
316
317 case WCStrTy: {
318 const PointerType *PT = argTy->getAs<PointerType>();
319 if (!PT)
320 return false;
321 QualType pointeeTy =
322 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
323 return pointeeTy == C.getWCharType();
324 }
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000325
326 case WIntTy: {
James Molloy392da482012-05-04 10:55:22 +0000327
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000328 QualType PromoArg =
329 argTy->isPromotableIntegerType()
330 ? C.getPromotedIntegerType(argTy) : argTy;
331
James Molloy392da482012-05-04 10:55:22 +0000332 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000333 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
334
James Molloy392da482012-05-04 10:55:22 +0000335 // If the promoted argument is the corresponding signed type of the
336 // wint_t type, then it should match.
337 if (PromoArg->hasSignedIntegerRepresentation() &&
338 C.getCorrespondingUnsignedType(PromoArg) == WInt)
339 return true;
340
341 return WInt == PromoArg;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000342 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000343
344 case CPointerTy:
Anders Carlsson62425992010-11-06 14:58:53 +0000345 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
Ted Kremenekafcd1952012-03-15 21:22:27 +0000346 argTy->isBlockPointerType() || argTy->isNullPtrType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000347
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000348 case ObjCPointerTy: {
349 if (argTy->getAs<ObjCObjectPointerType>() ||
350 argTy->getAs<BlockPointerType>())
351 return true;
352
353 // Handle implicit toll-free bridging.
354 if (const PointerType *PT = argTy->getAs<PointerType>()) {
355 // Things such as CFTypeRef are really just opaque pointers
356 // to C structs representing CF types that can often be bridged
357 // to Objective-C objects. Since the compiler doesn't know which
358 // structs can be toll-free bridged, we just accept them all.
359 QualType pointee = PT->getPointeeType();
360 if (pointee->getAsStructureType() || pointee->isVoidType())
361 return true;
362 }
363 return false;
364 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000365 }
366
David Blaikie30263482012-01-20 21:50:17 +0000367 llvm_unreachable("Invalid ArgTypeResult Kind!");
Ted Kremenek826a3452010-07-16 02:11:22 +0000368}
369
370QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
371 switch (K) {
372 case InvalidTy:
David Blaikieb219cfc2011-09-23 05:06:16 +0000373 llvm_unreachable("No representative type for Invalid ArgTypeResult");
Ted Kremenek826a3452010-07-16 02:11:22 +0000374 case UnknownTy:
375 return QualType();
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000376 case AnyCharTy:
377 return C.CharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000378 case SpecificTy:
379 return T;
380 case CStrTy:
381 return C.getPointerType(C.CharTy);
382 case WCStrTy:
383 return C.getPointerType(C.getWCharType());
384 case ObjCPointerTy:
385 return C.ObjCBuiltinIdTy;
386 case CPointerTy:
387 return C.VoidPtrTy;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000388 case WIntTy: {
James Molloy392da482012-05-04 10:55:22 +0000389 return C.getWIntType();
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000390 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000391 }
392
David Blaikie30263482012-01-20 21:50:17 +0000393 llvm_unreachable("Invalid ArgTypeResult Kind!");
Ted Kremenek826a3452010-07-16 02:11:22 +0000394}
395
Hans Wennborga792aff2011-12-07 10:33:11 +0000396std::string ArgTypeResult::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000397 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborg7da1f462012-01-31 14:59:59 +0000398 if (Name && S != Name)
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000399 return std::string("'") + Name + "' (aka '" + S + "')";
400 return std::string("'") + S + "'";
Hans Wennborga792aff2011-12-07 10:33:11 +0000401}
402
403
Ted Kremenek826a3452010-07-16 02:11:22 +0000404//===----------------------------------------------------------------------===//
405// Methods on OptionalAmount.
406//===----------------------------------------------------------------------===//
407
408ArgTypeResult
409analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
410 return Ctx.IntTy;
411}
412
413//===----------------------------------------------------------------------===//
414// Methods on LengthModifier.
415//===----------------------------------------------------------------------===//
416
417const char *
418analyze_format_string::LengthModifier::toString() const {
419 switch (kind) {
420 case AsChar:
421 return "hh";
422 case AsShort:
423 return "h";
424 case AsLong: // or AsWideChar
425 return "l";
426 case AsLongLong:
427 return "ll";
Hans Wennborg32addd52012-02-16 16:34:54 +0000428 case AsQuad:
429 return "q";
Ted Kremenek826a3452010-07-16 02:11:22 +0000430 case AsIntMax:
431 return "j";
432 case AsSizeT:
433 return "z";
434 case AsPtrDiff:
435 return "t";
436 case AsLongDouble:
437 return "L";
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000438 case AsAllocate:
439 return "a";
Hans Wennborg37969b72012-01-12 17:11:12 +0000440 case AsMAllocate:
441 return "m";
Ted Kremenek826a3452010-07-16 02:11:22 +0000442 case None:
443 return "";
444 }
445 return NULL;
446}
447
448//===----------------------------------------------------------------------===//
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000449// Methods on ConversionSpecifier.
450//===----------------------------------------------------------------------===//
451
452const char *ConversionSpecifier::toString() const {
453 switch (kind) {
454 case dArg: return "d";
455 case iArg: return "i";
456 case oArg: return "o";
457 case uArg: return "u";
458 case xArg: return "x";
459 case XArg: return "X";
460 case fArg: return "f";
461 case FArg: return "F";
462 case eArg: return "e";
463 case EArg: return "E";
464 case gArg: return "g";
465 case GArg: return "G";
466 case aArg: return "a";
467 case AArg: return "A";
468 case cArg: return "c";
469 case sArg: return "s";
470 case pArg: return "p";
471 case nArg: return "n";
472 case PercentArg: return "%";
473 case ScanListArg: return "[";
474 case InvalidSpecifier: return NULL;
475
476 // MacOS X unicode extensions.
477 case CArg: return "C";
478 case SArg: return "S";
479
480 // Objective-C specific specifiers.
481 case ObjCObjArg: return "@";
482
483 // GlibC specific specifiers.
484 case PrintErrno: return "m";
485 }
486 return NULL;
487}
488
489//===----------------------------------------------------------------------===//
Ted Kremenek826a3452010-07-16 02:11:22 +0000490// Methods on OptionalAmount.
491//===----------------------------------------------------------------------===//
492
Chris Lattner5f9e2722011-07-23 10:55:15 +0000493void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000494 switch (hs) {
495 case Invalid:
496 case NotSpecified:
497 return;
498 case Arg:
499 if (UsesDotPrefix)
500 os << ".";
501 if (usesPositionalArg())
502 os << "*" << getPositionalArgIndex() << "$";
503 else
504 os << "*";
505 break;
506 case Constant:
507 if (UsesDotPrefix)
508 os << ".";
509 os << amt;
510 break;
511 }
512}
513
Ted Kremeneka412a492010-07-20 20:04:42 +0000514bool FormatSpecifier::hasValidLengthModifier() const {
515 switch (LM.getKind()) {
516 case LengthModifier::None:
517 return true;
518
Hans Wennborg32addd52012-02-16 16:34:54 +0000519 // Handle most integer flags
Ted Kremeneka412a492010-07-20 20:04:42 +0000520 case LengthModifier::AsChar:
521 case LengthModifier::AsShort:
522 case LengthModifier::AsLongLong:
Hans Wennborg32addd52012-02-16 16:34:54 +0000523 case LengthModifier::AsQuad:
Ted Kremeneka412a492010-07-20 20:04:42 +0000524 case LengthModifier::AsIntMax:
525 case LengthModifier::AsSizeT:
526 case LengthModifier::AsPtrDiff:
527 switch (CS.getKind()) {
528 case ConversionSpecifier::dArg:
529 case ConversionSpecifier::iArg:
530 case ConversionSpecifier::oArg:
531 case ConversionSpecifier::uArg:
532 case ConversionSpecifier::xArg:
533 case ConversionSpecifier::XArg:
534 case ConversionSpecifier::nArg:
535 return true;
536 default:
537 return false;
538 }
539
Hans Wennborg32addd52012-02-16 16:34:54 +0000540 // Handle 'l' flag
Ted Kremeneka412a492010-07-20 20:04:42 +0000541 case LengthModifier::AsLong:
542 switch (CS.getKind()) {
543 case ConversionSpecifier::dArg:
544 case ConversionSpecifier::iArg:
545 case ConversionSpecifier::oArg:
546 case ConversionSpecifier::uArg:
547 case ConversionSpecifier::xArg:
548 case ConversionSpecifier::XArg:
549 case ConversionSpecifier::aArg:
550 case ConversionSpecifier::AArg:
551 case ConversionSpecifier::fArg:
552 case ConversionSpecifier::FArg:
553 case ConversionSpecifier::eArg:
554 case ConversionSpecifier::EArg:
555 case ConversionSpecifier::gArg:
556 case ConversionSpecifier::GArg:
557 case ConversionSpecifier::nArg:
558 case ConversionSpecifier::cArg:
559 case ConversionSpecifier::sArg:
Ted Kremenekef1440b2012-01-20 22:11:52 +0000560 case ConversionSpecifier::ScanListArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000561 return true;
562 default:
563 return false;
564 }
565
566 case LengthModifier::AsLongDouble:
567 switch (CS.getKind()) {
568 case ConversionSpecifier::aArg:
569 case ConversionSpecifier::AArg:
570 case ConversionSpecifier::fArg:
571 case ConversionSpecifier::FArg:
572 case ConversionSpecifier::eArg:
573 case ConversionSpecifier::EArg:
574 case ConversionSpecifier::gArg:
575 case ConversionSpecifier::GArg:
576 return true;
Ted Kremenek9d24c2c2012-01-24 21:29:54 +0000577 // GNU extension.
578 case ConversionSpecifier::dArg:
579 case ConversionSpecifier::iArg:
580 case ConversionSpecifier::oArg:
581 case ConversionSpecifier::uArg:
582 case ConversionSpecifier::xArg:
583 case ConversionSpecifier::XArg:
584 return true;
Ted Kremeneka412a492010-07-20 20:04:42 +0000585 default:
586 return false;
587 }
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000588
589 case LengthModifier::AsAllocate:
590 switch (CS.getKind()) {
591 case ConversionSpecifier::sArg:
592 case ConversionSpecifier::SArg:
Hans Wennborg28058d12012-01-12 15:07:16 +0000593 case ConversionSpecifier::ScanListArg:
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000594 return true;
595 default:
596 return false;
597 }
Hans Wennborg37969b72012-01-12 17:11:12 +0000598
599 case LengthModifier::AsMAllocate:
600 switch (CS.getKind()) {
601 case ConversionSpecifier::cArg:
602 case ConversionSpecifier::CArg:
603 case ConversionSpecifier::sArg:
604 case ConversionSpecifier::SArg:
605 case ConversionSpecifier::ScanListArg:
606 return true;
607 default:
608 return false;
609 }
Ted Kremeneka412a492010-07-20 20:04:42 +0000610 }
David Blaikie30263482012-01-20 21:50:17 +0000611 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremeneka412a492010-07-20 20:04:42 +0000612}
Hans Wennborg76517422012-02-22 10:17:01 +0000613
614bool FormatSpecifier::hasStandardLengthModifier() const {
615 switch (LM.getKind()) {
616 case LengthModifier::None:
617 case LengthModifier::AsChar:
618 case LengthModifier::AsShort:
619 case LengthModifier::AsLong:
620 case LengthModifier::AsLongLong:
621 case LengthModifier::AsIntMax:
622 case LengthModifier::AsSizeT:
623 case LengthModifier::AsPtrDiff:
624 case LengthModifier::AsLongDouble:
625 return true;
626 case LengthModifier::AsAllocate:
627 case LengthModifier::AsMAllocate:
628 case LengthModifier::AsQuad:
629 return false;
630 }
631 llvm_unreachable("Invalid LengthModifier Kind!");
632}
633
634bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
635 switch (CS.getKind()) {
636 case ConversionSpecifier::cArg:
637 case ConversionSpecifier::dArg:
638 case ConversionSpecifier::iArg:
639 case ConversionSpecifier::oArg:
640 case ConversionSpecifier::uArg:
641 case ConversionSpecifier::xArg:
642 case ConversionSpecifier::XArg:
643 case ConversionSpecifier::fArg:
644 case ConversionSpecifier::FArg:
645 case ConversionSpecifier::eArg:
646 case ConversionSpecifier::EArg:
647 case ConversionSpecifier::gArg:
648 case ConversionSpecifier::GArg:
649 case ConversionSpecifier::aArg:
650 case ConversionSpecifier::AArg:
651 case ConversionSpecifier::sArg:
652 case ConversionSpecifier::pArg:
653 case ConversionSpecifier::nArg:
654 case ConversionSpecifier::ObjCObjArg:
655 case ConversionSpecifier::ScanListArg:
656 case ConversionSpecifier::PercentArg:
657 return true;
658 case ConversionSpecifier::CArg:
659 case ConversionSpecifier::SArg:
660 return LangOpt.ObjC1 || LangOpt.ObjC2;
661 case ConversionSpecifier::InvalidSpecifier:
662 case ConversionSpecifier::PrintErrno:
663 return false;
664 }
665 llvm_unreachable("Invalid ConversionSpecifier Kind!");
666}
667
668bool FormatSpecifier::hasStandardLengthConversionCombination() const {
669 if (LM.getKind() == LengthModifier::AsLongDouble) {
670 switch(CS.getKind()) {
671 case ConversionSpecifier::dArg:
672 case ConversionSpecifier::iArg:
673 case ConversionSpecifier::oArg:
674 case ConversionSpecifier::uArg:
675 case ConversionSpecifier::xArg:
676 case ConversionSpecifier::XArg:
677 return false;
678 default:
679 return true;
680 }
681 }
682 return true;
683}