blob: f776c89cc5610b3ee5a59cca9cdd4b8e35a48ecf [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: {
244 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
245 switch (BT->getKind()) {
246 default:
247 break;
248 case BuiltinType::Char_S:
249 case BuiltinType::SChar:
250 case BuiltinType::UChar:
251 case BuiltinType::Char_U:
252 return true;
253 }
254 return false;
255 }
256
Ted Kremenek826a3452010-07-16 02:11:22 +0000257 case SpecificTy: {
258 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Nick Lewycky687b5df2011-12-02 23:21:43 +0000259 if (T == argTy)
Ted Kremenek826a3452010-07-16 02:11:22 +0000260 return true;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000261 // Check for "compatible types".
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000262 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek826a3452010-07-16 02:11:22 +0000263 switch (BT->getKind()) {
264 default:
265 break;
266 case BuiltinType::Char_S:
267 case BuiltinType::SChar:
Ted Kremenekdc00d812011-07-13 17:35:14 +0000268 case BuiltinType::Char_U:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000269 case BuiltinType::UChar:
Hans Wennborg101d4e02012-05-08 17:21:31 +0000270 return T == C.UnsignedCharTy || T == C.SignedCharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000271 case BuiltinType::Short:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000272 return T == C.UnsignedShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000273 case BuiltinType::UShort:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000274 return T == C.ShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000275 case BuiltinType::Int:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000276 return T == C.UnsignedIntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000277 case BuiltinType::UInt:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000278 return T == C.IntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000279 case BuiltinType::Long:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000280 return T == C.UnsignedLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000281 case BuiltinType::ULong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000282 return T == C.LongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000283 case BuiltinType::LongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000284 return T == C.UnsignedLongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000285 case BuiltinType::ULongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000286 return T == C.LongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000287 }
288 return false;
289 }
290
291 case CStrTy: {
292 const PointerType *PT = argTy->getAs<PointerType>();
293 if (!PT)
294 return false;
295 QualType pointeeTy = PT->getPointeeType();
296 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
297 switch (BT->getKind()) {
298 case BuiltinType::Void:
299 case BuiltinType::Char_U:
300 case BuiltinType::UChar:
301 case BuiltinType::Char_S:
302 case BuiltinType::SChar:
303 return true;
304 default:
305 break;
306 }
307
308 return false;
309 }
310
311 case WCStrTy: {
312 const PointerType *PT = argTy->getAs<PointerType>();
313 if (!PT)
314 return false;
315 QualType pointeeTy =
316 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
317 return pointeeTy == C.getWCharType();
318 }
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000319
320 case WIntTy: {
James Molloy392da482012-05-04 10:55:22 +0000321
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000322 QualType PromoArg =
323 argTy->isPromotableIntegerType()
324 ? C.getPromotedIntegerType(argTy) : argTy;
325
James Molloy392da482012-05-04 10:55:22 +0000326 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000327 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
328
James Molloy392da482012-05-04 10:55:22 +0000329 // If the promoted argument is the corresponding signed type of the
330 // wint_t type, then it should match.
331 if (PromoArg->hasSignedIntegerRepresentation() &&
332 C.getCorrespondingUnsignedType(PromoArg) == WInt)
333 return true;
334
335 return WInt == PromoArg;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000336 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000337
338 case CPointerTy:
Anders Carlsson62425992010-11-06 14:58:53 +0000339 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
Ted Kremenekafcd1952012-03-15 21:22:27 +0000340 argTy->isBlockPointerType() || argTy->isNullPtrType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000341
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000342 case ObjCPointerTy: {
343 if (argTy->getAs<ObjCObjectPointerType>() ||
344 argTy->getAs<BlockPointerType>())
345 return true;
346
347 // Handle implicit toll-free bridging.
348 if (const PointerType *PT = argTy->getAs<PointerType>()) {
349 // Things such as CFTypeRef are really just opaque pointers
350 // to C structs representing CF types that can often be bridged
351 // to Objective-C objects. Since the compiler doesn't know which
352 // structs can be toll-free bridged, we just accept them all.
353 QualType pointee = PT->getPointeeType();
354 if (pointee->getAsStructureType() || pointee->isVoidType())
355 return true;
356 }
357 return false;
358 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000359 }
360
David Blaikie30263482012-01-20 21:50:17 +0000361 llvm_unreachable("Invalid ArgTypeResult Kind!");
Ted Kremenek826a3452010-07-16 02:11:22 +0000362}
363
364QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
365 switch (K) {
366 case InvalidTy:
David Blaikieb219cfc2011-09-23 05:06:16 +0000367 llvm_unreachable("No representative type for Invalid ArgTypeResult");
Ted Kremenek826a3452010-07-16 02:11:22 +0000368 case UnknownTy:
369 return QualType();
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000370 case AnyCharTy:
371 return C.CharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000372 case SpecificTy:
373 return T;
374 case CStrTy:
375 return C.getPointerType(C.CharTy);
376 case WCStrTy:
377 return C.getPointerType(C.getWCharType());
378 case ObjCPointerTy:
379 return C.ObjCBuiltinIdTy;
380 case CPointerTy:
381 return C.VoidPtrTy;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000382 case WIntTy: {
James Molloy392da482012-05-04 10:55:22 +0000383 return C.getWIntType();
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000384 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000385 }
386
David Blaikie30263482012-01-20 21:50:17 +0000387 llvm_unreachable("Invalid ArgTypeResult Kind!");
Ted Kremenek826a3452010-07-16 02:11:22 +0000388}
389
Hans Wennborga792aff2011-12-07 10:33:11 +0000390std::string ArgTypeResult::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000391 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborg7da1f462012-01-31 14:59:59 +0000392 if (Name && S != Name)
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000393 return std::string("'") + Name + "' (aka '" + S + "')";
394 return std::string("'") + S + "'";
Hans Wennborga792aff2011-12-07 10:33:11 +0000395}
396
397
Ted Kremenek826a3452010-07-16 02:11:22 +0000398//===----------------------------------------------------------------------===//
399// Methods on OptionalAmount.
400//===----------------------------------------------------------------------===//
401
402ArgTypeResult
403analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
404 return Ctx.IntTy;
405}
406
407//===----------------------------------------------------------------------===//
408// Methods on LengthModifier.
409//===----------------------------------------------------------------------===//
410
411const char *
412analyze_format_string::LengthModifier::toString() const {
413 switch (kind) {
414 case AsChar:
415 return "hh";
416 case AsShort:
417 return "h";
418 case AsLong: // or AsWideChar
419 return "l";
420 case AsLongLong:
421 return "ll";
Hans Wennborg32addd52012-02-16 16:34:54 +0000422 case AsQuad:
423 return "q";
Ted Kremenek826a3452010-07-16 02:11:22 +0000424 case AsIntMax:
425 return "j";
426 case AsSizeT:
427 return "z";
428 case AsPtrDiff:
429 return "t";
430 case AsLongDouble:
431 return "L";
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000432 case AsAllocate:
433 return "a";
Hans Wennborg37969b72012-01-12 17:11:12 +0000434 case AsMAllocate:
435 return "m";
Ted Kremenek826a3452010-07-16 02:11:22 +0000436 case None:
437 return "";
438 }
439 return NULL;
440}
441
442//===----------------------------------------------------------------------===//
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000443// Methods on ConversionSpecifier.
444//===----------------------------------------------------------------------===//
445
446const char *ConversionSpecifier::toString() const {
447 switch (kind) {
448 case dArg: return "d";
449 case iArg: return "i";
450 case oArg: return "o";
451 case uArg: return "u";
452 case xArg: return "x";
453 case XArg: return "X";
454 case fArg: return "f";
455 case FArg: return "F";
456 case eArg: return "e";
457 case EArg: return "E";
458 case gArg: return "g";
459 case GArg: return "G";
460 case aArg: return "a";
461 case AArg: return "A";
462 case cArg: return "c";
463 case sArg: return "s";
464 case pArg: return "p";
465 case nArg: return "n";
466 case PercentArg: return "%";
467 case ScanListArg: return "[";
468 case InvalidSpecifier: return NULL;
469
470 // MacOS X unicode extensions.
471 case CArg: return "C";
472 case SArg: return "S";
473
474 // Objective-C specific specifiers.
475 case ObjCObjArg: return "@";
476
477 // GlibC specific specifiers.
478 case PrintErrno: return "m";
479 }
480 return NULL;
481}
482
483//===----------------------------------------------------------------------===//
Ted Kremenek826a3452010-07-16 02:11:22 +0000484// Methods on OptionalAmount.
485//===----------------------------------------------------------------------===//
486
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000488 switch (hs) {
489 case Invalid:
490 case NotSpecified:
491 return;
492 case Arg:
493 if (UsesDotPrefix)
494 os << ".";
495 if (usesPositionalArg())
496 os << "*" << getPositionalArgIndex() << "$";
497 else
498 os << "*";
499 break;
500 case Constant:
501 if (UsesDotPrefix)
502 os << ".";
503 os << amt;
504 break;
505 }
506}
507
Ted Kremeneka412a492010-07-20 20:04:42 +0000508bool FormatSpecifier::hasValidLengthModifier() const {
509 switch (LM.getKind()) {
510 case LengthModifier::None:
511 return true;
512
Hans Wennborg32addd52012-02-16 16:34:54 +0000513 // Handle most integer flags
Ted Kremeneka412a492010-07-20 20:04:42 +0000514 case LengthModifier::AsChar:
515 case LengthModifier::AsShort:
516 case LengthModifier::AsLongLong:
Hans Wennborg32addd52012-02-16 16:34:54 +0000517 case LengthModifier::AsQuad:
Ted Kremeneka412a492010-07-20 20:04:42 +0000518 case LengthModifier::AsIntMax:
519 case LengthModifier::AsSizeT:
520 case LengthModifier::AsPtrDiff:
521 switch (CS.getKind()) {
522 case ConversionSpecifier::dArg:
523 case ConversionSpecifier::iArg:
524 case ConversionSpecifier::oArg:
525 case ConversionSpecifier::uArg:
526 case ConversionSpecifier::xArg:
527 case ConversionSpecifier::XArg:
528 case ConversionSpecifier::nArg:
529 return true;
530 default:
531 return false;
532 }
533
Hans Wennborg32addd52012-02-16 16:34:54 +0000534 // Handle 'l' flag
Ted Kremeneka412a492010-07-20 20:04:42 +0000535 case LengthModifier::AsLong:
536 switch (CS.getKind()) {
537 case ConversionSpecifier::dArg:
538 case ConversionSpecifier::iArg:
539 case ConversionSpecifier::oArg:
540 case ConversionSpecifier::uArg:
541 case ConversionSpecifier::xArg:
542 case ConversionSpecifier::XArg:
543 case ConversionSpecifier::aArg:
544 case ConversionSpecifier::AArg:
545 case ConversionSpecifier::fArg:
546 case ConversionSpecifier::FArg:
547 case ConversionSpecifier::eArg:
548 case ConversionSpecifier::EArg:
549 case ConversionSpecifier::gArg:
550 case ConversionSpecifier::GArg:
551 case ConversionSpecifier::nArg:
552 case ConversionSpecifier::cArg:
553 case ConversionSpecifier::sArg:
Ted Kremenekef1440b2012-01-20 22:11:52 +0000554 case ConversionSpecifier::ScanListArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000555 return true;
556 default:
557 return false;
558 }
559
560 case LengthModifier::AsLongDouble:
561 switch (CS.getKind()) {
562 case ConversionSpecifier::aArg:
563 case ConversionSpecifier::AArg:
564 case ConversionSpecifier::fArg:
565 case ConversionSpecifier::FArg:
566 case ConversionSpecifier::eArg:
567 case ConversionSpecifier::EArg:
568 case ConversionSpecifier::gArg:
569 case ConversionSpecifier::GArg:
570 return true;
Ted Kremenek9d24c2c2012-01-24 21:29:54 +0000571 // GNU extension.
572 case ConversionSpecifier::dArg:
573 case ConversionSpecifier::iArg:
574 case ConversionSpecifier::oArg:
575 case ConversionSpecifier::uArg:
576 case ConversionSpecifier::xArg:
577 case ConversionSpecifier::XArg:
578 return true;
Ted Kremeneka412a492010-07-20 20:04:42 +0000579 default:
580 return false;
581 }
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000582
583 case LengthModifier::AsAllocate:
584 switch (CS.getKind()) {
585 case ConversionSpecifier::sArg:
586 case ConversionSpecifier::SArg:
Hans Wennborg28058d12012-01-12 15:07:16 +0000587 case ConversionSpecifier::ScanListArg:
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000588 return true;
589 default:
590 return false;
591 }
Hans Wennborg37969b72012-01-12 17:11:12 +0000592
593 case LengthModifier::AsMAllocate:
594 switch (CS.getKind()) {
595 case ConversionSpecifier::cArg:
596 case ConversionSpecifier::CArg:
597 case ConversionSpecifier::sArg:
598 case ConversionSpecifier::SArg:
599 case ConversionSpecifier::ScanListArg:
600 return true;
601 default:
602 return false;
603 }
Ted Kremeneka412a492010-07-20 20:04:42 +0000604 }
David Blaikie30263482012-01-20 21:50:17 +0000605 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremeneka412a492010-07-20 20:04:42 +0000606}
Hans Wennborg76517422012-02-22 10:17:01 +0000607
608bool FormatSpecifier::hasStandardLengthModifier() const {
609 switch (LM.getKind()) {
610 case LengthModifier::None:
611 case LengthModifier::AsChar:
612 case LengthModifier::AsShort:
613 case LengthModifier::AsLong:
614 case LengthModifier::AsLongLong:
615 case LengthModifier::AsIntMax:
616 case LengthModifier::AsSizeT:
617 case LengthModifier::AsPtrDiff:
618 case LengthModifier::AsLongDouble:
619 return true;
620 case LengthModifier::AsAllocate:
621 case LengthModifier::AsMAllocate:
622 case LengthModifier::AsQuad:
623 return false;
624 }
625 llvm_unreachable("Invalid LengthModifier Kind!");
626}
627
628bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
629 switch (CS.getKind()) {
630 case ConversionSpecifier::cArg:
631 case ConversionSpecifier::dArg:
632 case ConversionSpecifier::iArg:
633 case ConversionSpecifier::oArg:
634 case ConversionSpecifier::uArg:
635 case ConversionSpecifier::xArg:
636 case ConversionSpecifier::XArg:
637 case ConversionSpecifier::fArg:
638 case ConversionSpecifier::FArg:
639 case ConversionSpecifier::eArg:
640 case ConversionSpecifier::EArg:
641 case ConversionSpecifier::gArg:
642 case ConversionSpecifier::GArg:
643 case ConversionSpecifier::aArg:
644 case ConversionSpecifier::AArg:
645 case ConversionSpecifier::sArg:
646 case ConversionSpecifier::pArg:
647 case ConversionSpecifier::nArg:
648 case ConversionSpecifier::ObjCObjArg:
649 case ConversionSpecifier::ScanListArg:
650 case ConversionSpecifier::PercentArg:
651 return true;
652 case ConversionSpecifier::CArg:
653 case ConversionSpecifier::SArg:
654 return LangOpt.ObjC1 || LangOpt.ObjC2;
655 case ConversionSpecifier::InvalidSpecifier:
656 case ConversionSpecifier::PrintErrno:
657 return false;
658 }
659 llvm_unreachable("Invalid ConversionSpecifier Kind!");
660}
661
662bool FormatSpecifier::hasStandardLengthConversionCombination() const {
663 if (LM.getKind() == LengthModifier::AsLongDouble) {
664 switch(CS.getKind()) {
665 case ConversionSpecifier::dArg:
666 case ConversionSpecifier::iArg:
667 case ConversionSpecifier::oArg:
668 case ConversionSpecifier::uArg:
669 case ConversionSpecifier::xArg:
670 case ConversionSpecifier::XArg:
671 return false;
672 default:
673 return true;
674 }
675 }
676 return true;
677}