blob: f70086b8830c3633e3b2b7ae9d14dae42ea78dc6 [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"
Jordan Rose92303592012-09-08 04:00:03 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenek02087932010-07-16 02:11:22 +000018
Hans Wennborgc3b3da02012-08-07 08:11:26 +000019using clang::analyze_format_string::ArgType;
Ted Kremenek02087932010-07-16 02:11:22 +000020using clang::analyze_format_string::FormatStringHandler;
21using clang::analyze_format_string::FormatSpecifier;
22using clang::analyze_format_string::LengthModifier;
23using clang::analyze_format_string::OptionalAmount;
24using clang::analyze_format_string::PositionContext;
Ted Kremenekea28f832010-07-20 20:04:42 +000025using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek02087932010-07-16 02:11:22 +000026using namespace clang;
27
28// Key function to FormatStringHandler.
29FormatStringHandler::~FormatStringHandler() {}
30
31//===----------------------------------------------------------------------===//
32// Functions for parsing format strings components in both printf and
33// scanf format strings.
34//===----------------------------------------------------------------------===//
35
36OptionalAmount
37clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
38 const char *I = Beg;
39 UpdateOnReturn <const char*> UpdateBeg(Beg, I);
40
41 unsigned accumulator = 0;
42 bool hasDigits = false;
43
44 for ( ; I != E; ++I) {
45 char c = *I;
46 if (c >= '0' && c <= '9') {
47 hasDigits = true;
48 accumulator = (accumulator * 10) + (c - '0');
49 continue;
50 }
51
52 if (hasDigits)
53 return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
54 false);
55
56 break;
57 }
58
59 return OptionalAmount();
60}
61
62OptionalAmount
63clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
64 const char *E,
65 unsigned &argIndex) {
66 if (*Beg == '*') {
67 ++Beg;
68 return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
69 }
70
71 return ParseAmount(Beg, E);
72}
73
74OptionalAmount
75clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
76 const char *Start,
77 const char *&Beg,
78 const char *E,
79 PositionContext p) {
80 if (*Beg == '*') {
81 const char *I = Beg + 1;
82 const OptionalAmount &Amt = ParseAmount(I, E);
83
84 if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
85 H.HandleInvalidPosition(Beg, I - Beg, p);
86 return OptionalAmount(false);
87 }
88
89 if (I == E) {
90 // No more characters left?
91 H.HandleIncompleteSpecifier(Start, E - Start);
92 return OptionalAmount(false);
93 }
94
95 assert(Amt.getHowSpecified() == OptionalAmount::Constant);
96
97 if (*I == '$') {
98 // Handle positional arguments
99
100 // Special case: '*0$', since this is an easy mistake.
101 if (Amt.getConstantAmount() == 0) {
102 H.HandleZeroPosition(Beg, I - Beg + 1);
103 return OptionalAmount(false);
104 }
105
106 const char *Tmp = Beg;
107 Beg = ++I;
108
109 return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
110 Tmp, 0, true);
111 }
112
113 H.HandleInvalidPosition(Beg, I - Beg, p);
114 return OptionalAmount(false);
115 }
116
117 return ParseAmount(Beg, E);
118}
119
120
121bool
122clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
123 FormatSpecifier &CS,
124 const char *Start,
125 const char *&Beg, const char *E,
126 unsigned *argIndex) {
127 // FIXME: Support negative field widths.
128 if (argIndex) {
129 CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
130 }
131 else {
132 const OptionalAmount Amt =
133 ParsePositionAmount(H, Start, Beg, E,
134 analyze_format_string::FieldWidthPos);
135
136 if (Amt.isInvalid())
137 return true;
138 CS.setFieldWidth(Amt);
139 }
140 return false;
141}
142
143bool
144clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
145 FormatSpecifier &FS,
146 const char *Start,
147 const char *&Beg,
148 const char *E) {
149 const char *I = Beg;
150
151 const OptionalAmount &Amt = ParseAmount(I, E);
152
153 if (I == E) {
154 // No more characters left?
155 H.HandleIncompleteSpecifier(Start, E - Start);
156 return true;
157 }
158
159 if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
Hans Wennborgaa8c61c2012-03-09 10:10:54 +0000160 // Warn that positional arguments are non-standard.
161 H.HandlePosition(Start, I - Start);
162
Ted Kremenek02087932010-07-16 02:11:22 +0000163 // Special case: '%0$', since this is an easy mistake.
164 if (Amt.getConstantAmount() == 0) {
165 H.HandleZeroPosition(Start, I - Start);
166 return true;
167 }
168
169 FS.setArgIndex(Amt.getConstantAmount() - 1);
170 FS.setUsesPositionalArg();
171 // Update the caller's pointer if we decided to consume
172 // these characters.
173 Beg = I;
174 return false;
175 }
176
177 return false;
178}
179
180bool
181clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
182 const char *&I,
Hans Wennborg23926bd2011-12-15 10:25:47 +0000183 const char *E,
184 const LangOptions &LO,
185 bool IsScanf) {
Ted Kremenek02087932010-07-16 02:11:22 +0000186 LengthModifier::Kind lmKind = LengthModifier::None;
187 const char *lmPosition = I;
188 switch (*I) {
189 default:
190 return false;
191 case 'h':
192 ++I;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000193 lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
194 : LengthModifier::AsShort;
Ted Kremenek02087932010-07-16 02:11:22 +0000195 break;
196 case 'l':
197 ++I;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000198 lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
199 : LengthModifier::AsLong;
Ted Kremenek02087932010-07-16 02:11:22 +0000200 break;
201 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
202 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
203 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
204 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000205 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborg23926bd2011-12-15 10:25:47 +0000206 case 'a':
Hans Wennborge5f554a2011-12-28 13:10:50 +0000207 if (IsScanf && !LO.C99 && !LO.CPlusPlus0x) {
Hans Wennborg23926bd2011-12-15 10:25:47 +0000208 // For scanf in C90, look at the next character to see if this should
209 // be parsed as the GNU extension 'a' length modifier. If not, this
210 // will be parsed as a conversion specifier.
211 ++I;
212 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
213 lmKind = LengthModifier::AsAllocate;
214 break;
215 }
216 --I;
217 }
218 return false;
Hans Wennborg6073e312012-01-12 17:11:12 +0000219 case 'm':
220 if (IsScanf) {
221 lmKind = LengthModifier::AsMAllocate;
222 ++I;
223 break;
224 }
225 return false;
Ted Kremenek02087932010-07-16 02:11:22 +0000226 }
227 LengthModifier lm(lmPosition, lmKind);
228 FS.setLengthModifier(lm);
229 return true;
230}
231
232//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000233// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000234//===----------------------------------------------------------------------===//
235
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000236bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000237 if (Ptr) {
238 // It has to be a pointer.
239 const PointerType *PT = argTy->getAs<PointerType>();
240 if (!PT)
241 return false;
242
243 // We cannot write through a const qualified pointer.
244 if (PT->getPointeeType().isConstQualified())
245 return false;
246
247 argTy = PT->getPointeeType();
248 }
249
Ted Kremenek02087932010-07-16 02:11:22 +0000250 switch (K) {
251 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000252 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000253
254 case UnknownTy:
255 return true;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000256
257 case AnyCharTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000258 if (const EnumType *ETy = argTy->getAs<EnumType>())
259 argTy = ETy->getDecl()->getIntegerType();
260
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000261 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
262 switch (BT->getKind()) {
263 default:
264 break;
265 case BuiltinType::Char_S:
266 case BuiltinType::SChar:
267 case BuiltinType::UChar:
268 case BuiltinType::Char_U:
269 return true;
270 }
271 return false;
272 }
273
Ted Kremenek02087932010-07-16 02:11:22 +0000274 case SpecificTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000275 if (const EnumType *ETy = argTy->getAs<EnumType>())
276 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek02087932010-07-16 02:11:22 +0000277 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000278
Nick Lewycky45ccba62011-12-02 23:21:43 +0000279 if (T == argTy)
Ted Kremenek02087932010-07-16 02:11:22 +0000280 return true;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000281 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000282 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000283 switch (BT->getKind()) {
284 default:
285 break;
286 case BuiltinType::Char_S:
287 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000288 case BuiltinType::Char_U:
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000289 case BuiltinType::UChar:
Hans Wennborg967b9ce2012-05-08 17:21:31 +0000290 return T == C.UnsignedCharTy || T == C.SignedCharTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000291 case BuiltinType::Short:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000292 return T == C.UnsignedShortTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000293 case BuiltinType::UShort:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000294 return T == C.ShortTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000295 case BuiltinType::Int:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000296 return T == C.UnsignedIntTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000297 case BuiltinType::UInt:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000298 return T == C.IntTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000299 case BuiltinType::Long:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000300 return T == C.UnsignedLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000301 case BuiltinType::ULong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000302 return T == C.LongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000303 case BuiltinType::LongLong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000304 return T == C.UnsignedLongLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000305 case BuiltinType::ULongLong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000306 return T == C.LongLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000307 }
308 return false;
309 }
310
311 case CStrTy: {
312 const PointerType *PT = argTy->getAs<PointerType>();
313 if (!PT)
314 return false;
315 QualType pointeeTy = PT->getPointeeType();
316 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
317 switch (BT->getKind()) {
318 case BuiltinType::Void:
319 case BuiltinType::Char_U:
320 case BuiltinType::UChar:
321 case BuiltinType::Char_S:
322 case BuiltinType::SChar:
323 return true;
324 default:
325 break;
326 }
327
328 return false;
329 }
330
331 case WCStrTy: {
332 const PointerType *PT = argTy->getAs<PointerType>();
333 if (!PT)
334 return false;
335 QualType pointeeTy =
336 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
337 return pointeeTy == C.getWCharType();
338 }
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000339
340 case WIntTy: {
James Molloy36365542012-05-04 10:55:22 +0000341
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000342 QualType PromoArg =
343 argTy->isPromotableIntegerType()
344 ? C.getPromotedIntegerType(argTy) : argTy;
345
James Molloy36365542012-05-04 10:55:22 +0000346 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000347 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
348
James Molloy36365542012-05-04 10:55:22 +0000349 // If the promoted argument is the corresponding signed type of the
350 // wint_t type, then it should match.
351 if (PromoArg->hasSignedIntegerRepresentation() &&
352 C.getCorrespondingUnsignedType(PromoArg) == WInt)
353 return true;
354
355 return WInt == PromoArg;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000356 }
Ted Kremenek02087932010-07-16 02:11:22 +0000357
358 case CPointerTy:
Anders Carlsson3fd50312010-11-06 14:58:53 +0000359 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
Ted Kremenekc08c4752012-03-15 21:22:27 +0000360 argTy->isBlockPointerType() || argTy->isNullPtrType();
Ted Kremenek02087932010-07-16 02:11:22 +0000361
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000362 case ObjCPointerTy: {
363 if (argTy->getAs<ObjCObjectPointerType>() ||
364 argTy->getAs<BlockPointerType>())
365 return true;
366
367 // Handle implicit toll-free bridging.
368 if (const PointerType *PT = argTy->getAs<PointerType>()) {
369 // Things such as CFTypeRef are really just opaque pointers
370 // to C structs representing CF types that can often be bridged
371 // to Objective-C objects. Since the compiler doesn't know which
372 // structs can be toll-free bridged, we just accept them all.
373 QualType pointee = PT->getPointeeType();
374 if (pointee->getAsStructureType() || pointee->isVoidType())
375 return true;
376 }
377 return false;
378 }
Ted Kremenek02087932010-07-16 02:11:22 +0000379 }
380
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000381 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000382}
383
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000384QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000385 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000386 switch (K) {
387 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000388 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000389 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000390 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000391 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000392 Res = C.CharTy;
393 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000394 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000395 Res = T;
396 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000397 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000398 Res = C.getPointerType(C.CharTy);
399 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000400 case WCStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000401 Res = C.getPointerType(C.getWCharType());
402 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000403 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000404 Res = C.ObjCBuiltinIdTy;
405 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000406 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000407 Res = C.VoidPtrTy;
408 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000409 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000410 Res = C.getWIntType();
411 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000412 }
Ted Kremenek02087932010-07-16 02:11:22 +0000413 }
414
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000415 if (Ptr)
416 Res = C.getPointerType(Res);
417 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000418}
419
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000420std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000421 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000422
423 std::string Alias;
424 if (Name) {
425 // Use a specific name for this type, e.g. "size_t".
426 Alias = Name;
427 if (Ptr) {
428 // If ArgType is actually a pointer to T, append an asterisk.
429 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
430 }
431 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
432 if (S == Alias)
433 Alias.clear();
434 }
435
436 if (!Alias.empty())
437 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000438 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000439}
440
441
Ted Kremenek02087932010-07-16 02:11:22 +0000442//===----------------------------------------------------------------------===//
443// Methods on OptionalAmount.
444//===----------------------------------------------------------------------===//
445
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000446ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000447analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
448 return Ctx.IntTy;
449}
450
451//===----------------------------------------------------------------------===//
452// Methods on LengthModifier.
453//===----------------------------------------------------------------------===//
454
455const char *
456analyze_format_string::LengthModifier::toString() const {
457 switch (kind) {
458 case AsChar:
459 return "hh";
460 case AsShort:
461 return "h";
462 case AsLong: // or AsWideChar
463 return "l";
464 case AsLongLong:
465 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000466 case AsQuad:
467 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000468 case AsIntMax:
469 return "j";
470 case AsSizeT:
471 return "z";
472 case AsPtrDiff:
473 return "t";
474 case AsLongDouble:
475 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000476 case AsAllocate:
477 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000478 case AsMAllocate:
479 return "m";
Ted Kremenek02087932010-07-16 02:11:22 +0000480 case None:
481 return "";
482 }
483 return NULL;
484}
485
486//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000487// Methods on ConversionSpecifier.
488//===----------------------------------------------------------------------===//
489
490const char *ConversionSpecifier::toString() const {
491 switch (kind) {
492 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000493 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000494 case iArg: return "i";
495 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000496 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000497 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000498 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000499 case xArg: return "x";
500 case XArg: return "X";
501 case fArg: return "f";
502 case FArg: return "F";
503 case eArg: return "e";
504 case EArg: return "E";
505 case gArg: return "g";
506 case GArg: return "G";
507 case aArg: return "a";
508 case AArg: return "A";
509 case cArg: return "c";
510 case sArg: return "s";
511 case pArg: return "p";
512 case nArg: return "n";
513 case PercentArg: return "%";
514 case ScanListArg: return "[";
515 case InvalidSpecifier: return NULL;
516
517 // MacOS X unicode extensions.
518 case CArg: return "C";
519 case SArg: return "S";
520
521 // Objective-C specific specifiers.
522 case ObjCObjArg: return "@";
523
524 // GlibC specific specifiers.
525 case PrintErrno: return "m";
526 }
527 return NULL;
528}
529
530//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000531// Methods on OptionalAmount.
532//===----------------------------------------------------------------------===//
533
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000534void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000535 switch (hs) {
536 case Invalid:
537 case NotSpecified:
538 return;
539 case Arg:
540 if (UsesDotPrefix)
541 os << ".";
542 if (usesPositionalArg())
543 os << "*" << getPositionalArgIndex() << "$";
544 else
545 os << "*";
546 break;
547 case Constant:
548 if (UsesDotPrefix)
549 os << ".";
550 os << amt;
551 break;
552 }
553}
554
Jordan Rose92303592012-09-08 04:00:03 +0000555bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000556 switch (LM.getKind()) {
557 case LengthModifier::None:
558 return true;
559
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000560 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000561 case LengthModifier::AsChar:
562 case LengthModifier::AsShort:
563 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000564 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000565 case LengthModifier::AsIntMax:
566 case LengthModifier::AsSizeT:
567 case LengthModifier::AsPtrDiff:
568 switch (CS.getKind()) {
569 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000570 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000571 case ConversionSpecifier::iArg:
572 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000573 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000574 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000575 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000576 case ConversionSpecifier::xArg:
577 case ConversionSpecifier::XArg:
578 case ConversionSpecifier::nArg:
579 return true;
580 default:
581 return false;
582 }
583
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000584 // Handle 'l' flag
Ted Kremenekea28f832010-07-20 20:04:42 +0000585 case LengthModifier::AsLong:
586 switch (CS.getKind()) {
587 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000588 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000589 case ConversionSpecifier::iArg:
590 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000591 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000592 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000593 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000594 case ConversionSpecifier::xArg:
595 case ConversionSpecifier::XArg:
596 case ConversionSpecifier::aArg:
597 case ConversionSpecifier::AArg:
598 case ConversionSpecifier::fArg:
599 case ConversionSpecifier::FArg:
600 case ConversionSpecifier::eArg:
601 case ConversionSpecifier::EArg:
602 case ConversionSpecifier::gArg:
603 case ConversionSpecifier::GArg:
604 case ConversionSpecifier::nArg:
605 case ConversionSpecifier::cArg:
606 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000607 case ConversionSpecifier::ScanListArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000608 return true;
609 default:
610 return false;
611 }
612
613 case LengthModifier::AsLongDouble:
614 switch (CS.getKind()) {
615 case ConversionSpecifier::aArg:
616 case ConversionSpecifier::AArg:
617 case ConversionSpecifier::fArg:
618 case ConversionSpecifier::FArg:
619 case ConversionSpecifier::eArg:
620 case ConversionSpecifier::EArg:
621 case ConversionSpecifier::gArg:
622 case ConversionSpecifier::GArg:
623 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000624 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000625 case ConversionSpecifier::dArg:
626 case ConversionSpecifier::iArg:
627 case ConversionSpecifier::oArg:
628 case ConversionSpecifier::uArg:
629 case ConversionSpecifier::xArg:
630 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000631 return !Target.getTriple().isOSDarwin() &&
632 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000633 default:
634 return false;
635 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000636
637 case LengthModifier::AsAllocate:
638 switch (CS.getKind()) {
639 case ConversionSpecifier::sArg:
640 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000641 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000642 return true;
643 default:
644 return false;
645 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000646
647 case LengthModifier::AsMAllocate:
648 switch (CS.getKind()) {
649 case ConversionSpecifier::cArg:
650 case ConversionSpecifier::CArg:
651 case ConversionSpecifier::sArg:
652 case ConversionSpecifier::SArg:
653 case ConversionSpecifier::ScanListArg:
654 return true;
655 default:
656 return false;
657 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000658 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000659 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000660}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000661
662bool FormatSpecifier::hasStandardLengthModifier() const {
663 switch (LM.getKind()) {
664 case LengthModifier::None:
665 case LengthModifier::AsChar:
666 case LengthModifier::AsShort:
667 case LengthModifier::AsLong:
668 case LengthModifier::AsLongLong:
669 case LengthModifier::AsIntMax:
670 case LengthModifier::AsSizeT:
671 case LengthModifier::AsPtrDiff:
672 case LengthModifier::AsLongDouble:
673 return true;
674 case LengthModifier::AsAllocate:
675 case LengthModifier::AsMAllocate:
676 case LengthModifier::AsQuad:
677 return false;
678 }
679 llvm_unreachable("Invalid LengthModifier Kind!");
680}
681
682bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
683 switch (CS.getKind()) {
684 case ConversionSpecifier::cArg:
685 case ConversionSpecifier::dArg:
686 case ConversionSpecifier::iArg:
687 case ConversionSpecifier::oArg:
688 case ConversionSpecifier::uArg:
689 case ConversionSpecifier::xArg:
690 case ConversionSpecifier::XArg:
691 case ConversionSpecifier::fArg:
692 case ConversionSpecifier::FArg:
693 case ConversionSpecifier::eArg:
694 case ConversionSpecifier::EArg:
695 case ConversionSpecifier::gArg:
696 case ConversionSpecifier::GArg:
697 case ConversionSpecifier::aArg:
698 case ConversionSpecifier::AArg:
699 case ConversionSpecifier::sArg:
700 case ConversionSpecifier::pArg:
701 case ConversionSpecifier::nArg:
702 case ConversionSpecifier::ObjCObjArg:
703 case ConversionSpecifier::ScanListArg:
704 case ConversionSpecifier::PercentArg:
705 return true;
706 case ConversionSpecifier::CArg:
707 case ConversionSpecifier::SArg:
708 return LangOpt.ObjC1 || LangOpt.ObjC2;
709 case ConversionSpecifier::InvalidSpecifier:
710 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000711 case ConversionSpecifier::DArg:
712 case ConversionSpecifier::OArg:
713 case ConversionSpecifier::UArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000714 return false;
715 }
716 llvm_unreachable("Invalid ConversionSpecifier Kind!");
717}
718
719bool FormatSpecifier::hasStandardLengthConversionCombination() const {
720 if (LM.getKind() == LengthModifier::AsLongDouble) {
721 switch(CS.getKind()) {
722 case ConversionSpecifier::dArg:
723 case ConversionSpecifier::iArg:
724 case ConversionSpecifier::oArg:
725 case ConversionSpecifier::uArg:
726 case ConversionSpecifier::xArg:
727 case ConversionSpecifier::XArg:
728 return false;
729 default:
730 return true;
731 }
732 }
733 return true;
734}
Hans Wennborg08574d32012-07-27 19:17:46 +0000735
Jordan Rose92303592012-09-08 04:00:03 +0000736llvm::Optional<LengthModifier>
737FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000738 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
739 if (LM.getKind() == LengthModifier::AsLongDouble ||
740 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000741 LengthModifier FixedLM(LM);
742 FixedLM.setKind(LengthModifier::AsLongLong);
743 return FixedLM;
744 }
Jordan Rose92303592012-09-08 04:00:03 +0000745 }
746
747 return llvm::Optional<LengthModifier>();
748}
749
Hans Wennborg08574d32012-07-27 19:17:46 +0000750bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
751 LengthModifier &LM) {
752 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
753 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
754
755 for (;;) {
756 const IdentifierInfo *Identifier = Typedef->getIdentifier();
757 if (Identifier->getName() == "size_t") {
758 LM.setKind(LengthModifier::AsSizeT);
759 return true;
760 } else if (Identifier->getName() == "ssize_t") {
761 // Not C99, but common in Unix.
762 LM.setKind(LengthModifier::AsSizeT);
763 return true;
764 } else if (Identifier->getName() == "intmax_t") {
765 LM.setKind(LengthModifier::AsIntMax);
766 return true;
767 } else if (Identifier->getName() == "uintmax_t") {
768 LM.setKind(LengthModifier::AsIntMax);
769 return true;
770 } else if (Identifier->getName() == "ptrdiff_t") {
771 LM.setKind(LengthModifier::AsPtrDiff);
772 return true;
773 }
774
775 QualType T = Typedef->getUnderlyingType();
776 if (!isa<TypedefType>(T))
777 break;
778
779 Typedef = cast<TypedefType>(T)->getDecl();
780 }
781 return false;
782}