blob: c62e537e92dd9dbc55abc89424ef21726923fac0 [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
Justin Lebar90910552016-09-30 00:38:45 +0000269 const llvm::UTF8 *SB =
270 reinterpret_cast<const llvm::UTF8 *>(SpecifierBegin + 1);
271 const llvm::UTF8 *SE = reinterpret_cast<const llvm::UTF8 *>(FmtStrEnd);
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +0000272 const char FirstByte = *SB;
273
274 // If the invalid specifier is a multibyte UTF-8 string, return the
275 // total length accordingly so that the conversion specifier can be
276 // properly updated to reflect a complete UTF-8 specifier.
Justin Lebar90910552016-09-30 00:38:45 +0000277 unsigned NumBytes = llvm::getNumBytesForUTF8(FirstByte);
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +0000278 if (NumBytes == 1)
279 return false;
280 if (SB + NumBytes > SE)
281 return false;
282
283 Len = NumBytes + 1;
284 return true;
285}
286
Ted Kremenek02087932010-07-16 02:11:22 +0000287//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000288// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000289//===----------------------------------------------------------------------===//
290
Seth Cantrellb4802962015-03-04 03:12:10 +0000291clang::analyze_format_string::ArgType::MatchKind
292ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000293 if (Ptr) {
294 // It has to be a pointer.
295 const PointerType *PT = argTy->getAs<PointerType>();
296 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000297 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000298
299 // We cannot write through a const qualified pointer.
300 if (PT->getPointeeType().isConstQualified())
Seth Cantrellb4802962015-03-04 03:12:10 +0000301 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000302
303 argTy = PT->getPointeeType();
304 }
305
Ted Kremenek02087932010-07-16 02:11:22 +0000306 switch (K) {
307 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000308 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000309
310 case UnknownTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000311 return Match;
312
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000313 case AnyCharTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000314 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
315 // If the enum is incomplete we know nothing about the underlying type.
316 // Assume that it's 'int'.
317 if (!ETy->getDecl()->isComplete())
318 return NoMatch;
Jordan Rose98709982012-06-04 22:48:57 +0000319 argTy = ETy->getDecl()->getIntegerType();
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000320 }
Jordan Rose98709982012-06-04 22:48:57 +0000321
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000322 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
323 switch (BT->getKind()) {
324 default:
325 break;
326 case BuiltinType::Char_S:
327 case BuiltinType::SChar:
328 case BuiltinType::UChar:
329 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000330 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000331 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000332 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000333 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000334
Ted Kremenek02087932010-07-16 02:11:22 +0000335 case SpecificTy: {
Benjamin Kramerf3b323d2016-08-20 16:51:33 +0000336 if (const EnumType *ETy = argTy->getAs<EnumType>()) {
337 // If the enum is incomplete we know nothing about the underlying type.
338 // Assume that it's 'int'.
339 if (!ETy->getDecl()->isComplete())
340 argTy = C.IntTy;
341 else
342 argTy = ETy->getDecl()->getIntegerType();
343 }
Ted Kremenek02087932010-07-16 02:11:22 +0000344 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000345
Nick Lewycky45ccba62011-12-02 23:21:43 +0000346 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000347 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000348 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000349 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000350 switch (BT->getKind()) {
351 default:
352 break;
353 case BuiltinType::Char_S:
354 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000355 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000356 case BuiltinType::UChar:
357 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
358 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000359 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000360 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000361 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000362 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000363 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000364 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000365 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000366 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000367 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000368 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000369 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000370 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000371 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000372 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000373 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000374 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000375 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000376 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000377 }
378
379 case CStrTy: {
380 const PointerType *PT = argTy->getAs<PointerType>();
381 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000382 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000383 QualType pointeeTy = PT->getPointeeType();
384 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
385 switch (BT->getKind()) {
386 case BuiltinType::Void:
387 case BuiltinType::Char_U:
388 case BuiltinType::UChar:
389 case BuiltinType::Char_S:
390 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000391 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000392 default:
393 break;
394 }
395
Seth Cantrellb4802962015-03-04 03:12:10 +0000396 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000397 }
398
399 case WCStrTy: {
400 const PointerType *PT = argTy->getAs<PointerType>();
401 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000402 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000403 QualType pointeeTy =
404 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000405 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000406 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000407
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000408 case WIntTy: {
Seth Cantrellb4802962015-03-04 03:12:10 +0000409
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000410 QualType PromoArg =
411 argTy->isPromotableIntegerType()
412 ? C.getPromotedIntegerType(argTy) : argTy;
Seth Cantrellb4802962015-03-04 03:12:10 +0000413
James Molloy36365542012-05-04 10:55:22 +0000414 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000415 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000416
James Molloy36365542012-05-04 10:55:22 +0000417 // If the promoted argument is the corresponding signed type of the
418 // wint_t type, then it should match.
419 if (PromoArg->hasSignedIntegerRepresentation() &&
420 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000421 return Match;
James Molloy36365542012-05-04 10:55:22 +0000422
Seth Cantrellb4802962015-03-04 03:12:10 +0000423 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000424 }
Ted Kremenek02087932010-07-16 02:11:22 +0000425
426 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000427 if (argTy->isVoidPointerType()) {
428 return Match;
429 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
430 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
431 return NoMatchPedantic;
432 } else {
433 return NoMatch;
434 }
Ted Kremenek02087932010-07-16 02:11:22 +0000435
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000436 case ObjCPointerTy: {
437 if (argTy->getAs<ObjCObjectPointerType>() ||
438 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000439 return Match;
440
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000441 // Handle implicit toll-free bridging.
442 if (const PointerType *PT = argTy->getAs<PointerType>()) {
443 // Things such as CFTypeRef are really just opaque pointers
444 // to C structs representing CF types that can often be bridged
445 // to Objective-C objects. Since the compiler doesn't know which
446 // structs can be toll-free bridged, we just accept them all.
447 QualType pointee = PT->getPointeeType();
448 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000449 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000450 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000451 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000452 }
Ted Kremenek02087932010-07-16 02:11:22 +0000453 }
454
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000455 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000456}
457
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000458QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000459 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000460 switch (K) {
461 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000462 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000463 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000464 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000465 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000466 Res = C.CharTy;
467 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000468 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000469 Res = T;
470 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000471 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000472 Res = C.getPointerType(C.CharTy);
473 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000474 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000475 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000476 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000477 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000478 Res = C.ObjCBuiltinIdTy;
479 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000480 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000481 Res = C.VoidPtrTy;
482 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000483 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000484 Res = C.getWIntType();
485 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000486 }
Ted Kremenek02087932010-07-16 02:11:22 +0000487 }
488
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000489 if (Ptr)
490 Res = C.getPointerType(Res);
491 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000492}
493
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000494std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000495 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000496
497 std::string Alias;
498 if (Name) {
499 // Use a specific name for this type, e.g. "size_t".
500 Alias = Name;
501 if (Ptr) {
502 // If ArgType is actually a pointer to T, append an asterisk.
503 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
504 }
505 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
506 if (S == Alias)
507 Alias.clear();
508 }
509
510 if (!Alias.empty())
511 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000512 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000513}
514
515
Ted Kremenek02087932010-07-16 02:11:22 +0000516//===----------------------------------------------------------------------===//
517// Methods on OptionalAmount.
518//===----------------------------------------------------------------------===//
519
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000520ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000521analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
522 return Ctx.IntTy;
523}
524
525//===----------------------------------------------------------------------===//
526// Methods on LengthModifier.
527//===----------------------------------------------------------------------===//
528
529const char *
530analyze_format_string::LengthModifier::toString() const {
531 switch (kind) {
532 case AsChar:
533 return "hh";
534 case AsShort:
535 return "h";
536 case AsLong: // or AsWideChar
537 return "l";
538 case AsLongLong:
539 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000540 case AsQuad:
541 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000542 case AsIntMax:
543 return "j";
544 case AsSizeT:
545 return "z";
546 case AsPtrDiff:
547 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000548 case AsInt32:
549 return "I32";
550 case AsInt3264:
551 return "I";
552 case AsInt64:
553 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000554 case AsLongDouble:
555 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000556 case AsAllocate:
557 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000558 case AsMAllocate:
559 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000560 case AsWide:
561 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000562 case None:
563 return "";
564 }
Craig Topper25542942014-05-20 04:30:07 +0000565 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000566}
567
568//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000569// Methods on ConversionSpecifier.
570//===----------------------------------------------------------------------===//
571
572const char *ConversionSpecifier::toString() const {
573 switch (kind) {
574 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000575 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000576 case iArg: return "i";
577 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000578 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000579 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000580 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000581 case xArg: return "x";
582 case XArg: return "X";
583 case fArg: return "f";
584 case FArg: return "F";
585 case eArg: return "e";
586 case EArg: return "E";
587 case gArg: return "g";
588 case GArg: return "G";
589 case aArg: return "a";
590 case AArg: return "A";
591 case cArg: return "c";
592 case sArg: return "s";
593 case pArg: return "p";
Mehdi Amini06d367c2016-10-24 20:39:34 +0000594 case PArg:
595 return "P";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000596 case nArg: return "n";
597 case PercentArg: return "%";
598 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000599 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000600
David Majnemer3cba4952013-08-21 21:54:46 +0000601 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000602 case CArg: return "C";
603 case SArg: return "S";
604
605 // Objective-C specific specifiers.
606 case ObjCObjArg: return "@";
607
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000608 // FreeBSD kernel specific specifiers.
609 case FreeBSDbArg: return "b";
610 case FreeBSDDArg: return "D";
611 case FreeBSDrArg: return "r";
612 case FreeBSDyArg: return "y";
613
Hans Wennborga8b042d2011-12-09 11:11:07 +0000614 // GlibC specific specifiers.
615 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000616
617 // MS specific specifiers.
618 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000619 }
Craig Topper25542942014-05-20 04:30:07 +0000620 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000621}
622
David Blaikie05785d12013-02-20 22:23:23 +0000623Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000624ConversionSpecifier::getStandardSpecifier() const {
625 ConversionSpecifier::Kind NewKind;
626
627 switch (getKind()) {
628 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000629 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000630 case DArg:
631 NewKind = dArg;
632 break;
633 case UArg:
634 NewKind = uArg;
635 break;
636 case OArg:
637 NewKind = oArg;
638 break;
639 }
640
641 ConversionSpecifier FixedCS(*this);
642 FixedCS.setKind(NewKind);
643 return FixedCS;
644}
645
Hans Wennborga8b042d2011-12-09 11:11:07 +0000646//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000647// Methods on OptionalAmount.
648//===----------------------------------------------------------------------===//
649
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000650void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000651 switch (hs) {
652 case Invalid:
653 case NotSpecified:
654 return;
655 case Arg:
656 if (UsesDotPrefix)
657 os << ".";
658 if (usesPositionalArg())
659 os << "*" << getPositionalArgIndex() << "$";
660 else
661 os << "*";
662 break;
663 case Constant:
664 if (UsesDotPrefix)
665 os << ".";
666 os << amt;
667 break;
668 }
669}
670
Jordan Rose92303592012-09-08 04:00:03 +0000671bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000672 switch (LM.getKind()) {
673 case LengthModifier::None:
674 return true;
675
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000676 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000677 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000678 if (Target.getTriple().isOSMSVCRT()) {
679 switch (CS.getKind()) {
680 case ConversionSpecifier::cArg:
681 case ConversionSpecifier::CArg:
682 case ConversionSpecifier::sArg:
683 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000684 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000685 return true;
686 default:
687 break;
688 }
689 }
690 // Fall through.
691 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000692 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000693 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000694 case LengthModifier::AsIntMax:
695 case LengthModifier::AsSizeT:
696 case LengthModifier::AsPtrDiff:
697 switch (CS.getKind()) {
698 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000699 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000700 case ConversionSpecifier::iArg:
701 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000702 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000703 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000704 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000705 case ConversionSpecifier::xArg:
706 case ConversionSpecifier::XArg:
707 case ConversionSpecifier::nArg:
708 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000709 case ConversionSpecifier::FreeBSDrArg:
710 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000711 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000712 default:
713 return false;
714 }
715
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000716 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000717 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000718 switch (CS.getKind()) {
719 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000720 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000721 case ConversionSpecifier::iArg:
722 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000723 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000724 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000725 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000726 case ConversionSpecifier::xArg:
727 case ConversionSpecifier::XArg:
728 case ConversionSpecifier::aArg:
729 case ConversionSpecifier::AArg:
730 case ConversionSpecifier::fArg:
731 case ConversionSpecifier::FArg:
732 case ConversionSpecifier::eArg:
733 case ConversionSpecifier::EArg:
734 case ConversionSpecifier::gArg:
735 case ConversionSpecifier::GArg:
736 case ConversionSpecifier::nArg:
737 case ConversionSpecifier::cArg:
738 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000739 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000740 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000741 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000742 case ConversionSpecifier::FreeBSDrArg:
743 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000744 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000745 default:
746 return false;
747 }
748
749 case LengthModifier::AsLongDouble:
750 switch (CS.getKind()) {
751 case ConversionSpecifier::aArg:
752 case ConversionSpecifier::AArg:
753 case ConversionSpecifier::fArg:
754 case ConversionSpecifier::FArg:
755 case ConversionSpecifier::eArg:
756 case ConversionSpecifier::EArg:
757 case ConversionSpecifier::gArg:
758 case ConversionSpecifier::GArg:
759 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000760 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000761 case ConversionSpecifier::dArg:
762 case ConversionSpecifier::iArg:
763 case ConversionSpecifier::oArg:
764 case ConversionSpecifier::uArg:
765 case ConversionSpecifier::xArg:
766 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000767 return !Target.getTriple().isOSDarwin() &&
768 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000769 default:
770 return false;
771 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000772
773 case LengthModifier::AsAllocate:
774 switch (CS.getKind()) {
775 case ConversionSpecifier::sArg:
776 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000777 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000778 return true;
779 default:
780 return false;
781 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000782
783 case LengthModifier::AsMAllocate:
784 switch (CS.getKind()) {
785 case ConversionSpecifier::cArg:
786 case ConversionSpecifier::CArg:
787 case ConversionSpecifier::sArg:
788 case ConversionSpecifier::SArg:
789 case ConversionSpecifier::ScanListArg:
790 return true;
791 default:
792 return false;
793 }
David Majnemer3cba4952013-08-21 21:54:46 +0000794 case LengthModifier::AsInt32:
795 case LengthModifier::AsInt3264:
796 case LengthModifier::AsInt64:
797 switch (CS.getKind()) {
798 case ConversionSpecifier::dArg:
799 case ConversionSpecifier::iArg:
800 case ConversionSpecifier::oArg:
801 case ConversionSpecifier::uArg:
802 case ConversionSpecifier::xArg:
803 case ConversionSpecifier::XArg:
804 return Target.getTriple().isOSMSVCRT();
805 default:
806 return false;
807 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000808 case LengthModifier::AsWide:
809 switch (CS.getKind()) {
810 case ConversionSpecifier::cArg:
811 case ConversionSpecifier::CArg:
812 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000813 case ConversionSpecifier::SArg:
814 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000815 return Target.getTriple().isOSMSVCRT();
816 default:
817 return false;
818 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000819 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000820 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000821}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000822
823bool FormatSpecifier::hasStandardLengthModifier() const {
824 switch (LM.getKind()) {
825 case LengthModifier::None:
826 case LengthModifier::AsChar:
827 case LengthModifier::AsShort:
828 case LengthModifier::AsLong:
829 case LengthModifier::AsLongLong:
830 case LengthModifier::AsIntMax:
831 case LengthModifier::AsSizeT:
832 case LengthModifier::AsPtrDiff:
833 case LengthModifier::AsLongDouble:
834 return true;
835 case LengthModifier::AsAllocate:
836 case LengthModifier::AsMAllocate:
837 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000838 case LengthModifier::AsInt32:
839 case LengthModifier::AsInt3264:
840 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000841 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000842 return false;
843 }
844 llvm_unreachable("Invalid LengthModifier Kind!");
845}
846
Nico Webereffdb192015-05-18 02:41:17 +0000847bool FormatSpecifier::hasStandardConversionSpecifier(
848 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000849 switch (CS.getKind()) {
850 case ConversionSpecifier::cArg:
851 case ConversionSpecifier::dArg:
852 case ConversionSpecifier::iArg:
853 case ConversionSpecifier::oArg:
854 case ConversionSpecifier::uArg:
855 case ConversionSpecifier::xArg:
856 case ConversionSpecifier::XArg:
857 case ConversionSpecifier::fArg:
858 case ConversionSpecifier::FArg:
859 case ConversionSpecifier::eArg:
860 case ConversionSpecifier::EArg:
861 case ConversionSpecifier::gArg:
862 case ConversionSpecifier::GArg:
863 case ConversionSpecifier::aArg:
864 case ConversionSpecifier::AArg:
865 case ConversionSpecifier::sArg:
866 case ConversionSpecifier::pArg:
867 case ConversionSpecifier::nArg:
868 case ConversionSpecifier::ObjCObjArg:
869 case ConversionSpecifier::ScanListArg:
870 case ConversionSpecifier::PercentArg:
Mehdi Amini06d367c2016-10-24 20:39:34 +0000871 case ConversionSpecifier::PArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000872 return true;
873 case ConversionSpecifier::CArg:
874 case ConversionSpecifier::SArg:
875 return LangOpt.ObjC1 || LangOpt.ObjC2;
876 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000877 case ConversionSpecifier::FreeBSDbArg:
878 case ConversionSpecifier::FreeBSDDArg:
879 case ConversionSpecifier::FreeBSDrArg:
880 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000881 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000882 case ConversionSpecifier::DArg:
883 case ConversionSpecifier::OArg:
884 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000885 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000886 return false;
887 }
888 llvm_unreachable("Invalid ConversionSpecifier Kind!");
889}
890
891bool FormatSpecifier::hasStandardLengthConversionCombination() const {
892 if (LM.getKind() == LengthModifier::AsLongDouble) {
893 switch(CS.getKind()) {
894 case ConversionSpecifier::dArg:
895 case ConversionSpecifier::iArg:
896 case ConversionSpecifier::oArg:
897 case ConversionSpecifier::uArg:
898 case ConversionSpecifier::xArg:
899 case ConversionSpecifier::XArg:
900 return false;
901 default:
902 return true;
903 }
904 }
905 return true;
906}
Hans Wennborg08574d32012-07-27 19:17:46 +0000907
David Blaikie05785d12013-02-20 22:23:23 +0000908Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000909 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
910 if (LM.getKind() == LengthModifier::AsLongDouble ||
911 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000912 LengthModifier FixedLM(LM);
913 FixedLM.setKind(LengthModifier::AsLongLong);
914 return FixedLM;
915 }
Jordan Rose92303592012-09-08 04:00:03 +0000916 }
917
David Blaikie7a30dc52013-02-21 01:47:18 +0000918 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000919}
920
Hans Wennborg08574d32012-07-27 19:17:46 +0000921bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
922 LengthModifier &LM) {
923 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
924 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
925
926 for (;;) {
927 const IdentifierInfo *Identifier = Typedef->getIdentifier();
928 if (Identifier->getName() == "size_t") {
929 LM.setKind(LengthModifier::AsSizeT);
930 return true;
931 } else if (Identifier->getName() == "ssize_t") {
932 // Not C99, but common in Unix.
933 LM.setKind(LengthModifier::AsSizeT);
934 return true;
935 } else if (Identifier->getName() == "intmax_t") {
936 LM.setKind(LengthModifier::AsIntMax);
937 return true;
938 } else if (Identifier->getName() == "uintmax_t") {
939 LM.setKind(LengthModifier::AsIntMax);
940 return true;
941 } else if (Identifier->getName() == "ptrdiff_t") {
942 LM.setKind(LengthModifier::AsPtrDiff);
943 return true;
944 }
945
946 QualType T = Typedef->getUnderlyingType();
947 if (!isa<TypedefType>(T))
948 break;
949
950 Typedef = cast<TypedefType>(T)->getDecl();
951 }
952 return false;
953}