blob: e9f6b88631af5b563e36aa4e717987a074c9cbae [file] [log] [blame]
Ted Kremenek02087932010-07-16 02:11:22 +00001// FormatString.cpp - Common stuff for handling printf/scanf formats -*- C++ -*-
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek02087932010-07-16 02:11:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Shared details for processing format strings of printf and scanf
10// (and friends).
11//
12//===----------------------------------------------------------------------===//
13
14#include "FormatStringParsing.h"
Hans Wennborg23926bd2011-12-15 10:25:47 +000015#include "clang/Basic/LangOptions.h"
Jordan Rose92303592012-09-08 04:00:03 +000016#include "clang/Basic/TargetInfo.h"
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +000017#include "llvm/Support/ConvertUTF.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.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000029FormatStringHandler::~FormatStringHandler() {}
Ted Kremenek02087932010-07-16 02:11:22 +000030
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
Matt Arsenault0ff50d42018-12-01 22:16:27 +0000181clang::analyze_format_string::ParseVectorModifier(FormatStringHandler &H,
182 FormatSpecifier &FS,
183 const char *&I,
184 const char *E,
185 const LangOptions &LO) {
186 if (!LO.OpenCL)
187 return false;
188
189 const char *Start = I;
190 if (*I == 'v') {
191 ++I;
192
193 if (I == E) {
194 H.HandleIncompleteSpecifier(Start, E - Start);
195 return true;
196 }
197
198 OptionalAmount NumElts = ParseAmount(I, E);
199 if (NumElts.getHowSpecified() != OptionalAmount::Constant) {
200 H.HandleIncompleteSpecifier(Start, E - Start);
201 return true;
202 }
203
204 FS.setVectorNumElts(NumElts);
205 }
206
207 return false;
208}
209
210bool
Ted Kremenek02087932010-07-16 02:11:22 +0000211clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
212 const char *&I,
Hans Wennborg23926bd2011-12-15 10:25:47 +0000213 const char *E,
214 const LangOptions &LO,
215 bool IsScanf) {
Ted Kremenek02087932010-07-16 02:11:22 +0000216 LengthModifier::Kind lmKind = LengthModifier::None;
217 const char *lmPosition = I;
218 switch (*I) {
219 default:
220 return false;
221 case 'h':
222 ++I;
Richard Trieucc3949d2016-02-18 22:34:54 +0000223 if (I != E && *I == 'h') {
224 ++I;
225 lmKind = LengthModifier::AsChar;
Matt Arsenault58fc8082019-01-29 20:49:54 +0000226 } else if (I != E && *I == 'l' && LO.OpenCL) {
227 ++I;
228 lmKind = LengthModifier::AsShortLong;
Richard Trieucc3949d2016-02-18 22:34:54 +0000229 } else {
230 lmKind = LengthModifier::AsShort;
231 }
Ted Kremenek02087932010-07-16 02:11:22 +0000232 break;
233 case 'l':
234 ++I;
Richard Trieucc3949d2016-02-18 22:34:54 +0000235 if (I != E && *I == 'l') {
236 ++I;
237 lmKind = LengthModifier::AsLongLong;
238 } else {
239 lmKind = LengthModifier::AsLong;
240 }
Ted Kremenek02087932010-07-16 02:11:22 +0000241 break;
242 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
243 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
244 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
245 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000246 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborg23926bd2011-12-15 10:25:47 +0000247 case 'a':
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000248 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
Hans Wennborg23926bd2011-12-15 10:25:47 +0000249 // For scanf in C90, look at the next character to see if this should
250 // be parsed as the GNU extension 'a' length modifier. If not, this
251 // will be parsed as a conversion specifier.
252 ++I;
253 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
254 lmKind = LengthModifier::AsAllocate;
255 break;
256 }
257 --I;
258 }
259 return false;
Hans Wennborg6073e312012-01-12 17:11:12 +0000260 case 'm':
261 if (IsScanf) {
262 lmKind = LengthModifier::AsMAllocate;
263 ++I;
264 break;
265 }
266 return false;
David Majnemer3cba4952013-08-21 21:54:46 +0000267 // printf: AsInt64, AsInt32, AsInt3264
268 // scanf: AsInt64
269 case 'I':
270 if (I + 1 != E && I + 2 != E) {
271 if (I[1] == '6' && I[2] == '4') {
272 I += 3;
273 lmKind = LengthModifier::AsInt64;
274 break;
275 }
276 if (IsScanf)
277 return false;
278
279 if (I[1] == '3' && I[2] == '2') {
280 I += 3;
281 lmKind = LengthModifier::AsInt32;
282 break;
283 }
284 }
285 ++I;
286 lmKind = LengthModifier::AsInt3264;
287 break;
Hans Wennborg68f42b92014-09-04 21:39:46 +0000288 case 'w':
289 lmKind = LengthModifier::AsWide; ++I; break;
Ted Kremenek02087932010-07-16 02:11:22 +0000290 }
291 LengthModifier lm(lmPosition, lmKind);
292 FS.setLengthModifier(lm);
293 return true;
294}
295
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +0000296bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
297 const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len) {
298 if (SpecifierBegin + 1 >= FmtStrEnd)
299 return false;
300
Justin Lebar90910552016-09-30 00:38:45 +0000301 const llvm::UTF8 *SB =
302 reinterpret_cast<const llvm::UTF8 *>(SpecifierBegin + 1);
303 const llvm::UTF8 *SE = reinterpret_cast<const llvm::UTF8 *>(FmtStrEnd);
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +0000304 const char FirstByte = *SB;
305
306 // If the invalid specifier is a multibyte UTF-8 string, return the
307 // total length accordingly so that the conversion specifier can be
308 // properly updated to reflect a complete UTF-8 specifier.
Justin Lebar90910552016-09-30 00:38:45 +0000309 unsigned NumBytes = llvm::getNumBytesForUTF8(FirstByte);
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +0000310 if (NumBytes == 1)
311 return false;
312 if (SB + NumBytes > SE)
313 return false;
314
315 Len = NumBytes + 1;
316 return true;
317}
318
Ted Kremenek02087932010-07-16 02:11:22 +0000319//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000320// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000321//===----------------------------------------------------------------------===//
322
Seth Cantrellb4802962015-03-04 03:12:10 +0000323clang::analyze_format_string::ArgType::MatchKind
324ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000325 if (Ptr) {
326 // It has to be a pointer.
327 const PointerType *PT = argTy->getAs<PointerType>();
328 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000329 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000330
331 // We cannot write through a const qualified pointer.
332 if (PT->getPointeeType().isConstQualified())
Seth Cantrellb4802962015-03-04 03:12:10 +0000333 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000334
335 argTy = PT->getPointeeType();
336 }
337
Ted Kremenek02087932010-07-16 02:11:22 +0000338 switch (K) {
339 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000340 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000341
342 case UnknownTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000343 return Match;
344
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000345 case AnyCharTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000346 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
347 // If the enum is incomplete we know nothing about the underlying type.
348 // Assume that it's 'int'.
349 if (!ETy->getDecl()->isComplete())
350 return NoMatch;
Jordan Rose98709982012-06-04 22:48:57 +0000351 argTy = ETy->getDecl()->getIntegerType();
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000352 }
Jordan Rose98709982012-06-04 22:48:57 +0000353
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000354 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
355 switch (BT->getKind()) {
356 default:
357 break;
358 case BuiltinType::Char_S:
359 case BuiltinType::SChar:
360 case BuiltinType::UChar:
361 case BuiltinType::Char_U:
Erik Pilkington5741d192019-09-18 19:05:14 +0000362 case BuiltinType::Bool:
Seth Cantrellb4802962015-03-04 03:12:10 +0000363 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000364 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000365 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000366 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000367
Ted Kremenek02087932010-07-16 02:11:22 +0000368 case SpecificTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000369 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
370 // If the enum is incomplete we know nothing about the underlying type.
371 // Assume that it's 'int'.
372 if (!ETy->getDecl()->isComplete())
373 argTy = C.IntTy;
374 else
375 argTy = ETy->getDecl()->getIntegerType();
376 }
Ted Kremenek02087932010-07-16 02:11:22 +0000377 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000378
Nick Lewycky45ccba62011-12-02 23:21:43 +0000379 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000380 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000381 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000382 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000383 switch (BT->getKind()) {
384 default:
385 break;
386 case BuiltinType::Char_S:
387 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000388 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000389 case BuiltinType::UChar:
Erik Pilkington5741d192019-09-18 19:05:14 +0000390 case BuiltinType::Bool:
Nathan Huckleberrycc01d642019-08-23 18:01:57 +0000391 if (T == C.UnsignedShortTy || T == C.ShortTy)
Erik Pilkingtonf7766b1e2019-10-04 19:20:27 +0000392 return NoMatchTypeConfusion;
Seth Cantrellb4802962015-03-04 03:12:10 +0000393 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
394 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000395 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000396 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000397 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000398 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000399 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000400 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000401 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000402 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000403 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000404 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000405 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000406 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000407 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000408 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000409 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000410 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000411 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000412 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000413 }
414
415 case CStrTy: {
416 const PointerType *PT = argTy->getAs<PointerType>();
417 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000418 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000419 QualType pointeeTy = PT->getPointeeType();
420 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
421 switch (BT->getKind()) {
422 case BuiltinType::Void:
423 case BuiltinType::Char_U:
424 case BuiltinType::UChar:
425 case BuiltinType::Char_S:
426 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000427 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000428 default:
429 break;
430 }
431
Seth Cantrellb4802962015-03-04 03:12:10 +0000432 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000433 }
434
435 case WCStrTy: {
436 const PointerType *PT = argTy->getAs<PointerType>();
437 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000438 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000439 QualType pointeeTy =
440 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000441 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000442 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000443
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000444 case WIntTy: {
James Molloy36365542012-05-04 10:55:22 +0000445 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Saleem Abdulrasool29bf94d2018-09-19 18:13:34 +0000446
447 if (C.getCanonicalType(argTy).getUnqualifiedType() == WInt)
448 return Match;
449
450 QualType PromoArg = argTy->isPromotableIntegerType()
451 ? C.getPromotedIntegerType(argTy)
452 : argTy;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000453 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000454
James Molloy36365542012-05-04 10:55:22 +0000455 // If the promoted argument is the corresponding signed type of the
456 // wint_t type, then it should match.
457 if (PromoArg->hasSignedIntegerRepresentation() &&
458 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000459 return Match;
James Molloy36365542012-05-04 10:55:22 +0000460
Seth Cantrellb4802962015-03-04 03:12:10 +0000461 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000462 }
Ted Kremenek02087932010-07-16 02:11:22 +0000463
464 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000465 if (argTy->isVoidPointerType()) {
466 return Match;
467 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
468 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
469 return NoMatchPedantic;
470 } else {
471 return NoMatch;
472 }
Ted Kremenek02087932010-07-16 02:11:22 +0000473
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000474 case ObjCPointerTy: {
475 if (argTy->getAs<ObjCObjectPointerType>() ||
476 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000477 return Match;
478
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000479 // Handle implicit toll-free bridging.
480 if (const PointerType *PT = argTy->getAs<PointerType>()) {
481 // Things such as CFTypeRef are really just opaque pointers
482 // to C structs representing CF types that can often be bridged
483 // to Objective-C objects. Since the compiler doesn't know which
484 // structs can be toll-free bridged, we just accept them all.
485 QualType pointee = PT->getPointeeType();
486 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000487 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000488 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000489 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000490 }
Ted Kremenek02087932010-07-16 02:11:22 +0000491 }
492
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000493 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000494}
495
Matt Arsenault0ff50d42018-12-01 22:16:27 +0000496ArgType ArgType::makeVectorType(ASTContext &C, unsigned NumElts) const {
Matt Arsenault58fc8082019-01-29 20:49:54 +0000497 // Check for valid vector element types.
498 if (T.isNull())
Matt Arsenault0ff50d42018-12-01 22:16:27 +0000499 return ArgType::Invalid();
500
501 QualType Vec = C.getExtVectorType(T, NumElts);
502 return ArgType(Vec, Name);
503}
504
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000505QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000506 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000507 switch (K) {
508 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000509 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000510 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000511 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000512 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000513 Res = C.CharTy;
514 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000515 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000516 Res = T;
517 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000518 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000519 Res = C.getPointerType(C.CharTy);
520 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000521 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000522 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000523 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000524 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000525 Res = C.ObjCBuiltinIdTy;
526 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000527 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000528 Res = C.VoidPtrTy;
529 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000530 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000531 Res = C.getWIntType();
532 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000533 }
Ted Kremenek02087932010-07-16 02:11:22 +0000534 }
535
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000536 if (Ptr)
537 Res = C.getPointerType(Res);
538 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000539}
540
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000541std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Jessica Clarke5fee6932020-04-28 23:43:48 +0100542 std::string S = getRepresentativeType(C).getAsString(C.getPrintingPolicy());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000543
544 std::string Alias;
545 if (Name) {
546 // Use a specific name for this type, e.g. "size_t".
547 Alias = Name;
548 if (Ptr) {
549 // If ArgType is actually a pointer to T, append an asterisk.
550 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
551 }
552 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
553 if (S == Alias)
554 Alias.clear();
555 }
556
557 if (!Alias.empty())
558 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000559 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000560}
561
562
Ted Kremenek02087932010-07-16 02:11:22 +0000563//===----------------------------------------------------------------------===//
564// Methods on OptionalAmount.
565//===----------------------------------------------------------------------===//
566
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000567ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000568analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
569 return Ctx.IntTy;
570}
571
572//===----------------------------------------------------------------------===//
573// Methods on LengthModifier.
574//===----------------------------------------------------------------------===//
575
576const char *
577analyze_format_string::LengthModifier::toString() const {
578 switch (kind) {
579 case AsChar:
580 return "hh";
581 case AsShort:
582 return "h";
Matt Arsenault58fc8082019-01-29 20:49:54 +0000583 case AsShortLong:
584 return "hl";
Ted Kremenek02087932010-07-16 02:11:22 +0000585 case AsLong: // or AsWideChar
586 return "l";
587 case AsLongLong:
588 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000589 case AsQuad:
590 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000591 case AsIntMax:
592 return "j";
593 case AsSizeT:
594 return "z";
595 case AsPtrDiff:
596 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000597 case AsInt32:
598 return "I32";
599 case AsInt3264:
600 return "I";
601 case AsInt64:
602 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000603 case AsLongDouble:
604 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000605 case AsAllocate:
606 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000607 case AsMAllocate:
608 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000609 case AsWide:
610 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000611 case None:
612 return "";
613 }
Craig Topper25542942014-05-20 04:30:07 +0000614 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000615}
616
617//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000618// Methods on ConversionSpecifier.
619//===----------------------------------------------------------------------===//
620
621const char *ConversionSpecifier::toString() const {
622 switch (kind) {
623 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000624 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000625 case iArg: return "i";
626 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000627 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000628 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000629 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000630 case xArg: return "x";
631 case XArg: return "X";
632 case fArg: return "f";
633 case FArg: return "F";
634 case eArg: return "e";
635 case EArg: return "E";
636 case gArg: return "g";
637 case GArg: return "G";
638 case aArg: return "a";
639 case AArg: return "A";
640 case cArg: return "c";
641 case sArg: return "s";
642 case pArg: return "p";
Mehdi Amini06d367c2016-10-24 20:39:34 +0000643 case PArg:
644 return "P";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000645 case nArg: return "n";
646 case PercentArg: return "%";
647 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000648 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000649
David Majnemer3cba4952013-08-21 21:54:46 +0000650 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000651 case CArg: return "C";
652 case SArg: return "S";
653
654 // Objective-C specific specifiers.
655 case ObjCObjArg: return "@";
656
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000657 // FreeBSD kernel specific specifiers.
658 case FreeBSDbArg: return "b";
659 case FreeBSDDArg: return "D";
660 case FreeBSDrArg: return "r";
661 case FreeBSDyArg: return "y";
662
Hans Wennborga8b042d2011-12-09 11:11:07 +0000663 // GlibC specific specifiers.
664 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000665
666 // MS specific specifiers.
667 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000668 }
Craig Topper25542942014-05-20 04:30:07 +0000669 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000670}
671
David Blaikie05785d12013-02-20 22:23:23 +0000672Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000673ConversionSpecifier::getStandardSpecifier() const {
674 ConversionSpecifier::Kind NewKind;
Fangrui Song6907ce22018-07-30 19:24:48 +0000675
Jordan Rose4c266aa2012-09-13 02:11:15 +0000676 switch (getKind()) {
677 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000678 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000679 case DArg:
680 NewKind = dArg;
681 break;
682 case UArg:
683 NewKind = uArg;
684 break;
685 case OArg:
686 NewKind = oArg;
687 break;
688 }
689
690 ConversionSpecifier FixedCS(*this);
691 FixedCS.setKind(NewKind);
692 return FixedCS;
693}
694
Hans Wennborga8b042d2011-12-09 11:11:07 +0000695//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000696// Methods on OptionalAmount.
697//===----------------------------------------------------------------------===//
698
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000699void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000700 switch (hs) {
701 case Invalid:
702 case NotSpecified:
703 return;
704 case Arg:
705 if (UsesDotPrefix)
706 os << ".";
707 if (usesPositionalArg())
708 os << "*" << getPositionalArgIndex() << "$";
709 else
710 os << "*";
711 break;
712 case Constant:
713 if (UsesDotPrefix)
714 os << ".";
715 os << amt;
716 break;
717 }
718}
719
Matt Arsenault58fc8082019-01-29 20:49:54 +0000720bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target,
721 const LangOptions &LO) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000722 switch (LM.getKind()) {
723 case LengthModifier::None:
724 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000725
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000726 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000727 case LengthModifier::AsShort:
Matt Arsenault58fc8082019-01-29 20:49:54 +0000728 // Length modifier only applies to FP vectors.
729 if (LO.OpenCL && CS.isDoubleArg())
730 return !VectorNumElts.isInvalid();
731
Hans Wennborg1b231582014-09-04 21:39:52 +0000732 if (Target.getTriple().isOSMSVCRT()) {
733 switch (CS.getKind()) {
734 case ConversionSpecifier::cArg:
735 case ConversionSpecifier::CArg:
736 case ConversionSpecifier::sArg:
737 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000738 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000739 return true;
740 default:
741 break;
742 }
743 }
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000744 LLVM_FALLTHROUGH;
Hans Wennborg1b231582014-09-04 21:39:52 +0000745 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000746 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000747 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000748 case LengthModifier::AsIntMax:
749 case LengthModifier::AsSizeT:
750 case LengthModifier::AsPtrDiff:
751 switch (CS.getKind()) {
752 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000753 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000754 case ConversionSpecifier::iArg:
755 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000756 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000757 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000758 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000759 case ConversionSpecifier::xArg:
760 case ConversionSpecifier::XArg:
761 case ConversionSpecifier::nArg:
762 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000763 case ConversionSpecifier::FreeBSDrArg:
764 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000765 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000766 default:
767 return false;
768 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000769
Matt Arsenault58fc8082019-01-29 20:49:54 +0000770 case LengthModifier::AsShortLong:
771 return LO.OpenCL && !VectorNumElts.isInvalid();
772
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000773 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000774 case LengthModifier::AsLong: // or AsWideChar
Matt Arsenault58fc8082019-01-29 20:49:54 +0000775 if (CS.isDoubleArg()) {
776 // Invalid for OpenCL FP scalars.
777 if (LO.OpenCL && VectorNumElts.isInvalid())
778 return false;
779 return true;
780 }
781
Ted Kremenekea28f832010-07-20 20:04:42 +0000782 switch (CS.getKind()) {
783 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000784 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000785 case ConversionSpecifier::iArg:
786 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000787 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000788 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000789 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000790 case ConversionSpecifier::xArg:
791 case ConversionSpecifier::XArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000792 case ConversionSpecifier::nArg:
793 case ConversionSpecifier::cArg:
794 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000795 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000796 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000797 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000798 case ConversionSpecifier::FreeBSDrArg:
799 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000800 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000801 default:
802 return false;
803 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000804
Ted Kremenekea28f832010-07-20 20:04:42 +0000805 case LengthModifier::AsLongDouble:
806 switch (CS.getKind()) {
807 case ConversionSpecifier::aArg:
808 case ConversionSpecifier::AArg:
809 case ConversionSpecifier::fArg:
810 case ConversionSpecifier::FArg:
811 case ConversionSpecifier::eArg:
812 case ConversionSpecifier::EArg:
813 case ConversionSpecifier::gArg:
814 case ConversionSpecifier::GArg:
815 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000816 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000817 case ConversionSpecifier::dArg:
818 case ConversionSpecifier::iArg:
819 case ConversionSpecifier::oArg:
820 case ConversionSpecifier::uArg:
821 case ConversionSpecifier::xArg:
822 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000823 return !Target.getTriple().isOSDarwin() &&
824 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000825 default:
826 return false;
827 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000828
829 case LengthModifier::AsAllocate:
830 switch (CS.getKind()) {
831 case ConversionSpecifier::sArg:
832 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000833 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000834 return true;
835 default:
836 return false;
837 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000838
839 case LengthModifier::AsMAllocate:
840 switch (CS.getKind()) {
841 case ConversionSpecifier::cArg:
842 case ConversionSpecifier::CArg:
843 case ConversionSpecifier::sArg:
844 case ConversionSpecifier::SArg:
845 case ConversionSpecifier::ScanListArg:
846 return true;
847 default:
848 return false;
849 }
David Majnemer3cba4952013-08-21 21:54:46 +0000850 case LengthModifier::AsInt32:
851 case LengthModifier::AsInt3264:
852 case LengthModifier::AsInt64:
853 switch (CS.getKind()) {
854 case ConversionSpecifier::dArg:
855 case ConversionSpecifier::iArg:
856 case ConversionSpecifier::oArg:
857 case ConversionSpecifier::uArg:
858 case ConversionSpecifier::xArg:
859 case ConversionSpecifier::XArg:
860 return Target.getTriple().isOSMSVCRT();
861 default:
862 return false;
863 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000864 case LengthModifier::AsWide:
865 switch (CS.getKind()) {
866 case ConversionSpecifier::cArg:
867 case ConversionSpecifier::CArg:
868 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000869 case ConversionSpecifier::SArg:
870 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000871 return Target.getTriple().isOSMSVCRT();
872 default:
873 return false;
874 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000875 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000876 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000877}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000878
879bool FormatSpecifier::hasStandardLengthModifier() const {
880 switch (LM.getKind()) {
881 case LengthModifier::None:
882 case LengthModifier::AsChar:
883 case LengthModifier::AsShort:
884 case LengthModifier::AsLong:
885 case LengthModifier::AsLongLong:
886 case LengthModifier::AsIntMax:
887 case LengthModifier::AsSizeT:
888 case LengthModifier::AsPtrDiff:
889 case LengthModifier::AsLongDouble:
890 return true;
891 case LengthModifier::AsAllocate:
892 case LengthModifier::AsMAllocate:
893 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000894 case LengthModifier::AsInt32:
895 case LengthModifier::AsInt3264:
896 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000897 case LengthModifier::AsWide:
Matt Arsenault58fc8082019-01-29 20:49:54 +0000898 case LengthModifier::AsShortLong: // ???
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000899 return false;
900 }
901 llvm_unreachable("Invalid LengthModifier Kind!");
902}
903
Nico Webereffdb192015-05-18 02:41:17 +0000904bool FormatSpecifier::hasStandardConversionSpecifier(
905 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000906 switch (CS.getKind()) {
907 case ConversionSpecifier::cArg:
908 case ConversionSpecifier::dArg:
909 case ConversionSpecifier::iArg:
910 case ConversionSpecifier::oArg:
911 case ConversionSpecifier::uArg:
912 case ConversionSpecifier::xArg:
913 case ConversionSpecifier::XArg:
914 case ConversionSpecifier::fArg:
915 case ConversionSpecifier::FArg:
916 case ConversionSpecifier::eArg:
917 case ConversionSpecifier::EArg:
918 case ConversionSpecifier::gArg:
919 case ConversionSpecifier::GArg:
920 case ConversionSpecifier::aArg:
921 case ConversionSpecifier::AArg:
922 case ConversionSpecifier::sArg:
923 case ConversionSpecifier::pArg:
924 case ConversionSpecifier::nArg:
925 case ConversionSpecifier::ObjCObjArg:
926 case ConversionSpecifier::ScanListArg:
927 case ConversionSpecifier::PercentArg:
Mehdi Amini06d367c2016-10-24 20:39:34 +0000928 case ConversionSpecifier::PArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000929 return true;
930 case ConversionSpecifier::CArg:
931 case ConversionSpecifier::SArg:
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000932 return LangOpt.ObjC;
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000933 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000934 case ConversionSpecifier::FreeBSDbArg:
935 case ConversionSpecifier::FreeBSDDArg:
936 case ConversionSpecifier::FreeBSDrArg:
937 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000938 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000939 case ConversionSpecifier::DArg:
940 case ConversionSpecifier::OArg:
941 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000942 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000943 return false;
944 }
945 llvm_unreachable("Invalid ConversionSpecifier Kind!");
946}
947
948bool FormatSpecifier::hasStandardLengthConversionCombination() const {
949 if (LM.getKind() == LengthModifier::AsLongDouble) {
950 switch(CS.getKind()) {
951 case ConversionSpecifier::dArg:
952 case ConversionSpecifier::iArg:
953 case ConversionSpecifier::oArg:
954 case ConversionSpecifier::uArg:
955 case ConversionSpecifier::xArg:
956 case ConversionSpecifier::XArg:
957 return false;
958 default:
959 return true;
960 }
961 }
962 return true;
963}
Hans Wennborg08574d32012-07-27 19:17:46 +0000964
David Blaikie05785d12013-02-20 22:23:23 +0000965Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000966 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
967 if (LM.getKind() == LengthModifier::AsLongDouble ||
968 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000969 LengthModifier FixedLM(LM);
970 FixedLM.setKind(LengthModifier::AsLongLong);
971 return FixedLM;
972 }
Jordan Rose92303592012-09-08 04:00:03 +0000973 }
974
David Blaikie7a30dc52013-02-21 01:47:18 +0000975 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000976}
977
Hans Wennborg08574d32012-07-27 19:17:46 +0000978bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
979 LengthModifier &LM) {
980 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
981 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
982
983 for (;;) {
984 const IdentifierInfo *Identifier = Typedef->getIdentifier();
985 if (Identifier->getName() == "size_t") {
986 LM.setKind(LengthModifier::AsSizeT);
987 return true;
988 } else if (Identifier->getName() == "ssize_t") {
989 // Not C99, but common in Unix.
990 LM.setKind(LengthModifier::AsSizeT);
991 return true;
992 } else if (Identifier->getName() == "intmax_t") {
993 LM.setKind(LengthModifier::AsIntMax);
994 return true;
995 } else if (Identifier->getName() == "uintmax_t") {
996 LM.setKind(LengthModifier::AsIntMax);
997 return true;
998 } else if (Identifier->getName() == "ptrdiff_t") {
999 LM.setKind(LengthModifier::AsPtrDiff);
1000 return true;
1001 }
1002
1003 QualType T = Typedef->getUnderlyingType();
1004 if (!isa<TypedefType>(T))
1005 break;
1006
1007 Typedef = cast<TypedefType>(T)->getDecl();
1008 }
1009 return false;
1010}