blob: badc71021a12f5ecaa11c61d07c5dd6d0ba3a78d [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: {
Jordan Rose98709982012-06-04 22:48:57 +0000313 if (const EnumType *ETy = argTy->getAs<EnumType>())
314 argTy = ETy->getDecl()->getIntegerType();
315
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000316 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
317 switch (BT->getKind()) {
318 default:
319 break;
320 case BuiltinType::Char_S:
321 case BuiltinType::SChar:
322 case BuiltinType::UChar:
323 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000324 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000325 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000326 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000327 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000328
Ted Kremenek02087932010-07-16 02:11:22 +0000329 case SpecificTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000330 if (const EnumType *ETy = argTy->getAs<EnumType>())
331 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek02087932010-07-16 02:11:22 +0000332 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000333
Nick Lewycky45ccba62011-12-02 23:21:43 +0000334 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000335 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000336 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000337 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000338 switch (BT->getKind()) {
339 default:
340 break;
341 case BuiltinType::Char_S:
342 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000343 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000344 case BuiltinType::UChar:
345 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
346 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000347 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000348 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000349 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000350 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000351 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000352 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000353 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000354 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000355 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000356 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000357 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000358 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000359 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000360 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000361 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000362 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000363 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000364 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000365 }
366
367 case CStrTy: {
368 const PointerType *PT = argTy->getAs<PointerType>();
369 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000370 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000371 QualType pointeeTy = PT->getPointeeType();
372 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
373 switch (BT->getKind()) {
374 case BuiltinType::Void:
375 case BuiltinType::Char_U:
376 case BuiltinType::UChar:
377 case BuiltinType::Char_S:
378 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000379 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000380 default:
381 break;
382 }
383
Seth Cantrellb4802962015-03-04 03:12:10 +0000384 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000385 }
386
387 case WCStrTy: {
388 const PointerType *PT = argTy->getAs<PointerType>();
389 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000390 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000391 QualType pointeeTy =
392 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000393 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000394 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000395
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000396 case WIntTy: {
Seth Cantrellb4802962015-03-04 03:12:10 +0000397
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000398 QualType PromoArg =
399 argTy->isPromotableIntegerType()
400 ? C.getPromotedIntegerType(argTy) : argTy;
Seth Cantrellb4802962015-03-04 03:12:10 +0000401
James Molloy36365542012-05-04 10:55:22 +0000402 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000403 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000404
James Molloy36365542012-05-04 10:55:22 +0000405 // If the promoted argument is the corresponding signed type of the
406 // wint_t type, then it should match.
407 if (PromoArg->hasSignedIntegerRepresentation() &&
408 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000409 return Match;
James Molloy36365542012-05-04 10:55:22 +0000410
Seth Cantrellb4802962015-03-04 03:12:10 +0000411 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000412 }
Ted Kremenek02087932010-07-16 02:11:22 +0000413
414 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000415 if (argTy->isVoidPointerType()) {
416 return Match;
417 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
418 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
419 return NoMatchPedantic;
420 } else {
421 return NoMatch;
422 }
Ted Kremenek02087932010-07-16 02:11:22 +0000423
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000424 case ObjCPointerTy: {
425 if (argTy->getAs<ObjCObjectPointerType>() ||
426 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000427 return Match;
428
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000429 // Handle implicit toll-free bridging.
430 if (const PointerType *PT = argTy->getAs<PointerType>()) {
431 // Things such as CFTypeRef are really just opaque pointers
432 // to C structs representing CF types that can often be bridged
433 // to Objective-C objects. Since the compiler doesn't know which
434 // structs can be toll-free bridged, we just accept them all.
435 QualType pointee = PT->getPointeeType();
436 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000437 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000438 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000439 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000440 }
Ted Kremenek02087932010-07-16 02:11:22 +0000441 }
442
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000443 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000444}
445
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000446QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000447 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000448 switch (K) {
449 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000450 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000451 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000452 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000453 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000454 Res = C.CharTy;
455 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000456 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000457 Res = T;
458 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000459 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000460 Res = C.getPointerType(C.CharTy);
461 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000462 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000463 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000464 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000465 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000466 Res = C.ObjCBuiltinIdTy;
467 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000468 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000469 Res = C.VoidPtrTy;
470 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000471 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000472 Res = C.getWIntType();
473 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000474 }
Ted Kremenek02087932010-07-16 02:11:22 +0000475 }
476
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000477 if (Ptr)
478 Res = C.getPointerType(Res);
479 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000480}
481
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000482std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000483 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000484
485 std::string Alias;
486 if (Name) {
487 // Use a specific name for this type, e.g. "size_t".
488 Alias = Name;
489 if (Ptr) {
490 // If ArgType is actually a pointer to T, append an asterisk.
491 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
492 }
493 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
494 if (S == Alias)
495 Alias.clear();
496 }
497
498 if (!Alias.empty())
499 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000500 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000501}
502
503
Ted Kremenek02087932010-07-16 02:11:22 +0000504//===----------------------------------------------------------------------===//
505// Methods on OptionalAmount.
506//===----------------------------------------------------------------------===//
507
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000508ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000509analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
510 return Ctx.IntTy;
511}
512
513//===----------------------------------------------------------------------===//
514// Methods on LengthModifier.
515//===----------------------------------------------------------------------===//
516
517const char *
518analyze_format_string::LengthModifier::toString() const {
519 switch (kind) {
520 case AsChar:
521 return "hh";
522 case AsShort:
523 return "h";
524 case AsLong: // or AsWideChar
525 return "l";
526 case AsLongLong:
527 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000528 case AsQuad:
529 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000530 case AsIntMax:
531 return "j";
532 case AsSizeT:
533 return "z";
534 case AsPtrDiff:
535 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000536 case AsInt32:
537 return "I32";
538 case AsInt3264:
539 return "I";
540 case AsInt64:
541 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000542 case AsLongDouble:
543 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000544 case AsAllocate:
545 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000546 case AsMAllocate:
547 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000548 case AsWide:
549 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000550 case None:
551 return "";
552 }
Craig Topper25542942014-05-20 04:30:07 +0000553 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000554}
555
556//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000557// Methods on ConversionSpecifier.
558//===----------------------------------------------------------------------===//
559
560const char *ConversionSpecifier::toString() const {
561 switch (kind) {
562 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000563 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000564 case iArg: return "i";
565 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000566 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000567 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000568 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000569 case xArg: return "x";
570 case XArg: return "X";
571 case fArg: return "f";
572 case FArg: return "F";
573 case eArg: return "e";
574 case EArg: return "E";
575 case gArg: return "g";
576 case GArg: return "G";
577 case aArg: return "a";
578 case AArg: return "A";
579 case cArg: return "c";
580 case sArg: return "s";
581 case pArg: return "p";
582 case nArg: return "n";
583 case PercentArg: return "%";
584 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000585 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000586
David Majnemer3cba4952013-08-21 21:54:46 +0000587 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000588 case CArg: return "C";
589 case SArg: return "S";
590
591 // Objective-C specific specifiers.
592 case ObjCObjArg: return "@";
593
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000594 // FreeBSD kernel specific specifiers.
595 case FreeBSDbArg: return "b";
596 case FreeBSDDArg: return "D";
597 case FreeBSDrArg: return "r";
598 case FreeBSDyArg: return "y";
599
Hans Wennborga8b042d2011-12-09 11:11:07 +0000600 // GlibC specific specifiers.
601 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000602
603 // MS specific specifiers.
604 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000605 }
Craig Topper25542942014-05-20 04:30:07 +0000606 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000607}
608
David Blaikie05785d12013-02-20 22:23:23 +0000609Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000610ConversionSpecifier::getStandardSpecifier() const {
611 ConversionSpecifier::Kind NewKind;
612
613 switch (getKind()) {
614 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000615 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000616 case DArg:
617 NewKind = dArg;
618 break;
619 case UArg:
620 NewKind = uArg;
621 break;
622 case OArg:
623 NewKind = oArg;
624 break;
625 }
626
627 ConversionSpecifier FixedCS(*this);
628 FixedCS.setKind(NewKind);
629 return FixedCS;
630}
631
Hans Wennborga8b042d2011-12-09 11:11:07 +0000632//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000633// Methods on OptionalAmount.
634//===----------------------------------------------------------------------===//
635
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000636void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000637 switch (hs) {
638 case Invalid:
639 case NotSpecified:
640 return;
641 case Arg:
642 if (UsesDotPrefix)
643 os << ".";
644 if (usesPositionalArg())
645 os << "*" << getPositionalArgIndex() << "$";
646 else
647 os << "*";
648 break;
649 case Constant:
650 if (UsesDotPrefix)
651 os << ".";
652 os << amt;
653 break;
654 }
655}
656
Jordan Rose92303592012-09-08 04:00:03 +0000657bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000658 switch (LM.getKind()) {
659 case LengthModifier::None:
660 return true;
661
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000662 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000663 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000664 if (Target.getTriple().isOSMSVCRT()) {
665 switch (CS.getKind()) {
666 case ConversionSpecifier::cArg:
667 case ConversionSpecifier::CArg:
668 case ConversionSpecifier::sArg:
669 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000670 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000671 return true;
672 default:
673 break;
674 }
675 }
676 // Fall through.
677 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000678 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000679 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000680 case LengthModifier::AsIntMax:
681 case LengthModifier::AsSizeT:
682 case LengthModifier::AsPtrDiff:
683 switch (CS.getKind()) {
684 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000685 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000686 case ConversionSpecifier::iArg:
687 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000688 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000689 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000690 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000691 case ConversionSpecifier::xArg:
692 case ConversionSpecifier::XArg:
693 case ConversionSpecifier::nArg:
694 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000695 case ConversionSpecifier::FreeBSDrArg:
696 case ConversionSpecifier::FreeBSDyArg:
697 return Target.getTriple().isOSFreeBSD();
Ted Kremenekea28f832010-07-20 20:04:42 +0000698 default:
699 return false;
700 }
701
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000702 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000703 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000704 switch (CS.getKind()) {
705 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000706 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000707 case ConversionSpecifier::iArg:
708 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000709 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000710 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000711 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000712 case ConversionSpecifier::xArg:
713 case ConversionSpecifier::XArg:
714 case ConversionSpecifier::aArg:
715 case ConversionSpecifier::AArg:
716 case ConversionSpecifier::fArg:
717 case ConversionSpecifier::FArg:
718 case ConversionSpecifier::eArg:
719 case ConversionSpecifier::EArg:
720 case ConversionSpecifier::gArg:
721 case ConversionSpecifier::GArg:
722 case ConversionSpecifier::nArg:
723 case ConversionSpecifier::cArg:
724 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000725 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000726 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000727 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000728 case ConversionSpecifier::FreeBSDrArg:
729 case ConversionSpecifier::FreeBSDyArg:
730 return Target.getTriple().isOSFreeBSD();
Ted Kremenekea28f832010-07-20 20:04:42 +0000731 default:
732 return false;
733 }
734
735 case LengthModifier::AsLongDouble:
736 switch (CS.getKind()) {
737 case ConversionSpecifier::aArg:
738 case ConversionSpecifier::AArg:
739 case ConversionSpecifier::fArg:
740 case ConversionSpecifier::FArg:
741 case ConversionSpecifier::eArg:
742 case ConversionSpecifier::EArg:
743 case ConversionSpecifier::gArg:
744 case ConversionSpecifier::GArg:
745 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000746 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000747 case ConversionSpecifier::dArg:
748 case ConversionSpecifier::iArg:
749 case ConversionSpecifier::oArg:
750 case ConversionSpecifier::uArg:
751 case ConversionSpecifier::xArg:
752 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000753 return !Target.getTriple().isOSDarwin() &&
754 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000755 default:
756 return false;
757 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000758
759 case LengthModifier::AsAllocate:
760 switch (CS.getKind()) {
761 case ConversionSpecifier::sArg:
762 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000763 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000764 return true;
765 default:
766 return false;
767 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000768
769 case LengthModifier::AsMAllocate:
770 switch (CS.getKind()) {
771 case ConversionSpecifier::cArg:
772 case ConversionSpecifier::CArg:
773 case ConversionSpecifier::sArg:
774 case ConversionSpecifier::SArg:
775 case ConversionSpecifier::ScanListArg:
776 return true;
777 default:
778 return false;
779 }
David Majnemer3cba4952013-08-21 21:54:46 +0000780 case LengthModifier::AsInt32:
781 case LengthModifier::AsInt3264:
782 case LengthModifier::AsInt64:
783 switch (CS.getKind()) {
784 case ConversionSpecifier::dArg:
785 case ConversionSpecifier::iArg:
786 case ConversionSpecifier::oArg:
787 case ConversionSpecifier::uArg:
788 case ConversionSpecifier::xArg:
789 case ConversionSpecifier::XArg:
790 return Target.getTriple().isOSMSVCRT();
791 default:
792 return false;
793 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000794 case LengthModifier::AsWide:
795 switch (CS.getKind()) {
796 case ConversionSpecifier::cArg:
797 case ConversionSpecifier::CArg:
798 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000799 case ConversionSpecifier::SArg:
800 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000801 return Target.getTriple().isOSMSVCRT();
802 default:
803 return false;
804 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000805 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000806 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000807}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000808
809bool FormatSpecifier::hasStandardLengthModifier() const {
810 switch (LM.getKind()) {
811 case LengthModifier::None:
812 case LengthModifier::AsChar:
813 case LengthModifier::AsShort:
814 case LengthModifier::AsLong:
815 case LengthModifier::AsLongLong:
816 case LengthModifier::AsIntMax:
817 case LengthModifier::AsSizeT:
818 case LengthModifier::AsPtrDiff:
819 case LengthModifier::AsLongDouble:
820 return true;
821 case LengthModifier::AsAllocate:
822 case LengthModifier::AsMAllocate:
823 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000824 case LengthModifier::AsInt32:
825 case LengthModifier::AsInt3264:
826 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000827 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000828 return false;
829 }
830 llvm_unreachable("Invalid LengthModifier Kind!");
831}
832
Nico Webereffdb192015-05-18 02:41:17 +0000833bool FormatSpecifier::hasStandardConversionSpecifier(
834 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000835 switch (CS.getKind()) {
836 case ConversionSpecifier::cArg:
837 case ConversionSpecifier::dArg:
838 case ConversionSpecifier::iArg:
839 case ConversionSpecifier::oArg:
840 case ConversionSpecifier::uArg:
841 case ConversionSpecifier::xArg:
842 case ConversionSpecifier::XArg:
843 case ConversionSpecifier::fArg:
844 case ConversionSpecifier::FArg:
845 case ConversionSpecifier::eArg:
846 case ConversionSpecifier::EArg:
847 case ConversionSpecifier::gArg:
848 case ConversionSpecifier::GArg:
849 case ConversionSpecifier::aArg:
850 case ConversionSpecifier::AArg:
851 case ConversionSpecifier::sArg:
852 case ConversionSpecifier::pArg:
853 case ConversionSpecifier::nArg:
854 case ConversionSpecifier::ObjCObjArg:
855 case ConversionSpecifier::ScanListArg:
856 case ConversionSpecifier::PercentArg:
857 return true;
858 case ConversionSpecifier::CArg:
859 case ConversionSpecifier::SArg:
860 return LangOpt.ObjC1 || LangOpt.ObjC2;
861 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000862 case ConversionSpecifier::FreeBSDbArg:
863 case ConversionSpecifier::FreeBSDDArg:
864 case ConversionSpecifier::FreeBSDrArg:
865 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000866 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000867 case ConversionSpecifier::DArg:
868 case ConversionSpecifier::OArg:
869 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000870 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000871 return false;
872 }
873 llvm_unreachable("Invalid ConversionSpecifier Kind!");
874}
875
876bool FormatSpecifier::hasStandardLengthConversionCombination() const {
877 if (LM.getKind() == LengthModifier::AsLongDouble) {
878 switch(CS.getKind()) {
879 case ConversionSpecifier::dArg:
880 case ConversionSpecifier::iArg:
881 case ConversionSpecifier::oArg:
882 case ConversionSpecifier::uArg:
883 case ConversionSpecifier::xArg:
884 case ConversionSpecifier::XArg:
885 return false;
886 default:
887 return true;
888 }
889 }
890 return true;
891}
Hans Wennborg08574d32012-07-27 19:17:46 +0000892
David Blaikie05785d12013-02-20 22:23:23 +0000893Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000894 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
895 if (LM.getKind() == LengthModifier::AsLongDouble ||
896 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000897 LengthModifier FixedLM(LM);
898 FixedLM.setKind(LengthModifier::AsLongLong);
899 return FixedLM;
900 }
Jordan Rose92303592012-09-08 04:00:03 +0000901 }
902
David Blaikie7a30dc52013-02-21 01:47:18 +0000903 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000904}
905
Hans Wennborg08574d32012-07-27 19:17:46 +0000906bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
907 LengthModifier &LM) {
908 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
909 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
910
911 for (;;) {
912 const IdentifierInfo *Identifier = Typedef->getIdentifier();
913 if (Identifier->getName() == "size_t") {
914 LM.setKind(LengthModifier::AsSizeT);
915 return true;
916 } else if (Identifier->getName() == "ssize_t") {
917 // Not C99, but common in Unix.
918 LM.setKind(LengthModifier::AsSizeT);
919 return true;
920 } else if (Identifier->getName() == "intmax_t") {
921 LM.setKind(LengthModifier::AsIntMax);
922 return true;
923 } else if (Identifier->getName() == "uintmax_t") {
924 LM.setKind(LengthModifier::AsIntMax);
925 return true;
926 } else if (Identifier->getName() == "ptrdiff_t") {
927 LM.setKind(LengthModifier::AsPtrDiff);
928 return true;
929 }
930
931 QualType T = Typedef->getUnderlyingType();
932 if (!isa<TypedefType>(T))
933 break;
934
935 Typedef = cast<TypedefType>(T)->getDecl();
936 }
937 return false;
938}