blob: 0872e788c60b72d82bbaa6e406aea47131c460d9 [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"
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +000018#include "llvm/Support/ConvertUTF.h"
Ted Kremenek02087932010-07-16 02:11:22 +000019
Hans Wennborgc3b3da02012-08-07 08:11:26 +000020using clang::analyze_format_string::ArgType;
Ted Kremenek02087932010-07-16 02:11:22 +000021using clang::analyze_format_string::FormatStringHandler;
22using clang::analyze_format_string::FormatSpecifier;
23using clang::analyze_format_string::LengthModifier;
24using clang::analyze_format_string::OptionalAmount;
25using clang::analyze_format_string::PositionContext;
Ted Kremenekea28f832010-07-20 20:04:42 +000026using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek02087932010-07-16 02:11:22 +000027using namespace clang;
28
29// Key function to FormatStringHandler.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000030FormatStringHandler::~FormatStringHandler() {}
Ted Kremenek02087932010-07-16 02:11:22 +000031
32//===----------------------------------------------------------------------===//
33// Functions for parsing format strings components in both printf and
34// scanf format strings.
35//===----------------------------------------------------------------------===//
36
37OptionalAmount
38clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
39 const char *I = Beg;
40 UpdateOnReturn <const char*> UpdateBeg(Beg, I);
41
42 unsigned accumulator = 0;
43 bool hasDigits = false;
44
45 for ( ; I != E; ++I) {
46 char c = *I;
47 if (c >= '0' && c <= '9') {
48 hasDigits = true;
49 accumulator = (accumulator * 10) + (c - '0');
50 continue;
51 }
52
53 if (hasDigits)
54 return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
55 false);
56
57 break;
58 }
59
60 return OptionalAmount();
61}
62
63OptionalAmount
64clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
65 const char *E,
66 unsigned &argIndex) {
67 if (*Beg == '*') {
68 ++Beg;
69 return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
70 }
71
72 return ParseAmount(Beg, E);
73}
74
75OptionalAmount
76clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
77 const char *Start,
78 const char *&Beg,
79 const char *E,
80 PositionContext p) {
81 if (*Beg == '*') {
82 const char *I = Beg + 1;
83 const OptionalAmount &Amt = ParseAmount(I, E);
84
85 if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
86 H.HandleInvalidPosition(Beg, I - Beg, p);
87 return OptionalAmount(false);
88 }
89
90 if (I == E) {
91 // No more characters left?
92 H.HandleIncompleteSpecifier(Start, E - Start);
93 return OptionalAmount(false);
94 }
95
96 assert(Amt.getHowSpecified() == OptionalAmount::Constant);
97
98 if (*I == '$') {
99 // Handle positional arguments
100
101 // Special case: '*0$', since this is an easy mistake.
102 if (Amt.getConstantAmount() == 0) {
103 H.HandleZeroPosition(Beg, I - Beg + 1);
104 return OptionalAmount(false);
105 }
106
107 const char *Tmp = Beg;
108 Beg = ++I;
109
110 return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
111 Tmp, 0, true);
112 }
113
114 H.HandleInvalidPosition(Beg, I - Beg, p);
115 return OptionalAmount(false);
116 }
117
118 return ParseAmount(Beg, E);
119}
120
121
122bool
123clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
124 FormatSpecifier &CS,
125 const char *Start,
126 const char *&Beg, const char *E,
127 unsigned *argIndex) {
128 // FIXME: Support negative field widths.
129 if (argIndex) {
130 CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
131 }
132 else {
133 const OptionalAmount Amt =
134 ParsePositionAmount(H, Start, Beg, E,
135 analyze_format_string::FieldWidthPos);
136
137 if (Amt.isInvalid())
138 return true;
139 CS.setFieldWidth(Amt);
140 }
141 return false;
142}
143
144bool
145clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
146 FormatSpecifier &FS,
147 const char *Start,
148 const char *&Beg,
149 const char *E) {
150 const char *I = Beg;
151
152 const OptionalAmount &Amt = ParseAmount(I, E);
153
154 if (I == E) {
155 // No more characters left?
156 H.HandleIncompleteSpecifier(Start, E - Start);
157 return true;
158 }
159
160 if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
Hans Wennborgaa8c61c2012-03-09 10:10:54 +0000161 // Warn that positional arguments are non-standard.
162 H.HandlePosition(Start, I - Start);
163
Ted Kremenek02087932010-07-16 02:11:22 +0000164 // Special case: '%0$', since this is an easy mistake.
165 if (Amt.getConstantAmount() == 0) {
166 H.HandleZeroPosition(Start, I - Start);
167 return true;
168 }
169
170 FS.setArgIndex(Amt.getConstantAmount() - 1);
171 FS.setUsesPositionalArg();
172 // Update the caller's pointer if we decided to consume
173 // these characters.
174 Beg = I;
175 return false;
176 }
177
178 return false;
179}
180
181bool
182clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
183 const char *&I,
Hans Wennborg23926bd2011-12-15 10:25:47 +0000184 const char *E,
185 const LangOptions &LO,
186 bool IsScanf) {
Ted Kremenek02087932010-07-16 02:11:22 +0000187 LengthModifier::Kind lmKind = LengthModifier::None;
188 const char *lmPosition = I;
189 switch (*I) {
190 default:
191 return false;
192 case 'h':
193 ++I;
Richard Trieucc3949d2016-02-18 22:34:54 +0000194 if (I != E && *I == 'h') {
195 ++I;
196 lmKind = LengthModifier::AsChar;
197 } else {
198 lmKind = LengthModifier::AsShort;
199 }
Ted Kremenek02087932010-07-16 02:11:22 +0000200 break;
201 case 'l':
202 ++I;
Richard Trieucc3949d2016-02-18 22:34:54 +0000203 if (I != E && *I == 'l') {
204 ++I;
205 lmKind = LengthModifier::AsLongLong;
206 } else {
207 lmKind = LengthModifier::AsLong;
208 }
Ted Kremenek02087932010-07-16 02:11:22 +0000209 break;
210 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
211 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
212 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
213 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000214 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborg23926bd2011-12-15 10:25:47 +0000215 case 'a':
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000216 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
Hans Wennborg23926bd2011-12-15 10:25:47 +0000217 // For scanf in C90, look at the next character to see if this should
218 // be parsed as the GNU extension 'a' length modifier. If not, this
219 // will be parsed as a conversion specifier.
220 ++I;
221 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
222 lmKind = LengthModifier::AsAllocate;
223 break;
224 }
225 --I;
226 }
227 return false;
Hans Wennborg6073e312012-01-12 17:11:12 +0000228 case 'm':
229 if (IsScanf) {
230 lmKind = LengthModifier::AsMAllocate;
231 ++I;
232 break;
233 }
234 return false;
David Majnemer3cba4952013-08-21 21:54:46 +0000235 // printf: AsInt64, AsInt32, AsInt3264
236 // scanf: AsInt64
237 case 'I':
238 if (I + 1 != E && I + 2 != E) {
239 if (I[1] == '6' && I[2] == '4') {
240 I += 3;
241 lmKind = LengthModifier::AsInt64;
242 break;
243 }
244 if (IsScanf)
245 return false;
246
247 if (I[1] == '3' && I[2] == '2') {
248 I += 3;
249 lmKind = LengthModifier::AsInt32;
250 break;
251 }
252 }
253 ++I;
254 lmKind = LengthModifier::AsInt3264;
255 break;
Hans Wennborg68f42b92014-09-04 21:39:46 +0000256 case 'w':
257 lmKind = LengthModifier::AsWide; ++I; break;
Ted Kremenek02087932010-07-16 02:11:22 +0000258 }
259 LengthModifier lm(lmPosition, lmKind);
260 FS.setLengthModifier(lm);
261 return true;
262}
263
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +0000264bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
265 const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len) {
266 if (SpecifierBegin + 1 >= FmtStrEnd)
267 return false;
268
269 const UTF8 *SB = reinterpret_cast<const UTF8 *>(SpecifierBegin + 1);
270 const UTF8 *SE = reinterpret_cast<const UTF8 *>(FmtStrEnd);
271 const char FirstByte = *SB;
272
273 // If the invalid specifier is a multibyte UTF-8 string, return the
274 // total length accordingly so that the conversion specifier can be
275 // properly updated to reflect a complete UTF-8 specifier.
276 unsigned NumBytes = getNumBytesForUTF8(FirstByte);
277 if (NumBytes == 1)
278 return false;
279 if (SB + NumBytes > SE)
280 return false;
281
282 Len = NumBytes + 1;
283 return true;
284}
285
Ted Kremenek02087932010-07-16 02:11:22 +0000286//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000287// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000288//===----------------------------------------------------------------------===//
289
Seth Cantrellb4802962015-03-04 03:12:10 +0000290clang::analyze_format_string::ArgType::MatchKind
291ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000292 if (Ptr) {
293 // It has to be a pointer.
294 const PointerType *PT = argTy->getAs<PointerType>();
295 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000296 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000297
298 // We cannot write through a const qualified pointer.
299 if (PT->getPointeeType().isConstQualified())
Seth Cantrellb4802962015-03-04 03:12:10 +0000300 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000301
302 argTy = PT->getPointeeType();
303 }
304
Ted Kremenek02087932010-07-16 02:11:22 +0000305 switch (K) {
306 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000307 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000308
309 case UnknownTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000310 return Match;
311
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000312 case AnyCharTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000313 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
314 // If the enum is incomplete we know nothing about the underlying type.
315 // Assume that it's 'int'.
316 if (!ETy->getDecl()->isComplete())
317 return NoMatch;
Jordan Rose98709982012-06-04 22:48:57 +0000318 argTy = ETy->getDecl()->getIntegerType();
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000319 }
Jordan Rose98709982012-06-04 22:48:57 +0000320
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000321 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
322 switch (BT->getKind()) {
323 default:
324 break;
325 case BuiltinType::Char_S:
326 case BuiltinType::SChar:
327 case BuiltinType::UChar:
328 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000329 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000330 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000331 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000332 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000333
Ted Kremenek02087932010-07-16 02:11:22 +0000334 case SpecificTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000335 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
336 // If the enum is incomplete we know nothing about the underlying type.
337 // Assume that it's 'int'.
338 if (!ETy->getDecl()->isComplete())
339 argTy = C.IntTy;
340 else
341 argTy = ETy->getDecl()->getIntegerType();
342 }
Ted Kremenek02087932010-07-16 02:11:22 +0000343 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000344
Nick Lewycky45ccba62011-12-02 23:21:43 +0000345 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000346 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000347 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000348 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000349 switch (BT->getKind()) {
350 default:
351 break;
352 case BuiltinType::Char_S:
353 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000354 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000355 case BuiltinType::UChar:
356 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
357 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000358 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000359 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000360 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000361 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000362 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000363 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000364 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000365 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000366 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000367 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000368 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000369 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000370 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000371 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000372 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000373 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000374 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000375 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000376 }
377
378 case CStrTy: {
379 const PointerType *PT = argTy->getAs<PointerType>();
380 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000381 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000382 QualType pointeeTy = PT->getPointeeType();
383 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
384 switch (BT->getKind()) {
385 case BuiltinType::Void:
386 case BuiltinType::Char_U:
387 case BuiltinType::UChar:
388 case BuiltinType::Char_S:
389 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000390 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000391 default:
392 break;
393 }
394
Seth Cantrellb4802962015-03-04 03:12:10 +0000395 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000396 }
397
398 case WCStrTy: {
399 const PointerType *PT = argTy->getAs<PointerType>();
400 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000401 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000402 QualType pointeeTy =
403 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000404 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000405 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000406
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000407 case WIntTy: {
Seth Cantrellb4802962015-03-04 03:12:10 +0000408
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000409 QualType PromoArg =
410 argTy->isPromotableIntegerType()
411 ? C.getPromotedIntegerType(argTy) : argTy;
Seth Cantrellb4802962015-03-04 03:12:10 +0000412
James Molloy36365542012-05-04 10:55:22 +0000413 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000414 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000415
James Molloy36365542012-05-04 10:55:22 +0000416 // If the promoted argument is the corresponding signed type of the
417 // wint_t type, then it should match.
418 if (PromoArg->hasSignedIntegerRepresentation() &&
419 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000420 return Match;
James Molloy36365542012-05-04 10:55:22 +0000421
Seth Cantrellb4802962015-03-04 03:12:10 +0000422 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000423 }
Ted Kremenek02087932010-07-16 02:11:22 +0000424
425 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000426 if (argTy->isVoidPointerType()) {
427 return Match;
428 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
429 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
430 return NoMatchPedantic;
431 } else {
432 return NoMatch;
433 }
Ted Kremenek02087932010-07-16 02:11:22 +0000434
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000435 case ObjCPointerTy: {
436 if (argTy->getAs<ObjCObjectPointerType>() ||
437 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000438 return Match;
439
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000440 // Handle implicit toll-free bridging.
441 if (const PointerType *PT = argTy->getAs<PointerType>()) {
442 // Things such as CFTypeRef are really just opaque pointers
443 // to C structs representing CF types that can often be bridged
444 // to Objective-C objects. Since the compiler doesn't know which
445 // structs can be toll-free bridged, we just accept them all.
446 QualType pointee = PT->getPointeeType();
447 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000448 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000449 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000450 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000451 }
Ted Kremenek02087932010-07-16 02:11:22 +0000452 }
453
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000454 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000455}
456
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000457QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000458 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000459 switch (K) {
460 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000461 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000462 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000463 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000464 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000465 Res = C.CharTy;
466 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000467 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000468 Res = T;
469 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000470 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000471 Res = C.getPointerType(C.CharTy);
472 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000473 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000474 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000475 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000476 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000477 Res = C.ObjCBuiltinIdTy;
478 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000479 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000480 Res = C.VoidPtrTy;
481 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000482 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000483 Res = C.getWIntType();
484 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000485 }
Ted Kremenek02087932010-07-16 02:11:22 +0000486 }
487
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000488 if (Ptr)
489 Res = C.getPointerType(Res);
490 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000491}
492
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000493std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000494 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000495
496 std::string Alias;
497 if (Name) {
498 // Use a specific name for this type, e.g. "size_t".
499 Alias = Name;
500 if (Ptr) {
501 // If ArgType is actually a pointer to T, append an asterisk.
502 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
503 }
504 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
505 if (S == Alias)
506 Alias.clear();
507 }
508
509 if (!Alias.empty())
510 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000511 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000512}
513
514
Ted Kremenek02087932010-07-16 02:11:22 +0000515//===----------------------------------------------------------------------===//
516// Methods on OptionalAmount.
517//===----------------------------------------------------------------------===//
518
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000519ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000520analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
521 return Ctx.IntTy;
522}
523
524//===----------------------------------------------------------------------===//
525// Methods on LengthModifier.
526//===----------------------------------------------------------------------===//
527
528const char *
529analyze_format_string::LengthModifier::toString() const {
530 switch (kind) {
531 case AsChar:
532 return "hh";
533 case AsShort:
534 return "h";
535 case AsLong: // or AsWideChar
536 return "l";
537 case AsLongLong:
538 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000539 case AsQuad:
540 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000541 case AsIntMax:
542 return "j";
543 case AsSizeT:
544 return "z";
545 case AsPtrDiff:
546 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000547 case AsInt32:
548 return "I32";
549 case AsInt3264:
550 return "I";
551 case AsInt64:
552 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000553 case AsLongDouble:
554 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000555 case AsAllocate:
556 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000557 case AsMAllocate:
558 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000559 case AsWide:
560 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000561 case None:
562 return "";
563 }
Craig Topper25542942014-05-20 04:30:07 +0000564 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000565}
566
567//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000568// Methods on ConversionSpecifier.
569//===----------------------------------------------------------------------===//
570
571const char *ConversionSpecifier::toString() const {
572 switch (kind) {
573 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000574 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000575 case iArg: return "i";
576 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000577 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000578 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000579 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000580 case xArg: return "x";
581 case XArg: return "X";
582 case fArg: return "f";
583 case FArg: return "F";
584 case eArg: return "e";
585 case EArg: return "E";
586 case gArg: return "g";
587 case GArg: return "G";
588 case aArg: return "a";
589 case AArg: return "A";
590 case cArg: return "c";
591 case sArg: return "s";
592 case pArg: return "p";
593 case nArg: return "n";
594 case PercentArg: return "%";
595 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000596 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000597
David Majnemer3cba4952013-08-21 21:54:46 +0000598 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000599 case CArg: return "C";
600 case SArg: return "S";
601
602 // Objective-C specific specifiers.
603 case ObjCObjArg: return "@";
604
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000605 // FreeBSD kernel specific specifiers.
606 case FreeBSDbArg: return "b";
607 case FreeBSDDArg: return "D";
608 case FreeBSDrArg: return "r";
609 case FreeBSDyArg: return "y";
610
Hans Wennborga8b042d2011-12-09 11:11:07 +0000611 // GlibC specific specifiers.
612 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000613
614 // MS specific specifiers.
615 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000616 }
Craig Topper25542942014-05-20 04:30:07 +0000617 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000618}
619
David Blaikie05785d12013-02-20 22:23:23 +0000620Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000621ConversionSpecifier::getStandardSpecifier() const {
622 ConversionSpecifier::Kind NewKind;
623
624 switch (getKind()) {
625 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000626 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000627 case DArg:
628 NewKind = dArg;
629 break;
630 case UArg:
631 NewKind = uArg;
632 break;
633 case OArg:
634 NewKind = oArg;
635 break;
636 }
637
638 ConversionSpecifier FixedCS(*this);
639 FixedCS.setKind(NewKind);
640 return FixedCS;
641}
642
Hans Wennborga8b042d2011-12-09 11:11:07 +0000643//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000644// Methods on OptionalAmount.
645//===----------------------------------------------------------------------===//
646
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000647void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000648 switch (hs) {
649 case Invalid:
650 case NotSpecified:
651 return;
652 case Arg:
653 if (UsesDotPrefix)
654 os << ".";
655 if (usesPositionalArg())
656 os << "*" << getPositionalArgIndex() << "$";
657 else
658 os << "*";
659 break;
660 case Constant:
661 if (UsesDotPrefix)
662 os << ".";
663 os << amt;
664 break;
665 }
666}
667
Jordan Rose92303592012-09-08 04:00:03 +0000668bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000669 switch (LM.getKind()) {
670 case LengthModifier::None:
671 return true;
672
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000673 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000674 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000675 if (Target.getTriple().isOSMSVCRT()) {
676 switch (CS.getKind()) {
677 case ConversionSpecifier::cArg:
678 case ConversionSpecifier::CArg:
679 case ConversionSpecifier::sArg:
680 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000681 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000682 return true;
683 default:
684 break;
685 }
686 }
687 // Fall through.
688 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000689 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000690 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000691 case LengthModifier::AsIntMax:
692 case LengthModifier::AsSizeT:
693 case LengthModifier::AsPtrDiff:
694 switch (CS.getKind()) {
695 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000696 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000697 case ConversionSpecifier::iArg:
698 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000699 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000700 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000701 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000702 case ConversionSpecifier::xArg:
703 case ConversionSpecifier::XArg:
704 case ConversionSpecifier::nArg:
705 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000706 case ConversionSpecifier::FreeBSDrArg:
707 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000708 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000709 default:
710 return false;
711 }
712
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000713 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000714 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000715 switch (CS.getKind()) {
716 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000717 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000718 case ConversionSpecifier::iArg:
719 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000720 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000721 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000722 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000723 case ConversionSpecifier::xArg:
724 case ConversionSpecifier::XArg:
725 case ConversionSpecifier::aArg:
726 case ConversionSpecifier::AArg:
727 case ConversionSpecifier::fArg:
728 case ConversionSpecifier::FArg:
729 case ConversionSpecifier::eArg:
730 case ConversionSpecifier::EArg:
731 case ConversionSpecifier::gArg:
732 case ConversionSpecifier::GArg:
733 case ConversionSpecifier::nArg:
734 case ConversionSpecifier::cArg:
735 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000736 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000737 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000738 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000739 case ConversionSpecifier::FreeBSDrArg:
740 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000741 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000742 default:
743 return false;
744 }
745
746 case LengthModifier::AsLongDouble:
747 switch (CS.getKind()) {
748 case ConversionSpecifier::aArg:
749 case ConversionSpecifier::AArg:
750 case ConversionSpecifier::fArg:
751 case ConversionSpecifier::FArg:
752 case ConversionSpecifier::eArg:
753 case ConversionSpecifier::EArg:
754 case ConversionSpecifier::gArg:
755 case ConversionSpecifier::GArg:
756 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000757 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000758 case ConversionSpecifier::dArg:
759 case ConversionSpecifier::iArg:
760 case ConversionSpecifier::oArg:
761 case ConversionSpecifier::uArg:
762 case ConversionSpecifier::xArg:
763 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000764 return !Target.getTriple().isOSDarwin() &&
765 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000766 default:
767 return false;
768 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000769
770 case LengthModifier::AsAllocate:
771 switch (CS.getKind()) {
772 case ConversionSpecifier::sArg:
773 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000774 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000775 return true;
776 default:
777 return false;
778 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000779
780 case LengthModifier::AsMAllocate:
781 switch (CS.getKind()) {
782 case ConversionSpecifier::cArg:
783 case ConversionSpecifier::CArg:
784 case ConversionSpecifier::sArg:
785 case ConversionSpecifier::SArg:
786 case ConversionSpecifier::ScanListArg:
787 return true;
788 default:
789 return false;
790 }
David Majnemer3cba4952013-08-21 21:54:46 +0000791 case LengthModifier::AsInt32:
792 case LengthModifier::AsInt3264:
793 case LengthModifier::AsInt64:
794 switch (CS.getKind()) {
795 case ConversionSpecifier::dArg:
796 case ConversionSpecifier::iArg:
797 case ConversionSpecifier::oArg:
798 case ConversionSpecifier::uArg:
799 case ConversionSpecifier::xArg:
800 case ConversionSpecifier::XArg:
801 return Target.getTriple().isOSMSVCRT();
802 default:
803 return false;
804 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000805 case LengthModifier::AsWide:
806 switch (CS.getKind()) {
807 case ConversionSpecifier::cArg:
808 case ConversionSpecifier::CArg:
809 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000810 case ConversionSpecifier::SArg:
811 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000812 return Target.getTriple().isOSMSVCRT();
813 default:
814 return false;
815 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000816 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000817 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000818}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000819
820bool FormatSpecifier::hasStandardLengthModifier() const {
821 switch (LM.getKind()) {
822 case LengthModifier::None:
823 case LengthModifier::AsChar:
824 case LengthModifier::AsShort:
825 case LengthModifier::AsLong:
826 case LengthModifier::AsLongLong:
827 case LengthModifier::AsIntMax:
828 case LengthModifier::AsSizeT:
829 case LengthModifier::AsPtrDiff:
830 case LengthModifier::AsLongDouble:
831 return true;
832 case LengthModifier::AsAllocate:
833 case LengthModifier::AsMAllocate:
834 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000835 case LengthModifier::AsInt32:
836 case LengthModifier::AsInt3264:
837 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000838 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000839 return false;
840 }
841 llvm_unreachable("Invalid LengthModifier Kind!");
842}
843
Nico Webereffdb192015-05-18 02:41:17 +0000844bool FormatSpecifier::hasStandardConversionSpecifier(
845 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000846 switch (CS.getKind()) {
847 case ConversionSpecifier::cArg:
848 case ConversionSpecifier::dArg:
849 case ConversionSpecifier::iArg:
850 case ConversionSpecifier::oArg:
851 case ConversionSpecifier::uArg:
852 case ConversionSpecifier::xArg:
853 case ConversionSpecifier::XArg:
854 case ConversionSpecifier::fArg:
855 case ConversionSpecifier::FArg:
856 case ConversionSpecifier::eArg:
857 case ConversionSpecifier::EArg:
858 case ConversionSpecifier::gArg:
859 case ConversionSpecifier::GArg:
860 case ConversionSpecifier::aArg:
861 case ConversionSpecifier::AArg:
862 case ConversionSpecifier::sArg:
863 case ConversionSpecifier::pArg:
864 case ConversionSpecifier::nArg:
865 case ConversionSpecifier::ObjCObjArg:
866 case ConversionSpecifier::ScanListArg:
867 case ConversionSpecifier::PercentArg:
868 return true;
869 case ConversionSpecifier::CArg:
870 case ConversionSpecifier::SArg:
871 return LangOpt.ObjC1 || LangOpt.ObjC2;
872 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000873 case ConversionSpecifier::FreeBSDbArg:
874 case ConversionSpecifier::FreeBSDDArg:
875 case ConversionSpecifier::FreeBSDrArg:
876 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000877 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000878 case ConversionSpecifier::DArg:
879 case ConversionSpecifier::OArg:
880 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000881 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000882 return false;
883 }
884 llvm_unreachable("Invalid ConversionSpecifier Kind!");
885}
886
887bool FormatSpecifier::hasStandardLengthConversionCombination() const {
888 if (LM.getKind() == LengthModifier::AsLongDouble) {
889 switch(CS.getKind()) {
890 case ConversionSpecifier::dArg:
891 case ConversionSpecifier::iArg:
892 case ConversionSpecifier::oArg:
893 case ConversionSpecifier::uArg:
894 case ConversionSpecifier::xArg:
895 case ConversionSpecifier::XArg:
896 return false;
897 default:
898 return true;
899 }
900 }
901 return true;
902}
Hans Wennborg08574d32012-07-27 19:17:46 +0000903
David Blaikie05785d12013-02-20 22:23:23 +0000904Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000905 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
906 if (LM.getKind() == LengthModifier::AsLongDouble ||
907 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000908 LengthModifier FixedLM(LM);
909 FixedLM.setKind(LengthModifier::AsLongLong);
910 return FixedLM;
911 }
Jordan Rose92303592012-09-08 04:00:03 +0000912 }
913
David Blaikie7a30dc52013-02-21 01:47:18 +0000914 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000915}
916
Hans Wennborg08574d32012-07-27 19:17:46 +0000917bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
918 LengthModifier &LM) {
919 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
920 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
921
922 for (;;) {
923 const IdentifierInfo *Identifier = Typedef->getIdentifier();
924 if (Identifier->getName() == "size_t") {
925 LM.setKind(LengthModifier::AsSizeT);
926 return true;
927 } else if (Identifier->getName() == "ssize_t") {
928 // Not C99, but common in Unix.
929 LM.setKind(LengthModifier::AsSizeT);
930 return true;
931 } else if (Identifier->getName() == "intmax_t") {
932 LM.setKind(LengthModifier::AsIntMax);
933 return true;
934 } else if (Identifier->getName() == "uintmax_t") {
935 LM.setKind(LengthModifier::AsIntMax);
936 return true;
937 } else if (Identifier->getName() == "ptrdiff_t") {
938 LM.setKind(LengthModifier::AsPtrDiff);
939 return true;
940 }
941
942 QualType T = Typedef->getUnderlyingType();
943 if (!isa<TypedefType>(T))
944 break;
945
946 Typedef = cast<TypedefType>(T)->getDecl();
947 }
948 return false;
949}