blob: 578d5bc567338927a637dd831ef9d0789ddca1d0 [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:
Seth Cantrellb4802962015-03-04 03:12:10 +0000362 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000363 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000364 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000365 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000366
Ted Kremenek02087932010-07-16 02:11:22 +0000367 case SpecificTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000368 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
369 // If the enum is incomplete we know nothing about the underlying type.
370 // Assume that it's 'int'.
371 if (!ETy->getDecl()->isComplete())
372 argTy = C.IntTy;
373 else
374 argTy = ETy->getDecl()->getIntegerType();
375 }
Ted Kremenek02087932010-07-16 02:11:22 +0000376 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000377
Nick Lewycky45ccba62011-12-02 23:21:43 +0000378 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000379 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000380 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000381 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000382 switch (BT->getKind()) {
383 default:
384 break;
385 case BuiltinType::Char_S:
386 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000387 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000388 case BuiltinType::UChar:
389 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
390 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000391 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000392 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000393 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000394 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000395 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000396 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000397 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000398 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000399 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000400 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000401 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000402 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000403 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000404 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000405 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000406 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000407 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000408 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000409 }
410
411 case CStrTy: {
412 const PointerType *PT = argTy->getAs<PointerType>();
413 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000414 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000415 QualType pointeeTy = PT->getPointeeType();
416 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
417 switch (BT->getKind()) {
418 case BuiltinType::Void:
419 case BuiltinType::Char_U:
420 case BuiltinType::UChar:
421 case BuiltinType::Char_S:
422 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000423 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000424 default:
425 break;
426 }
427
Seth Cantrellb4802962015-03-04 03:12:10 +0000428 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000429 }
430
431 case WCStrTy: {
432 const PointerType *PT = argTy->getAs<PointerType>();
433 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000434 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000435 QualType pointeeTy =
436 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000437 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000438 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000439
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000440 case WIntTy: {
James Molloy36365542012-05-04 10:55:22 +0000441 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Saleem Abdulrasool29bf94d2018-09-19 18:13:34 +0000442
443 if (C.getCanonicalType(argTy).getUnqualifiedType() == WInt)
444 return Match;
445
446 QualType PromoArg = argTy->isPromotableIntegerType()
447 ? C.getPromotedIntegerType(argTy)
448 : argTy;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000449 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000450
James Molloy36365542012-05-04 10:55:22 +0000451 // If the promoted argument is the corresponding signed type of the
452 // wint_t type, then it should match.
453 if (PromoArg->hasSignedIntegerRepresentation() &&
454 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000455 return Match;
James Molloy36365542012-05-04 10:55:22 +0000456
Seth Cantrellb4802962015-03-04 03:12:10 +0000457 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000458 }
Ted Kremenek02087932010-07-16 02:11:22 +0000459
460 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000461 if (argTy->isVoidPointerType()) {
462 return Match;
463 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
464 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
465 return NoMatchPedantic;
466 } else {
467 return NoMatch;
468 }
Ted Kremenek02087932010-07-16 02:11:22 +0000469
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000470 case ObjCPointerTy: {
471 if (argTy->getAs<ObjCObjectPointerType>() ||
472 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000473 return Match;
474
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000475 // Handle implicit toll-free bridging.
476 if (const PointerType *PT = argTy->getAs<PointerType>()) {
477 // Things such as CFTypeRef are really just opaque pointers
478 // to C structs representing CF types that can often be bridged
479 // to Objective-C objects. Since the compiler doesn't know which
480 // structs can be toll-free bridged, we just accept them all.
481 QualType pointee = PT->getPointeeType();
482 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000483 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000484 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000485 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000486 }
Ted Kremenek02087932010-07-16 02:11:22 +0000487 }
488
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000489 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000490}
491
Matt Arsenault0ff50d42018-12-01 22:16:27 +0000492ArgType ArgType::makeVectorType(ASTContext &C, unsigned NumElts) const {
Matt Arsenault58fc8082019-01-29 20:49:54 +0000493 // Check for valid vector element types.
494 if (T.isNull())
Matt Arsenault0ff50d42018-12-01 22:16:27 +0000495 return ArgType::Invalid();
496
497 QualType Vec = C.getExtVectorType(T, NumElts);
498 return ArgType(Vec, Name);
499}
500
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000501QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000502 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000503 switch (K) {
504 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000505 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000506 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000507 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000508 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000509 Res = C.CharTy;
510 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000511 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000512 Res = T;
513 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000514 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000515 Res = C.getPointerType(C.CharTy);
516 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000517 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000518 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000519 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000520 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000521 Res = C.ObjCBuiltinIdTy;
522 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000523 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000524 Res = C.VoidPtrTy;
525 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000526 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000527 Res = C.getWIntType();
528 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000529 }
Ted Kremenek02087932010-07-16 02:11:22 +0000530 }
531
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000532 if (Ptr)
533 Res = C.getPointerType(Res);
534 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000535}
536
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000537std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000538 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000539
540 std::string Alias;
541 if (Name) {
542 // Use a specific name for this type, e.g. "size_t".
543 Alias = Name;
544 if (Ptr) {
545 // If ArgType is actually a pointer to T, append an asterisk.
546 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
547 }
548 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
549 if (S == Alias)
550 Alias.clear();
551 }
552
553 if (!Alias.empty())
554 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000555 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000556}
557
558
Ted Kremenek02087932010-07-16 02:11:22 +0000559//===----------------------------------------------------------------------===//
560// Methods on OptionalAmount.
561//===----------------------------------------------------------------------===//
562
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000563ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000564analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
565 return Ctx.IntTy;
566}
567
568//===----------------------------------------------------------------------===//
569// Methods on LengthModifier.
570//===----------------------------------------------------------------------===//
571
572const char *
573analyze_format_string::LengthModifier::toString() const {
574 switch (kind) {
575 case AsChar:
576 return "hh";
577 case AsShort:
578 return "h";
Matt Arsenault58fc8082019-01-29 20:49:54 +0000579 case AsShortLong:
580 return "hl";
Ted Kremenek02087932010-07-16 02:11:22 +0000581 case AsLong: // or AsWideChar
582 return "l";
583 case AsLongLong:
584 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000585 case AsQuad:
586 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000587 case AsIntMax:
588 return "j";
589 case AsSizeT:
590 return "z";
591 case AsPtrDiff:
592 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000593 case AsInt32:
594 return "I32";
595 case AsInt3264:
596 return "I";
597 case AsInt64:
598 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000599 case AsLongDouble:
600 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000601 case AsAllocate:
602 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000603 case AsMAllocate:
604 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000605 case AsWide:
606 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000607 case None:
608 return "";
609 }
Craig Topper25542942014-05-20 04:30:07 +0000610 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000611}
612
613//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000614// Methods on ConversionSpecifier.
615//===----------------------------------------------------------------------===//
616
617const char *ConversionSpecifier::toString() const {
618 switch (kind) {
619 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000620 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000621 case iArg: return "i";
622 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000623 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000624 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000625 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000626 case xArg: return "x";
627 case XArg: return "X";
628 case fArg: return "f";
629 case FArg: return "F";
630 case eArg: return "e";
631 case EArg: return "E";
632 case gArg: return "g";
633 case GArg: return "G";
634 case aArg: return "a";
635 case AArg: return "A";
636 case cArg: return "c";
637 case sArg: return "s";
638 case pArg: return "p";
Mehdi Amini06d367c2016-10-24 20:39:34 +0000639 case PArg:
640 return "P";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000641 case nArg: return "n";
642 case PercentArg: return "%";
643 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000644 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000645
David Majnemer3cba4952013-08-21 21:54:46 +0000646 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000647 case CArg: return "C";
648 case SArg: return "S";
649
650 // Objective-C specific specifiers.
651 case ObjCObjArg: return "@";
652
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000653 // FreeBSD kernel specific specifiers.
654 case FreeBSDbArg: return "b";
655 case FreeBSDDArg: return "D";
656 case FreeBSDrArg: return "r";
657 case FreeBSDyArg: return "y";
658
Hans Wennborga8b042d2011-12-09 11:11:07 +0000659 // GlibC specific specifiers.
660 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000661
662 // MS specific specifiers.
663 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000664 }
Craig Topper25542942014-05-20 04:30:07 +0000665 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000666}
667
David Blaikie05785d12013-02-20 22:23:23 +0000668Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000669ConversionSpecifier::getStandardSpecifier() const {
670 ConversionSpecifier::Kind NewKind;
Fangrui Song6907ce22018-07-30 19:24:48 +0000671
Jordan Rose4c266aa2012-09-13 02:11:15 +0000672 switch (getKind()) {
673 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000674 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000675 case DArg:
676 NewKind = dArg;
677 break;
678 case UArg:
679 NewKind = uArg;
680 break;
681 case OArg:
682 NewKind = oArg;
683 break;
684 }
685
686 ConversionSpecifier FixedCS(*this);
687 FixedCS.setKind(NewKind);
688 return FixedCS;
689}
690
Hans Wennborga8b042d2011-12-09 11:11:07 +0000691//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000692// Methods on OptionalAmount.
693//===----------------------------------------------------------------------===//
694
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000695void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000696 switch (hs) {
697 case Invalid:
698 case NotSpecified:
699 return;
700 case Arg:
701 if (UsesDotPrefix)
702 os << ".";
703 if (usesPositionalArg())
704 os << "*" << getPositionalArgIndex() << "$";
705 else
706 os << "*";
707 break;
708 case Constant:
709 if (UsesDotPrefix)
710 os << ".";
711 os << amt;
712 break;
713 }
714}
715
Matt Arsenault58fc8082019-01-29 20:49:54 +0000716bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target,
717 const LangOptions &LO) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000718 switch (LM.getKind()) {
719 case LengthModifier::None:
720 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000721
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000722 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000723 case LengthModifier::AsShort:
Matt Arsenault58fc8082019-01-29 20:49:54 +0000724 // Length modifier only applies to FP vectors.
725 if (LO.OpenCL && CS.isDoubleArg())
726 return !VectorNumElts.isInvalid();
727
Hans Wennborg1b231582014-09-04 21:39:52 +0000728 if (Target.getTriple().isOSMSVCRT()) {
729 switch (CS.getKind()) {
730 case ConversionSpecifier::cArg:
731 case ConversionSpecifier::CArg:
732 case ConversionSpecifier::sArg:
733 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000734 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000735 return true;
736 default:
737 break;
738 }
739 }
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000740 LLVM_FALLTHROUGH;
Hans Wennborg1b231582014-09-04 21:39:52 +0000741 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000742 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000743 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000744 case LengthModifier::AsIntMax:
745 case LengthModifier::AsSizeT:
746 case LengthModifier::AsPtrDiff:
747 switch (CS.getKind()) {
748 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000749 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000750 case ConversionSpecifier::iArg:
751 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000752 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000753 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000754 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000755 case ConversionSpecifier::xArg:
756 case ConversionSpecifier::XArg:
757 case ConversionSpecifier::nArg:
758 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000759 case ConversionSpecifier::FreeBSDrArg:
760 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000761 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000762 default:
763 return false;
764 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000765
Matt Arsenault58fc8082019-01-29 20:49:54 +0000766 case LengthModifier::AsShortLong:
767 return LO.OpenCL && !VectorNumElts.isInvalid();
768
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000769 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000770 case LengthModifier::AsLong: // or AsWideChar
Matt Arsenault58fc8082019-01-29 20:49:54 +0000771 if (CS.isDoubleArg()) {
772 // Invalid for OpenCL FP scalars.
773 if (LO.OpenCL && VectorNumElts.isInvalid())
774 return false;
775 return true;
776 }
777
Ted Kremenekea28f832010-07-20 20:04:42 +0000778 switch (CS.getKind()) {
779 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000780 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000781 case ConversionSpecifier::iArg:
782 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000783 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000784 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000785 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000786 case ConversionSpecifier::xArg:
787 case ConversionSpecifier::XArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000788 case ConversionSpecifier::nArg:
789 case ConversionSpecifier::cArg:
790 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000791 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000792 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000793 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000794 case ConversionSpecifier::FreeBSDrArg:
795 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000796 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000797 default:
798 return false;
799 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000800
Ted Kremenekea28f832010-07-20 20:04:42 +0000801 case LengthModifier::AsLongDouble:
802 switch (CS.getKind()) {
803 case ConversionSpecifier::aArg:
804 case ConversionSpecifier::AArg:
805 case ConversionSpecifier::fArg:
806 case ConversionSpecifier::FArg:
807 case ConversionSpecifier::eArg:
808 case ConversionSpecifier::EArg:
809 case ConversionSpecifier::gArg:
810 case ConversionSpecifier::GArg:
811 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000812 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000813 case ConversionSpecifier::dArg:
814 case ConversionSpecifier::iArg:
815 case ConversionSpecifier::oArg:
816 case ConversionSpecifier::uArg:
817 case ConversionSpecifier::xArg:
818 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000819 return !Target.getTriple().isOSDarwin() &&
820 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000821 default:
822 return false;
823 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000824
825 case LengthModifier::AsAllocate:
826 switch (CS.getKind()) {
827 case ConversionSpecifier::sArg:
828 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000829 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000830 return true;
831 default:
832 return false;
833 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000834
835 case LengthModifier::AsMAllocate:
836 switch (CS.getKind()) {
837 case ConversionSpecifier::cArg:
838 case ConversionSpecifier::CArg:
839 case ConversionSpecifier::sArg:
840 case ConversionSpecifier::SArg:
841 case ConversionSpecifier::ScanListArg:
842 return true;
843 default:
844 return false;
845 }
David Majnemer3cba4952013-08-21 21:54:46 +0000846 case LengthModifier::AsInt32:
847 case LengthModifier::AsInt3264:
848 case LengthModifier::AsInt64:
849 switch (CS.getKind()) {
850 case ConversionSpecifier::dArg:
851 case ConversionSpecifier::iArg:
852 case ConversionSpecifier::oArg:
853 case ConversionSpecifier::uArg:
854 case ConversionSpecifier::xArg:
855 case ConversionSpecifier::XArg:
856 return Target.getTriple().isOSMSVCRT();
857 default:
858 return false;
859 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000860 case LengthModifier::AsWide:
861 switch (CS.getKind()) {
862 case ConversionSpecifier::cArg:
863 case ConversionSpecifier::CArg:
864 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000865 case ConversionSpecifier::SArg:
866 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000867 return Target.getTriple().isOSMSVCRT();
868 default:
869 return false;
870 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000871 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000872 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000873}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000874
875bool FormatSpecifier::hasStandardLengthModifier() const {
876 switch (LM.getKind()) {
877 case LengthModifier::None:
878 case LengthModifier::AsChar:
879 case LengthModifier::AsShort:
880 case LengthModifier::AsLong:
881 case LengthModifier::AsLongLong:
882 case LengthModifier::AsIntMax:
883 case LengthModifier::AsSizeT:
884 case LengthModifier::AsPtrDiff:
885 case LengthModifier::AsLongDouble:
886 return true;
887 case LengthModifier::AsAllocate:
888 case LengthModifier::AsMAllocate:
889 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000890 case LengthModifier::AsInt32:
891 case LengthModifier::AsInt3264:
892 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000893 case LengthModifier::AsWide:
Matt Arsenault58fc8082019-01-29 20:49:54 +0000894 case LengthModifier::AsShortLong: // ???
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000895 return false;
896 }
897 llvm_unreachable("Invalid LengthModifier Kind!");
898}
899
Nico Webereffdb192015-05-18 02:41:17 +0000900bool FormatSpecifier::hasStandardConversionSpecifier(
901 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000902 switch (CS.getKind()) {
903 case ConversionSpecifier::cArg:
904 case ConversionSpecifier::dArg:
905 case ConversionSpecifier::iArg:
906 case ConversionSpecifier::oArg:
907 case ConversionSpecifier::uArg:
908 case ConversionSpecifier::xArg:
909 case ConversionSpecifier::XArg:
910 case ConversionSpecifier::fArg:
911 case ConversionSpecifier::FArg:
912 case ConversionSpecifier::eArg:
913 case ConversionSpecifier::EArg:
914 case ConversionSpecifier::gArg:
915 case ConversionSpecifier::GArg:
916 case ConversionSpecifier::aArg:
917 case ConversionSpecifier::AArg:
918 case ConversionSpecifier::sArg:
919 case ConversionSpecifier::pArg:
920 case ConversionSpecifier::nArg:
921 case ConversionSpecifier::ObjCObjArg:
922 case ConversionSpecifier::ScanListArg:
923 case ConversionSpecifier::PercentArg:
Mehdi Amini06d367c2016-10-24 20:39:34 +0000924 case ConversionSpecifier::PArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000925 return true;
926 case ConversionSpecifier::CArg:
927 case ConversionSpecifier::SArg:
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000928 return LangOpt.ObjC;
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000929 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000930 case ConversionSpecifier::FreeBSDbArg:
931 case ConversionSpecifier::FreeBSDDArg:
932 case ConversionSpecifier::FreeBSDrArg:
933 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000934 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000935 case ConversionSpecifier::DArg:
936 case ConversionSpecifier::OArg:
937 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000938 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000939 return false;
940 }
941 llvm_unreachable("Invalid ConversionSpecifier Kind!");
942}
943
944bool FormatSpecifier::hasStandardLengthConversionCombination() const {
945 if (LM.getKind() == LengthModifier::AsLongDouble) {
946 switch(CS.getKind()) {
947 case ConversionSpecifier::dArg:
948 case ConversionSpecifier::iArg:
949 case ConversionSpecifier::oArg:
950 case ConversionSpecifier::uArg:
951 case ConversionSpecifier::xArg:
952 case ConversionSpecifier::XArg:
953 return false;
954 default:
955 return true;
956 }
957 }
958 return true;
959}
Hans Wennborg08574d32012-07-27 19:17:46 +0000960
David Blaikie05785d12013-02-20 22:23:23 +0000961Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000962 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
963 if (LM.getKind() == LengthModifier::AsLongDouble ||
964 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000965 LengthModifier FixedLM(LM);
966 FixedLM.setKind(LengthModifier::AsLongLong);
967 return FixedLM;
968 }
Jordan Rose92303592012-09-08 04:00:03 +0000969 }
970
David Blaikie7a30dc52013-02-21 01:47:18 +0000971 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000972}
973
Hans Wennborg08574d32012-07-27 19:17:46 +0000974bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
975 LengthModifier &LM) {
976 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
977 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
978
979 for (;;) {
980 const IdentifierInfo *Identifier = Typedef->getIdentifier();
981 if (Identifier->getName() == "size_t") {
982 LM.setKind(LengthModifier::AsSizeT);
983 return true;
984 } else if (Identifier->getName() == "ssize_t") {
985 // Not C99, but common in Unix.
986 LM.setKind(LengthModifier::AsSizeT);
987 return true;
988 } else if (Identifier->getName() == "intmax_t") {
989 LM.setKind(LengthModifier::AsIntMax);
990 return true;
991 } else if (Identifier->getName() == "uintmax_t") {
992 LM.setKind(LengthModifier::AsIntMax);
993 return true;
994 } else if (Identifier->getName() == "ptrdiff_t") {
995 LM.setKind(LengthModifier::AsPtrDiff);
996 return true;
997 }
998
999 QualType T = Typedef->getUnderlyingType();
1000 if (!isa<TypedefType>(T))
1001 break;
1002
1003 Typedef = cast<TypedefType>(T)->getDecl();
1004 }
1005 return false;
1006}