blob: 8c663d856f6a0e0fc01a764d67132aa43bb36f30 [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':
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000207 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
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;
David Majnemer3cba4952013-08-21 21:54:46 +0000226 // printf: AsInt64, AsInt32, AsInt3264
227 // scanf: AsInt64
228 case 'I':
229 if (I + 1 != E && I + 2 != E) {
230 if (I[1] == '6' && I[2] == '4') {
231 I += 3;
232 lmKind = LengthModifier::AsInt64;
233 break;
234 }
235 if (IsScanf)
236 return false;
237
238 if (I[1] == '3' && I[2] == '2') {
239 I += 3;
240 lmKind = LengthModifier::AsInt32;
241 break;
242 }
243 }
244 ++I;
245 lmKind = LengthModifier::AsInt3264;
246 break;
Hans Wennborg68f42b92014-09-04 21:39:46 +0000247 case 'w':
248 lmKind = LengthModifier::AsWide; ++I; break;
Ted Kremenek02087932010-07-16 02:11:22 +0000249 }
250 LengthModifier lm(lmPosition, lmKind);
251 FS.setLengthModifier(lm);
252 return true;
253}
254
255//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000256// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000257//===----------------------------------------------------------------------===//
258
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000259bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000260 if (Ptr) {
261 // It has to be a pointer.
262 const PointerType *PT = argTy->getAs<PointerType>();
263 if (!PT)
264 return false;
265
266 // We cannot write through a const qualified pointer.
267 if (PT->getPointeeType().isConstQualified())
268 return false;
269
270 argTy = PT->getPointeeType();
271 }
272
Ted Kremenek02087932010-07-16 02:11:22 +0000273 switch (K) {
274 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000275 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000276
277 case UnknownTy:
278 return true;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000279
280 case AnyCharTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000281 if (const EnumType *ETy = argTy->getAs<EnumType>())
282 argTy = ETy->getDecl()->getIntegerType();
283
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000284 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
285 switch (BT->getKind()) {
286 default:
287 break;
288 case BuiltinType::Char_S:
289 case BuiltinType::SChar:
290 case BuiltinType::UChar:
291 case BuiltinType::Char_U:
292 return true;
293 }
294 return false;
295 }
296
Ted Kremenek02087932010-07-16 02:11:22 +0000297 case SpecificTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000298 if (const EnumType *ETy = argTy->getAs<EnumType>())
299 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek02087932010-07-16 02:11:22 +0000300 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000301
Nick Lewycky45ccba62011-12-02 23:21:43 +0000302 if (T == argTy)
Ted Kremenek02087932010-07-16 02:11:22 +0000303 return true;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000304 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000305 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000306 switch (BT->getKind()) {
307 default:
308 break;
309 case BuiltinType::Char_S:
310 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000311 case BuiltinType::Char_U:
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000312 case BuiltinType::UChar:
Hans Wennborg967b9ce2012-05-08 17:21:31 +0000313 return T == C.UnsignedCharTy || T == C.SignedCharTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000314 case BuiltinType::Short:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000315 return T == C.UnsignedShortTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000316 case BuiltinType::UShort:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000317 return T == C.ShortTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000318 case BuiltinType::Int:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000319 return T == C.UnsignedIntTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000320 case BuiltinType::UInt:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000321 return T == C.IntTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000322 case BuiltinType::Long:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000323 return T == C.UnsignedLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000324 case BuiltinType::ULong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000325 return T == C.LongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000326 case BuiltinType::LongLong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000327 return T == C.UnsignedLongLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000328 case BuiltinType::ULongLong:
Nick Lewycky45ccba62011-12-02 23:21:43 +0000329 return T == C.LongLongTy;
Ted Kremenek02087932010-07-16 02:11:22 +0000330 }
331 return false;
332 }
333
334 case CStrTy: {
335 const PointerType *PT = argTy->getAs<PointerType>();
336 if (!PT)
337 return false;
338 QualType pointeeTy = PT->getPointeeType();
339 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
340 switch (BT->getKind()) {
341 case BuiltinType::Void:
342 case BuiltinType::Char_U:
343 case BuiltinType::UChar:
344 case BuiltinType::Char_S:
345 case BuiltinType::SChar:
346 return true;
347 default:
348 break;
349 }
350
351 return false;
352 }
353
354 case WCStrTy: {
355 const PointerType *PT = argTy->getAs<PointerType>();
356 if (!PT)
357 return false;
358 QualType pointeeTy =
359 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Hans Wennborg0d81e012013-05-10 10:08:40 +0000360 return pointeeTy == C.getWideCharType();
Ted Kremenek02087932010-07-16 02:11:22 +0000361 }
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000362
363 case WIntTy: {
James Molloy36365542012-05-04 10:55:22 +0000364
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000365 QualType PromoArg =
366 argTy->isPromotableIntegerType()
367 ? C.getPromotedIntegerType(argTy) : argTy;
368
James Molloy36365542012-05-04 10:55:22 +0000369 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000370 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
371
James Molloy36365542012-05-04 10:55:22 +0000372 // If the promoted argument is the corresponding signed type of the
373 // wint_t type, then it should match.
374 if (PromoArg->hasSignedIntegerRepresentation() &&
375 C.getCorrespondingUnsignedType(PromoArg) == WInt)
376 return true;
377
378 return WInt == PromoArg;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000379 }
Ted Kremenek02087932010-07-16 02:11:22 +0000380
381 case CPointerTy:
Anders Carlsson3fd50312010-11-06 14:58:53 +0000382 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
Ted Kremenekc08c4752012-03-15 21:22:27 +0000383 argTy->isBlockPointerType() || argTy->isNullPtrType();
Ted Kremenek02087932010-07-16 02:11:22 +0000384
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000385 case ObjCPointerTy: {
386 if (argTy->getAs<ObjCObjectPointerType>() ||
387 argTy->getAs<BlockPointerType>())
388 return true;
389
390 // Handle implicit toll-free bridging.
391 if (const PointerType *PT = argTy->getAs<PointerType>()) {
392 // Things such as CFTypeRef are really just opaque pointers
393 // to C structs representing CF types that can often be bridged
394 // to Objective-C objects. Since the compiler doesn't know which
395 // structs can be toll-free bridged, we just accept them all.
396 QualType pointee = PT->getPointeeType();
397 if (pointee->getAsStructureType() || pointee->isVoidType())
398 return true;
399 }
400 return false;
401 }
Ted Kremenek02087932010-07-16 02:11:22 +0000402 }
403
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000404 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000405}
406
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000407QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000408 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000409 switch (K) {
410 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000411 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000412 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000413 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000414 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000415 Res = C.CharTy;
416 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000417 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000418 Res = T;
419 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000420 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000421 Res = C.getPointerType(C.CharTy);
422 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000423 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000424 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000425 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000426 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000427 Res = C.ObjCBuiltinIdTy;
428 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000429 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000430 Res = C.VoidPtrTy;
431 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000432 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000433 Res = C.getWIntType();
434 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000435 }
Ted Kremenek02087932010-07-16 02:11:22 +0000436 }
437
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000438 if (Ptr)
439 Res = C.getPointerType(Res);
440 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000441}
442
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000443std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000444 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000445
446 std::string Alias;
447 if (Name) {
448 // Use a specific name for this type, e.g. "size_t".
449 Alias = Name;
450 if (Ptr) {
451 // If ArgType is actually a pointer to T, append an asterisk.
452 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
453 }
454 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
455 if (S == Alias)
456 Alias.clear();
457 }
458
459 if (!Alias.empty())
460 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000461 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000462}
463
464
Ted Kremenek02087932010-07-16 02:11:22 +0000465//===----------------------------------------------------------------------===//
466// Methods on OptionalAmount.
467//===----------------------------------------------------------------------===//
468
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000469ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000470analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
471 return Ctx.IntTy;
472}
473
474//===----------------------------------------------------------------------===//
475// Methods on LengthModifier.
476//===----------------------------------------------------------------------===//
477
478const char *
479analyze_format_string::LengthModifier::toString() const {
480 switch (kind) {
481 case AsChar:
482 return "hh";
483 case AsShort:
484 return "h";
485 case AsLong: // or AsWideChar
486 return "l";
487 case AsLongLong:
488 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000489 case AsQuad:
490 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000491 case AsIntMax:
492 return "j";
493 case AsSizeT:
494 return "z";
495 case AsPtrDiff:
496 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000497 case AsInt32:
498 return "I32";
499 case AsInt3264:
500 return "I";
501 case AsInt64:
502 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000503 case AsLongDouble:
504 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000505 case AsAllocate:
506 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000507 case AsMAllocate:
508 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000509 case AsWide:
510 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000511 case None:
512 return "";
513 }
Craig Topper25542942014-05-20 04:30:07 +0000514 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000515}
516
517//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000518// Methods on ConversionSpecifier.
519//===----------------------------------------------------------------------===//
520
521const char *ConversionSpecifier::toString() const {
522 switch (kind) {
523 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000524 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000525 case iArg: return "i";
526 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000527 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000528 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000529 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000530 case xArg: return "x";
531 case XArg: return "X";
532 case fArg: return "f";
533 case FArg: return "F";
534 case eArg: return "e";
535 case EArg: return "E";
536 case gArg: return "g";
537 case GArg: return "G";
538 case aArg: return "a";
539 case AArg: return "A";
540 case cArg: return "c";
541 case sArg: return "s";
542 case pArg: return "p";
543 case nArg: return "n";
544 case PercentArg: return "%";
545 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000546 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000547
David Majnemer3cba4952013-08-21 21:54:46 +0000548 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000549 case CArg: return "C";
550 case SArg: return "S";
551
552 // Objective-C specific specifiers.
553 case ObjCObjArg: return "@";
554
555 // GlibC specific specifiers.
556 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000557
558 // MS specific specifiers.
559 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000560 }
Craig Topper25542942014-05-20 04:30:07 +0000561 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000562}
563
David Blaikie05785d12013-02-20 22:23:23 +0000564Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000565ConversionSpecifier::getStandardSpecifier() const {
566 ConversionSpecifier::Kind NewKind;
567
568 switch (getKind()) {
569 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000570 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000571 case DArg:
572 NewKind = dArg;
573 break;
574 case UArg:
575 NewKind = uArg;
576 break;
577 case OArg:
578 NewKind = oArg;
579 break;
580 }
581
582 ConversionSpecifier FixedCS(*this);
583 FixedCS.setKind(NewKind);
584 return FixedCS;
585}
586
Hans Wennborga8b042d2011-12-09 11:11:07 +0000587//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000588// Methods on OptionalAmount.
589//===----------------------------------------------------------------------===//
590
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000591void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000592 switch (hs) {
593 case Invalid:
594 case NotSpecified:
595 return;
596 case Arg:
597 if (UsesDotPrefix)
598 os << ".";
599 if (usesPositionalArg())
600 os << "*" << getPositionalArgIndex() << "$";
601 else
602 os << "*";
603 break;
604 case Constant:
605 if (UsesDotPrefix)
606 os << ".";
607 os << amt;
608 break;
609 }
610}
611
Jordan Rose92303592012-09-08 04:00:03 +0000612bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000613 switch (LM.getKind()) {
614 case LengthModifier::None:
615 return true;
616
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000617 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000618 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000619 if (Target.getTriple().isOSMSVCRT()) {
620 switch (CS.getKind()) {
621 case ConversionSpecifier::cArg:
622 case ConversionSpecifier::CArg:
623 case ConversionSpecifier::sArg:
624 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000625 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000626 return true;
627 default:
628 break;
629 }
630 }
631 // Fall through.
632 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000633 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000634 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000635 case LengthModifier::AsIntMax:
636 case LengthModifier::AsSizeT:
637 case LengthModifier::AsPtrDiff:
638 switch (CS.getKind()) {
639 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000640 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000641 case ConversionSpecifier::iArg:
642 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000643 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000644 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000645 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000646 case ConversionSpecifier::xArg:
647 case ConversionSpecifier::XArg:
648 case ConversionSpecifier::nArg:
649 return true;
650 default:
651 return false;
652 }
653
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000654 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000655 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000656 switch (CS.getKind()) {
657 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000658 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000659 case ConversionSpecifier::iArg:
660 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000661 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000662 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000663 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000664 case ConversionSpecifier::xArg:
665 case ConversionSpecifier::XArg:
666 case ConversionSpecifier::aArg:
667 case ConversionSpecifier::AArg:
668 case ConversionSpecifier::fArg:
669 case ConversionSpecifier::FArg:
670 case ConversionSpecifier::eArg:
671 case ConversionSpecifier::EArg:
672 case ConversionSpecifier::gArg:
673 case ConversionSpecifier::GArg:
674 case ConversionSpecifier::nArg:
675 case ConversionSpecifier::cArg:
676 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000677 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000678 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000679 return true;
680 default:
681 return false;
682 }
683
684 case LengthModifier::AsLongDouble:
685 switch (CS.getKind()) {
686 case ConversionSpecifier::aArg:
687 case ConversionSpecifier::AArg:
688 case ConversionSpecifier::fArg:
689 case ConversionSpecifier::FArg:
690 case ConversionSpecifier::eArg:
691 case ConversionSpecifier::EArg:
692 case ConversionSpecifier::gArg:
693 case ConversionSpecifier::GArg:
694 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000695 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000696 case ConversionSpecifier::dArg:
697 case ConversionSpecifier::iArg:
698 case ConversionSpecifier::oArg:
699 case ConversionSpecifier::uArg:
700 case ConversionSpecifier::xArg:
701 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000702 return !Target.getTriple().isOSDarwin() &&
703 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000704 default:
705 return false;
706 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000707
708 case LengthModifier::AsAllocate:
709 switch (CS.getKind()) {
710 case ConversionSpecifier::sArg:
711 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000712 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000713 return true;
714 default:
715 return false;
716 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000717
718 case LengthModifier::AsMAllocate:
719 switch (CS.getKind()) {
720 case ConversionSpecifier::cArg:
721 case ConversionSpecifier::CArg:
722 case ConversionSpecifier::sArg:
723 case ConversionSpecifier::SArg:
724 case ConversionSpecifier::ScanListArg:
725 return true;
726 default:
727 return false;
728 }
David Majnemer3cba4952013-08-21 21:54:46 +0000729 case LengthModifier::AsInt32:
730 case LengthModifier::AsInt3264:
731 case LengthModifier::AsInt64:
732 switch (CS.getKind()) {
733 case ConversionSpecifier::dArg:
734 case ConversionSpecifier::iArg:
735 case ConversionSpecifier::oArg:
736 case ConversionSpecifier::uArg:
737 case ConversionSpecifier::xArg:
738 case ConversionSpecifier::XArg:
739 return Target.getTriple().isOSMSVCRT();
740 default:
741 return false;
742 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000743 case LengthModifier::AsWide:
744 switch (CS.getKind()) {
745 case ConversionSpecifier::cArg:
746 case ConversionSpecifier::CArg:
747 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000748 case ConversionSpecifier::SArg:
749 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000750 return Target.getTriple().isOSMSVCRT();
751 default:
752 return false;
753 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000754 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000755 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000756}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000757
758bool FormatSpecifier::hasStandardLengthModifier() const {
759 switch (LM.getKind()) {
760 case LengthModifier::None:
761 case LengthModifier::AsChar:
762 case LengthModifier::AsShort:
763 case LengthModifier::AsLong:
764 case LengthModifier::AsLongLong:
765 case LengthModifier::AsIntMax:
766 case LengthModifier::AsSizeT:
767 case LengthModifier::AsPtrDiff:
768 case LengthModifier::AsLongDouble:
769 return true;
770 case LengthModifier::AsAllocate:
771 case LengthModifier::AsMAllocate:
772 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000773 case LengthModifier::AsInt32:
774 case LengthModifier::AsInt3264:
775 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000776 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000777 return false;
778 }
779 llvm_unreachable("Invalid LengthModifier Kind!");
780}
781
782bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
783 switch (CS.getKind()) {
784 case ConversionSpecifier::cArg:
785 case ConversionSpecifier::dArg:
786 case ConversionSpecifier::iArg:
787 case ConversionSpecifier::oArg:
788 case ConversionSpecifier::uArg:
789 case ConversionSpecifier::xArg:
790 case ConversionSpecifier::XArg:
791 case ConversionSpecifier::fArg:
792 case ConversionSpecifier::FArg:
793 case ConversionSpecifier::eArg:
794 case ConversionSpecifier::EArg:
795 case ConversionSpecifier::gArg:
796 case ConversionSpecifier::GArg:
797 case ConversionSpecifier::aArg:
798 case ConversionSpecifier::AArg:
799 case ConversionSpecifier::sArg:
800 case ConversionSpecifier::pArg:
801 case ConversionSpecifier::nArg:
802 case ConversionSpecifier::ObjCObjArg:
803 case ConversionSpecifier::ScanListArg:
804 case ConversionSpecifier::PercentArg:
805 return true;
806 case ConversionSpecifier::CArg:
807 case ConversionSpecifier::SArg:
808 return LangOpt.ObjC1 || LangOpt.ObjC2;
809 case ConversionSpecifier::InvalidSpecifier:
810 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000811 case ConversionSpecifier::DArg:
812 case ConversionSpecifier::OArg:
813 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000814 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000815 return false;
816 }
817 llvm_unreachable("Invalid ConversionSpecifier Kind!");
818}
819
820bool FormatSpecifier::hasStandardLengthConversionCombination() const {
821 if (LM.getKind() == LengthModifier::AsLongDouble) {
822 switch(CS.getKind()) {
823 case ConversionSpecifier::dArg:
824 case ConversionSpecifier::iArg:
825 case ConversionSpecifier::oArg:
826 case ConversionSpecifier::uArg:
827 case ConversionSpecifier::xArg:
828 case ConversionSpecifier::XArg:
829 return false;
830 default:
831 return true;
832 }
833 }
834 return true;
835}
Hans Wennborg08574d32012-07-27 19:17:46 +0000836
David Blaikie05785d12013-02-20 22:23:23 +0000837Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000838 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
839 if (LM.getKind() == LengthModifier::AsLongDouble ||
840 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000841 LengthModifier FixedLM(LM);
842 FixedLM.setKind(LengthModifier::AsLongLong);
843 return FixedLM;
844 }
Jordan Rose92303592012-09-08 04:00:03 +0000845 }
846
David Blaikie7a30dc52013-02-21 01:47:18 +0000847 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000848}
849
Hans Wennborg08574d32012-07-27 19:17:46 +0000850bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
851 LengthModifier &LM) {
852 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
853 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
854
855 for (;;) {
856 const IdentifierInfo *Identifier = Typedef->getIdentifier();
857 if (Identifier->getName() == "size_t") {
858 LM.setKind(LengthModifier::AsSizeT);
859 return true;
860 } else if (Identifier->getName() == "ssize_t") {
861 // Not C99, but common in Unix.
862 LM.setKind(LengthModifier::AsSizeT);
863 return true;
864 } else if (Identifier->getName() == "intmax_t") {
865 LM.setKind(LengthModifier::AsIntMax);
866 return true;
867 } else if (Identifier->getName() == "uintmax_t") {
868 LM.setKind(LengthModifier::AsIntMax);
869 return true;
870 } else if (Identifier->getName() == "ptrdiff_t") {
871 LM.setKind(LengthModifier::AsPtrDiff);
872 return true;
873 }
874
875 QualType T = Typedef->getUnderlyingType();
876 if (!isa<TypedefType>(T))
877 break;
878
879 Typedef = cast<TypedefType>(T)->getDecl();
880 }
881 return false;
882}