blob: e7ea48688daea50a831dc65be20c50de86198d9d [file] [log] [blame]
Ted Kremenek02087932010-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 Wennborg23926bd2011-12-15 10:25:47 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenek02087932010-07-16 02:11:22 +000017
Hans Wennborgc3b3da02012-08-07 08:11:26 +000018using clang::analyze_format_string::ArgType;
Ted Kremenek02087932010-07-16 02:11:22 +000019using 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 Kremenekea28f832010-07-20 20:04:42 +000024using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek02087932010-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 Wennborgaa8c61c2012-03-09 10:10:54 +0000159 // Warn that positional arguments are non-standard.
160 H.HandlePosition(Start, I - Start);
161
Ted Kremenek02087932010-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 Wennborg23926bd2011-12-15 10:25:47 +0000182 const char *E,
183 const LangOptions &LO,
184 bool IsScanf) {
Ted Kremenek02087932010-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 Kremenek74e82bd2011-10-25 04:20:41 +0000192 lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
193 : LengthModifier::AsShort;
Ted Kremenek02087932010-07-16 02:11:22 +0000194 break;
195 case 'l':
196 ++I;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000197 lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
198 : LengthModifier::AsLong;
Ted Kremenek02087932010-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 Wennborg9bc9bcc2012-02-16 16:34:54 +0000204 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborg23926bd2011-12-15 10:25:47 +0000205 case 'a':
Hans Wennborge5f554a2011-12-28 13:10:50 +0000206 if (IsScanf && !LO.C99 && !LO.CPlusPlus0x) {
Hans Wennborg23926bd2011-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 Wennborg6073e312012-01-12 17:11:12 +0000218 case 'm':
219 if (IsScanf) {
220 lmKind = LengthModifier::AsMAllocate;
221 ++I;
222 break;
223 }
224 return false;
Ted Kremenek02087932010-07-16 02:11:22 +0000225 }
226 LengthModifier lm(lmPosition, lmKind);
227 FS.setLengthModifier(lm);
228 return true;
229}
230
231//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000232// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000233//===----------------------------------------------------------------------===//
234
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000235bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000236 if (Ptr) {
237 // It has to be a pointer.
238 const PointerType *PT = argTy->getAs<PointerType>();
239 if (!PT)
240 return false;
241
242 // We cannot write through a const qualified pointer.
243 if (PT->getPointeeType().isConstQualified())
244 return false;
245
246 argTy = PT->getPointeeType();
247 }
248
Ted Kremenek02087932010-07-16 02:11:22 +0000249 switch (K) {
250 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000251 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000252
253 case UnknownTy:
254 return true;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000255
256 case AnyCharTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000257 if (const EnumType *ETy = argTy->getAs<EnumType>())
258 argTy = ETy->getDecl()->getIntegerType();
259
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000260 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
261 switch (BT->getKind()) {
262 default:
263 break;
264 case BuiltinType::Char_S:
265 case BuiltinType::SChar:
266 case BuiltinType::UChar:
267 case BuiltinType::Char_U:
268 return true;
269 }
270 return false;
271 }
272
Ted Kremenek02087932010-07-16 02:11:22 +0000273 case SpecificTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000274 if (const EnumType *ETy = argTy->getAs<EnumType>())
275 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek02087932010-07-16 02:11:22 +0000276 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000277
Nick Lewycky45ccba62011-12-02 23:21:43 +0000278 if (T == argTy)
Ted Kremenek02087932010-07-16 02:11:22 +0000279 return true;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000280 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000281 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000282 switch (BT->getKind()) {
283 default:
284 break;
285 case BuiltinType::Char_S:
286 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000287 case BuiltinType::Char_U:
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000288 case BuiltinType::UChar:
Hans Wennborg967b9ce2012-05-08 17:21:31 +0000289 return T == C.UnsignedCharTy || T == C.SignedCharTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000290 case BuiltinType::Short:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000291 return T == C.UnsignedShortTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000292 case BuiltinType::UShort:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000293 return T == C.ShortTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000294 case BuiltinType::Int:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000295 return T == C.UnsignedIntTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000296 case BuiltinType::UInt:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000297 return T == C.IntTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000298 case BuiltinType::Long:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000299 return T == C.UnsignedLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000300 case BuiltinType::ULong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000301 return T == C.LongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000302 case BuiltinType::LongLong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000303 return T == C.UnsignedLongLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000304 case BuiltinType::ULongLong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000305 return T == C.LongLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000306 }
307 return false;
308 }
309
310 case CStrTy: {
311 const PointerType *PT = argTy->getAs<PointerType>();
312 if (!PT)
313 return false;
314 QualType pointeeTy = PT->getPointeeType();
315 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
316 switch (BT->getKind()) {
317 case BuiltinType::Void:
318 case BuiltinType::Char_U:
319 case BuiltinType::UChar:
320 case BuiltinType::Char_S:
321 case BuiltinType::SChar:
322 return true;
323 default:
324 break;
325 }
326
327 return false;
328 }
329
330 case WCStrTy: {
331 const PointerType *PT = argTy->getAs<PointerType>();
332 if (!PT)
333 return false;
334 QualType pointeeTy =
335 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
336 return pointeeTy == C.getWCharType();
337 }
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000338
339 case WIntTy: {
James Molloy36365542012-05-04 10:55:22 +0000340
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000341 QualType PromoArg =
342 argTy->isPromotableIntegerType()
343 ? C.getPromotedIntegerType(argTy) : argTy;
344
James Molloy36365542012-05-04 10:55:22 +0000345 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000346 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
347
James Molloy36365542012-05-04 10:55:22 +0000348 // If the promoted argument is the corresponding signed type of the
349 // wint_t type, then it should match.
350 if (PromoArg->hasSignedIntegerRepresentation() &&
351 C.getCorrespondingUnsignedType(PromoArg) == WInt)
352 return true;
353
354 return WInt == PromoArg;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000355 }
Ted Kremenek02087932010-07-16 02:11:22 +0000356
357 case CPointerTy:
Anders Carlsson3fd50312010-11-06 14:58:53 +0000358 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
Ted Kremenekc08c4752012-03-15 21:22:27 +0000359 argTy->isBlockPointerType() || argTy->isNullPtrType();
Ted Kremenek02087932010-07-16 02:11:22 +0000360
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000361 case ObjCPointerTy: {
362 if (argTy->getAs<ObjCObjectPointerType>() ||
363 argTy->getAs<BlockPointerType>())
364 return true;
365
366 // Handle implicit toll-free bridging.
367 if (const PointerType *PT = argTy->getAs<PointerType>()) {
368 // Things such as CFTypeRef are really just opaque pointers
369 // to C structs representing CF types that can often be bridged
370 // to Objective-C objects. Since the compiler doesn't know which
371 // structs can be toll-free bridged, we just accept them all.
372 QualType pointee = PT->getPointeeType();
373 if (pointee->getAsStructureType() || pointee->isVoidType())
374 return true;
375 }
376 return false;
377 }
Ted Kremenek02087932010-07-16 02:11:22 +0000378 }
379
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000380 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000381}
382
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000383QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000384 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000385 switch (K) {
386 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000387 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000388 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000389 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000390 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000391 Res = C.CharTy;
392 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000393 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000394 Res = T;
395 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000396 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000397 Res = C.getPointerType(C.CharTy);
398 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000399 case WCStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000400 Res = C.getPointerType(C.getWCharType());
401 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000402 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000403 Res = C.ObjCBuiltinIdTy;
404 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000405 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000406 Res = C.VoidPtrTy;
407 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000408 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000409 Res = C.getWIntType();
410 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000411 }
Ted Kremenek02087932010-07-16 02:11:22 +0000412 }
413
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000414 if (Ptr)
415 Res = C.getPointerType(Res);
416 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000417}
418
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000419std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000420 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000421
422 std::string Alias;
423 if (Name) {
424 // Use a specific name for this type, e.g. "size_t".
425 Alias = Name;
426 if (Ptr) {
427 // If ArgType is actually a pointer to T, append an asterisk.
428 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
429 }
430 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
431 if (S == Alias)
432 Alias.clear();
433 }
434
435 if (!Alias.empty())
436 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000437 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000438}
439
440
Ted Kremenek02087932010-07-16 02:11:22 +0000441//===----------------------------------------------------------------------===//
442// Methods on OptionalAmount.
443//===----------------------------------------------------------------------===//
444
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000445ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000446analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
447 return Ctx.IntTy;
448}
449
450//===----------------------------------------------------------------------===//
451// Methods on LengthModifier.
452//===----------------------------------------------------------------------===//
453
454const char *
455analyze_format_string::LengthModifier::toString() const {
456 switch (kind) {
457 case AsChar:
458 return "hh";
459 case AsShort:
460 return "h";
461 case AsLong: // or AsWideChar
462 return "l";
463 case AsLongLong:
464 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000465 case AsQuad:
466 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000467 case AsIntMax:
468 return "j";
469 case AsSizeT:
470 return "z";
471 case AsPtrDiff:
472 return "t";
473 case AsLongDouble:
474 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000475 case AsAllocate:
476 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000477 case AsMAllocate:
478 return "m";
Ted Kremenek02087932010-07-16 02:11:22 +0000479 case None:
480 return "";
481 }
482 return NULL;
483}
484
485//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000486// Methods on ConversionSpecifier.
487//===----------------------------------------------------------------------===//
488
489const char *ConversionSpecifier::toString() const {
490 switch (kind) {
491 case dArg: return "d";
492 case iArg: return "i";
493 case oArg: return "o";
494 case uArg: return "u";
495 case xArg: return "x";
496 case XArg: return "X";
497 case fArg: return "f";
498 case FArg: return "F";
499 case eArg: return "e";
500 case EArg: return "E";
501 case gArg: return "g";
502 case GArg: return "G";
503 case aArg: return "a";
504 case AArg: return "A";
505 case cArg: return "c";
506 case sArg: return "s";
507 case pArg: return "p";
508 case nArg: return "n";
509 case PercentArg: return "%";
510 case ScanListArg: return "[";
511 case InvalidSpecifier: return NULL;
512
513 // MacOS X unicode extensions.
514 case CArg: return "C";
515 case SArg: return "S";
516
517 // Objective-C specific specifiers.
518 case ObjCObjArg: return "@";
519
520 // GlibC specific specifiers.
521 case PrintErrno: return "m";
522 }
523 return NULL;
524}
525
526//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000527// Methods on OptionalAmount.
528//===----------------------------------------------------------------------===//
529
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000530void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000531 switch (hs) {
532 case Invalid:
533 case NotSpecified:
534 return;
535 case Arg:
536 if (UsesDotPrefix)
537 os << ".";
538 if (usesPositionalArg())
539 os << "*" << getPositionalArgIndex() << "$";
540 else
541 os << "*";
542 break;
543 case Constant:
544 if (UsesDotPrefix)
545 os << ".";
546 os << amt;
547 break;
548 }
549}
550
Ted Kremenekea28f832010-07-20 20:04:42 +0000551bool FormatSpecifier::hasValidLengthModifier() const {
552 switch (LM.getKind()) {
553 case LengthModifier::None:
554 return true;
555
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000556 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000557 case LengthModifier::AsChar:
558 case LengthModifier::AsShort:
559 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000560 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000561 case LengthModifier::AsIntMax:
562 case LengthModifier::AsSizeT:
563 case LengthModifier::AsPtrDiff:
564 switch (CS.getKind()) {
565 case ConversionSpecifier::dArg:
566 case ConversionSpecifier::iArg:
567 case ConversionSpecifier::oArg:
568 case ConversionSpecifier::uArg:
569 case ConversionSpecifier::xArg:
570 case ConversionSpecifier::XArg:
571 case ConversionSpecifier::nArg:
572 return true;
573 default:
574 return false;
575 }
576
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000577 // Handle 'l' flag
Ted Kremenekea28f832010-07-20 20:04:42 +0000578 case LengthModifier::AsLong:
579 switch (CS.getKind()) {
580 case ConversionSpecifier::dArg:
581 case ConversionSpecifier::iArg:
582 case ConversionSpecifier::oArg:
583 case ConversionSpecifier::uArg:
584 case ConversionSpecifier::xArg:
585 case ConversionSpecifier::XArg:
586 case ConversionSpecifier::aArg:
587 case ConversionSpecifier::AArg:
588 case ConversionSpecifier::fArg:
589 case ConversionSpecifier::FArg:
590 case ConversionSpecifier::eArg:
591 case ConversionSpecifier::EArg:
592 case ConversionSpecifier::gArg:
593 case ConversionSpecifier::GArg:
594 case ConversionSpecifier::nArg:
595 case ConversionSpecifier::cArg:
596 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000597 case ConversionSpecifier::ScanListArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000598 return true;
599 default:
600 return false;
601 }
602
603 case LengthModifier::AsLongDouble:
604 switch (CS.getKind()) {
605 case ConversionSpecifier::aArg:
606 case ConversionSpecifier::AArg:
607 case ConversionSpecifier::fArg:
608 case ConversionSpecifier::FArg:
609 case ConversionSpecifier::eArg:
610 case ConversionSpecifier::EArg:
611 case ConversionSpecifier::gArg:
612 case ConversionSpecifier::GArg:
613 return true;
Ted Kremenek6fa57272012-01-24 21:29:54 +0000614 // GNU extension.
615 case ConversionSpecifier::dArg:
616 case ConversionSpecifier::iArg:
617 case ConversionSpecifier::oArg:
618 case ConversionSpecifier::uArg:
619 case ConversionSpecifier::xArg:
620 case ConversionSpecifier::XArg:
621 return true;
Ted Kremenekea28f832010-07-20 20:04:42 +0000622 default:
623 return false;
624 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000625
626 case LengthModifier::AsAllocate:
627 switch (CS.getKind()) {
628 case ConversionSpecifier::sArg:
629 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000630 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000631 return true;
632 default:
633 return false;
634 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000635
636 case LengthModifier::AsMAllocate:
637 switch (CS.getKind()) {
638 case ConversionSpecifier::cArg:
639 case ConversionSpecifier::CArg:
640 case ConversionSpecifier::sArg:
641 case ConversionSpecifier::SArg:
642 case ConversionSpecifier::ScanListArg:
643 return true;
644 default:
645 return false;
646 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000647 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000648 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000649}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000650
651bool FormatSpecifier::hasStandardLengthModifier() const {
652 switch (LM.getKind()) {
653 case LengthModifier::None:
654 case LengthModifier::AsChar:
655 case LengthModifier::AsShort:
656 case LengthModifier::AsLong:
657 case LengthModifier::AsLongLong:
658 case LengthModifier::AsIntMax:
659 case LengthModifier::AsSizeT:
660 case LengthModifier::AsPtrDiff:
661 case LengthModifier::AsLongDouble:
662 return true;
663 case LengthModifier::AsAllocate:
664 case LengthModifier::AsMAllocate:
665 case LengthModifier::AsQuad:
666 return false;
667 }
668 llvm_unreachable("Invalid LengthModifier Kind!");
669}
670
671bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
672 switch (CS.getKind()) {
673 case ConversionSpecifier::cArg:
674 case ConversionSpecifier::dArg:
675 case ConversionSpecifier::iArg:
676 case ConversionSpecifier::oArg:
677 case ConversionSpecifier::uArg:
678 case ConversionSpecifier::xArg:
679 case ConversionSpecifier::XArg:
680 case ConversionSpecifier::fArg:
681 case ConversionSpecifier::FArg:
682 case ConversionSpecifier::eArg:
683 case ConversionSpecifier::EArg:
684 case ConversionSpecifier::gArg:
685 case ConversionSpecifier::GArg:
686 case ConversionSpecifier::aArg:
687 case ConversionSpecifier::AArg:
688 case ConversionSpecifier::sArg:
689 case ConversionSpecifier::pArg:
690 case ConversionSpecifier::nArg:
691 case ConversionSpecifier::ObjCObjArg:
692 case ConversionSpecifier::ScanListArg:
693 case ConversionSpecifier::PercentArg:
694 return true;
695 case ConversionSpecifier::CArg:
696 case ConversionSpecifier::SArg:
697 return LangOpt.ObjC1 || LangOpt.ObjC2;
698 case ConversionSpecifier::InvalidSpecifier:
699 case ConversionSpecifier::PrintErrno:
700 return false;
701 }
702 llvm_unreachable("Invalid ConversionSpecifier Kind!");
703}
704
705bool FormatSpecifier::hasStandardLengthConversionCombination() const {
706 if (LM.getKind() == LengthModifier::AsLongDouble) {
707 switch(CS.getKind()) {
708 case ConversionSpecifier::dArg:
709 case ConversionSpecifier::iArg:
710 case ConversionSpecifier::oArg:
711 case ConversionSpecifier::uArg:
712 case ConversionSpecifier::xArg:
713 case ConversionSpecifier::XArg:
714 return false;
715 default:
716 return true;
717 }
718 }
719 return true;
720}
Hans Wennborg08574d32012-07-27 19:17:46 +0000721
722bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
723 LengthModifier &LM) {
724 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
725 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
726
727 for (;;) {
728 const IdentifierInfo *Identifier = Typedef->getIdentifier();
729 if (Identifier->getName() == "size_t") {
730 LM.setKind(LengthModifier::AsSizeT);
731 return true;
732 } else if (Identifier->getName() == "ssize_t") {
733 // Not C99, but common in Unix.
734 LM.setKind(LengthModifier::AsSizeT);
735 return true;
736 } else if (Identifier->getName() == "intmax_t") {
737 LM.setKind(LengthModifier::AsIntMax);
738 return true;
739 } else if (Identifier->getName() == "uintmax_t") {
740 LM.setKind(LengthModifier::AsIntMax);
741 return true;
742 } else if (Identifier->getName() == "ptrdiff_t") {
743 LM.setKind(LengthModifier::AsPtrDiff);
744 return true;
745 }
746
747 QualType T = Typedef->getUnderlyingType();
748 if (!isa<TypedefType>(T))
749 break;
750
751 Typedef = cast<TypedefType>(T)->getDecl();
752 }
753 return false;
754}