blob: 2a518cac3943169d0918c0c471603135178f9453 [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";
594 case nArg: return "n";
595 case PercentArg: return "%";
596 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000597 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000598
David Majnemer3cba4952013-08-21 21:54:46 +0000599 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000600 case CArg: return "C";
601 case SArg: return "S";
602
603 // Objective-C specific specifiers.
604 case ObjCObjArg: return "@";
605
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000606 // FreeBSD kernel specific specifiers.
607 case FreeBSDbArg: return "b";
608 case FreeBSDDArg: return "D";
609 case FreeBSDrArg: return "r";
610 case FreeBSDyArg: return "y";
611
Hans Wennborga8b042d2011-12-09 11:11:07 +0000612 // GlibC specific specifiers.
613 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000614
615 // MS specific specifiers.
616 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000617 }
Craig Topper25542942014-05-20 04:30:07 +0000618 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000619}
620
David Blaikie05785d12013-02-20 22:23:23 +0000621Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000622ConversionSpecifier::getStandardSpecifier() const {
623 ConversionSpecifier::Kind NewKind;
624
625 switch (getKind()) {
626 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000627 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000628 case DArg:
629 NewKind = dArg;
630 break;
631 case UArg:
632 NewKind = uArg;
633 break;
634 case OArg:
635 NewKind = oArg;
636 break;
637 }
638
639 ConversionSpecifier FixedCS(*this);
640 FixedCS.setKind(NewKind);
641 return FixedCS;
642}
643
Hans Wennborga8b042d2011-12-09 11:11:07 +0000644//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000645// Methods on OptionalAmount.
646//===----------------------------------------------------------------------===//
647
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000648void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000649 switch (hs) {
650 case Invalid:
651 case NotSpecified:
652 return;
653 case Arg:
654 if (UsesDotPrefix)
655 os << ".";
656 if (usesPositionalArg())
657 os << "*" << getPositionalArgIndex() << "$";
658 else
659 os << "*";
660 break;
661 case Constant:
662 if (UsesDotPrefix)
663 os << ".";
664 os << amt;
665 break;
666 }
667}
668
Jordan Rose92303592012-09-08 04:00:03 +0000669bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000670 switch (LM.getKind()) {
671 case LengthModifier::None:
672 return true;
673
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000674 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000675 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000676 if (Target.getTriple().isOSMSVCRT()) {
677 switch (CS.getKind()) {
678 case ConversionSpecifier::cArg:
679 case ConversionSpecifier::CArg:
680 case ConversionSpecifier::sArg:
681 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000682 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000683 return true;
684 default:
685 break;
686 }
687 }
688 // Fall through.
689 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000690 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000691 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000692 case LengthModifier::AsIntMax:
693 case LengthModifier::AsSizeT:
694 case LengthModifier::AsPtrDiff:
695 switch (CS.getKind()) {
696 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000697 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000698 case ConversionSpecifier::iArg:
699 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000700 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000701 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000702 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000703 case ConversionSpecifier::xArg:
704 case ConversionSpecifier::XArg:
705 case ConversionSpecifier::nArg:
706 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000707 case ConversionSpecifier::FreeBSDrArg:
708 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000709 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000710 default:
711 return false;
712 }
713
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000714 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000715 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000716 switch (CS.getKind()) {
717 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000718 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000719 case ConversionSpecifier::iArg:
720 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000721 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000722 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000723 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000724 case ConversionSpecifier::xArg:
725 case ConversionSpecifier::XArg:
726 case ConversionSpecifier::aArg:
727 case ConversionSpecifier::AArg:
728 case ConversionSpecifier::fArg:
729 case ConversionSpecifier::FArg:
730 case ConversionSpecifier::eArg:
731 case ConversionSpecifier::EArg:
732 case ConversionSpecifier::gArg:
733 case ConversionSpecifier::GArg:
734 case ConversionSpecifier::nArg:
735 case ConversionSpecifier::cArg:
736 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000737 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000738 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000739 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000740 case ConversionSpecifier::FreeBSDrArg:
741 case ConversionSpecifier::FreeBSDyArg:
Sunil Srivastavae9e36312016-04-26 23:19:00 +0000742 return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Ted Kremenekea28f832010-07-20 20:04:42 +0000743 default:
744 return false;
745 }
746
747 case LengthModifier::AsLongDouble:
748 switch (CS.getKind()) {
749 case ConversionSpecifier::aArg:
750 case ConversionSpecifier::AArg:
751 case ConversionSpecifier::fArg:
752 case ConversionSpecifier::FArg:
753 case ConversionSpecifier::eArg:
754 case ConversionSpecifier::EArg:
755 case ConversionSpecifier::gArg:
756 case ConversionSpecifier::GArg:
757 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000758 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000759 case ConversionSpecifier::dArg:
760 case ConversionSpecifier::iArg:
761 case ConversionSpecifier::oArg:
762 case ConversionSpecifier::uArg:
763 case ConversionSpecifier::xArg:
764 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000765 return !Target.getTriple().isOSDarwin() &&
766 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000767 default:
768 return false;
769 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000770
771 case LengthModifier::AsAllocate:
772 switch (CS.getKind()) {
773 case ConversionSpecifier::sArg:
774 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000775 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000776 return true;
777 default:
778 return false;
779 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000780
781 case LengthModifier::AsMAllocate:
782 switch (CS.getKind()) {
783 case ConversionSpecifier::cArg:
784 case ConversionSpecifier::CArg:
785 case ConversionSpecifier::sArg:
786 case ConversionSpecifier::SArg:
787 case ConversionSpecifier::ScanListArg:
788 return true;
789 default:
790 return false;
791 }
David Majnemer3cba4952013-08-21 21:54:46 +0000792 case LengthModifier::AsInt32:
793 case LengthModifier::AsInt3264:
794 case LengthModifier::AsInt64:
795 switch (CS.getKind()) {
796 case ConversionSpecifier::dArg:
797 case ConversionSpecifier::iArg:
798 case ConversionSpecifier::oArg:
799 case ConversionSpecifier::uArg:
800 case ConversionSpecifier::xArg:
801 case ConversionSpecifier::XArg:
802 return Target.getTriple().isOSMSVCRT();
803 default:
804 return false;
805 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000806 case LengthModifier::AsWide:
807 switch (CS.getKind()) {
808 case ConversionSpecifier::cArg:
809 case ConversionSpecifier::CArg:
810 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000811 case ConversionSpecifier::SArg:
812 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000813 return Target.getTriple().isOSMSVCRT();
814 default:
815 return false;
816 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000817 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000818 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000819}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000820
821bool FormatSpecifier::hasStandardLengthModifier() const {
822 switch (LM.getKind()) {
823 case LengthModifier::None:
824 case LengthModifier::AsChar:
825 case LengthModifier::AsShort:
826 case LengthModifier::AsLong:
827 case LengthModifier::AsLongLong:
828 case LengthModifier::AsIntMax:
829 case LengthModifier::AsSizeT:
830 case LengthModifier::AsPtrDiff:
831 case LengthModifier::AsLongDouble:
832 return true;
833 case LengthModifier::AsAllocate:
834 case LengthModifier::AsMAllocate:
835 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000836 case LengthModifier::AsInt32:
837 case LengthModifier::AsInt3264:
838 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000839 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000840 return false;
841 }
842 llvm_unreachable("Invalid LengthModifier Kind!");
843}
844
Nico Webereffdb192015-05-18 02:41:17 +0000845bool FormatSpecifier::hasStandardConversionSpecifier(
846 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000847 switch (CS.getKind()) {
848 case ConversionSpecifier::cArg:
849 case ConversionSpecifier::dArg:
850 case ConversionSpecifier::iArg:
851 case ConversionSpecifier::oArg:
852 case ConversionSpecifier::uArg:
853 case ConversionSpecifier::xArg:
854 case ConversionSpecifier::XArg:
855 case ConversionSpecifier::fArg:
856 case ConversionSpecifier::FArg:
857 case ConversionSpecifier::eArg:
858 case ConversionSpecifier::EArg:
859 case ConversionSpecifier::gArg:
860 case ConversionSpecifier::GArg:
861 case ConversionSpecifier::aArg:
862 case ConversionSpecifier::AArg:
863 case ConversionSpecifier::sArg:
864 case ConversionSpecifier::pArg:
865 case ConversionSpecifier::nArg:
866 case ConversionSpecifier::ObjCObjArg:
867 case ConversionSpecifier::ScanListArg:
868 case ConversionSpecifier::PercentArg:
869 return true;
870 case ConversionSpecifier::CArg:
871 case ConversionSpecifier::SArg:
872 return LangOpt.ObjC1 || LangOpt.ObjC2;
873 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000874 case ConversionSpecifier::FreeBSDbArg:
875 case ConversionSpecifier::FreeBSDDArg:
876 case ConversionSpecifier::FreeBSDrArg:
877 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000878 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000879 case ConversionSpecifier::DArg:
880 case ConversionSpecifier::OArg:
881 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000882 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000883 return false;
884 }
885 llvm_unreachable("Invalid ConversionSpecifier Kind!");
886}
887
888bool FormatSpecifier::hasStandardLengthConversionCombination() const {
889 if (LM.getKind() == LengthModifier::AsLongDouble) {
890 switch(CS.getKind()) {
891 case ConversionSpecifier::dArg:
892 case ConversionSpecifier::iArg:
893 case ConversionSpecifier::oArg:
894 case ConversionSpecifier::uArg:
895 case ConversionSpecifier::xArg:
896 case ConversionSpecifier::XArg:
897 return false;
898 default:
899 return true;
900 }
901 }
902 return true;
903}
Hans Wennborg08574d32012-07-27 19:17:46 +0000904
David Blaikie05785d12013-02-20 22:23:23 +0000905Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000906 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
907 if (LM.getKind() == LengthModifier::AsLongDouble ||
908 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000909 LengthModifier FixedLM(LM);
910 FixedLM.setKind(LengthModifier::AsLongLong);
911 return FixedLM;
912 }
Jordan Rose92303592012-09-08 04:00:03 +0000913 }
914
David Blaikie7a30dc52013-02-21 01:47:18 +0000915 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000916}
917
Hans Wennborg08574d32012-07-27 19:17:46 +0000918bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
919 LengthModifier &LM) {
920 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
921 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
922
923 for (;;) {
924 const IdentifierInfo *Identifier = Typedef->getIdentifier();
925 if (Identifier->getName() == "size_t") {
926 LM.setKind(LengthModifier::AsSizeT);
927 return true;
928 } else if (Identifier->getName() == "ssize_t") {
929 // Not C99, but common in Unix.
930 LM.setKind(LengthModifier::AsSizeT);
931 return true;
932 } else if (Identifier->getName() == "intmax_t") {
933 LM.setKind(LengthModifier::AsIntMax);
934 return true;
935 } else if (Identifier->getName() == "uintmax_t") {
936 LM.setKind(LengthModifier::AsIntMax);
937 return true;
938 } else if (Identifier->getName() == "ptrdiff_t") {
939 LM.setKind(LengthModifier::AsPtrDiff);
940 return true;
941 }
942
943 QualType T = Typedef->getUnderlyingType();
944 if (!isa<TypedefType>(T))
945 break;
946
947 Typedef = cast<TypedefType>(T)->getDecl();
948 }
949 return false;
950}