blob: 1b608941f0dcfab9d0ebbd94fdfd5bb386158501 [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"
Ted Kremenek02087932010-07-16 02:11:22 +000018
Hans Wennborgc3b3da02012-08-07 08:11:26 +000019using clang::analyze_format_string::ArgType;
Ted Kremenek02087932010-07-16 02:11:22 +000020using clang::analyze_format_string::FormatStringHandler;
21using clang::analyze_format_string::FormatSpecifier;
22using clang::analyze_format_string::LengthModifier;
23using clang::analyze_format_string::OptionalAmount;
24using clang::analyze_format_string::PositionContext;
Ted Kremenekea28f832010-07-20 20:04:42 +000025using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek02087932010-07-16 02:11:22 +000026using namespace clang;
27
28// Key function to FormatStringHandler.
29FormatStringHandler::~FormatStringHandler() {}
30
31//===----------------------------------------------------------------------===//
32// Functions for parsing format strings components in both printf and
33// scanf format strings.
34//===----------------------------------------------------------------------===//
35
36OptionalAmount
37clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
38 const char *I = Beg;
39 UpdateOnReturn <const char*> UpdateBeg(Beg, I);
40
41 unsigned accumulator = 0;
42 bool hasDigits = false;
43
44 for ( ; I != E; ++I) {
45 char c = *I;
46 if (c >= '0' && c <= '9') {
47 hasDigits = true;
48 accumulator = (accumulator * 10) + (c - '0');
49 continue;
50 }
51
52 if (hasDigits)
53 return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
54 false);
55
56 break;
57 }
58
59 return OptionalAmount();
60}
61
62OptionalAmount
63clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
64 const char *E,
65 unsigned &argIndex) {
66 if (*Beg == '*') {
67 ++Beg;
68 return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
69 }
70
71 return ParseAmount(Beg, E);
72}
73
74OptionalAmount
75clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
76 const char *Start,
77 const char *&Beg,
78 const char *E,
79 PositionContext p) {
80 if (*Beg == '*') {
81 const char *I = Beg + 1;
82 const OptionalAmount &Amt = ParseAmount(I, E);
83
84 if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
85 H.HandleInvalidPosition(Beg, I - Beg, p);
86 return OptionalAmount(false);
87 }
88
89 if (I == E) {
90 // No more characters left?
91 H.HandleIncompleteSpecifier(Start, E - Start);
92 return OptionalAmount(false);
93 }
94
95 assert(Amt.getHowSpecified() == OptionalAmount::Constant);
96
97 if (*I == '$') {
98 // Handle positional arguments
99
100 // Special case: '*0$', since this is an easy mistake.
101 if (Amt.getConstantAmount() == 0) {
102 H.HandleZeroPosition(Beg, I - Beg + 1);
103 return OptionalAmount(false);
104 }
105
106 const char *Tmp = Beg;
107 Beg = ++I;
108
109 return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
110 Tmp, 0, true);
111 }
112
113 H.HandleInvalidPosition(Beg, I - Beg, p);
114 return OptionalAmount(false);
115 }
116
117 return ParseAmount(Beg, E);
118}
119
120
121bool
122clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
123 FormatSpecifier &CS,
124 const char *Start,
125 const char *&Beg, const char *E,
126 unsigned *argIndex) {
127 // FIXME: Support negative field widths.
128 if (argIndex) {
129 CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
130 }
131 else {
132 const OptionalAmount Amt =
133 ParsePositionAmount(H, Start, Beg, E,
134 analyze_format_string::FieldWidthPos);
135
136 if (Amt.isInvalid())
137 return true;
138 CS.setFieldWidth(Amt);
139 }
140 return false;
141}
142
143bool
144clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
145 FormatSpecifier &FS,
146 const char *Start,
147 const char *&Beg,
148 const char *E) {
149 const char *I = Beg;
150
151 const OptionalAmount &Amt = ParseAmount(I, E);
152
153 if (I == E) {
154 // No more characters left?
155 H.HandleIncompleteSpecifier(Start, E - Start);
156 return true;
157 }
158
159 if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
Hans Wennborgaa8c61c2012-03-09 10:10:54 +0000160 // Warn that positional arguments are non-standard.
161 H.HandlePosition(Start, I - Start);
162
Ted Kremenek02087932010-07-16 02:11:22 +0000163 // Special case: '%0$', since this is an easy mistake.
164 if (Amt.getConstantAmount() == 0) {
165 H.HandleZeroPosition(Start, I - Start);
166 return true;
167 }
168
169 FS.setArgIndex(Amt.getConstantAmount() - 1);
170 FS.setUsesPositionalArg();
171 // Update the caller's pointer if we decided to consume
172 // these characters.
173 Beg = I;
174 return false;
175 }
176
177 return false;
178}
179
180bool
181clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
182 const char *&I,
Hans Wennborg23926bd2011-12-15 10:25:47 +0000183 const char *E,
184 const LangOptions &LO,
185 bool IsScanf) {
Ted Kremenek02087932010-07-16 02:11:22 +0000186 LengthModifier::Kind lmKind = LengthModifier::None;
187 const char *lmPosition = I;
188 switch (*I) {
189 default:
190 return false;
191 case 'h':
192 ++I;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000193 lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
194 : LengthModifier::AsShort;
Ted Kremenek02087932010-07-16 02:11:22 +0000195 break;
196 case 'l':
197 ++I;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000198 lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
199 : LengthModifier::AsLong;
Ted Kremenek02087932010-07-16 02:11:22 +0000200 break;
201 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
202 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
203 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
204 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000205 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborg23926bd2011-12-15 10:25:47 +0000206 case 'a':
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000207 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
Hans Wennborg23926bd2011-12-15 10:25:47 +0000208 // For scanf in C90, look at the next character to see if this should
209 // be parsed as the GNU extension 'a' length modifier. If not, this
210 // will be parsed as a conversion specifier.
211 ++I;
212 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
213 lmKind = LengthModifier::AsAllocate;
214 break;
215 }
216 --I;
217 }
218 return false;
Hans Wennborg6073e312012-01-12 17:11:12 +0000219 case 'm':
220 if (IsScanf) {
221 lmKind = LengthModifier::AsMAllocate;
222 ++I;
223 break;
224 }
225 return false;
David Majnemer3cba4952013-08-21 21:54:46 +0000226 // printf: AsInt64, AsInt32, AsInt3264
227 // scanf: AsInt64
228 case 'I':
229 if (I + 1 != E && I + 2 != E) {
230 if (I[1] == '6' && I[2] == '4') {
231 I += 3;
232 lmKind = LengthModifier::AsInt64;
233 break;
234 }
235 if (IsScanf)
236 return false;
237
238 if (I[1] == '3' && I[2] == '2') {
239 I += 3;
240 lmKind = LengthModifier::AsInt32;
241 break;
242 }
243 }
244 ++I;
245 lmKind = LengthModifier::AsInt3264;
246 break;
Hans Wennborg68f42b92014-09-04 21:39:46 +0000247 case 'w':
248 lmKind = LengthModifier::AsWide; ++I; break;
Ted Kremenek02087932010-07-16 02:11:22 +0000249 }
250 LengthModifier lm(lmPosition, lmKind);
251 FS.setLengthModifier(lm);
252 return true;
253}
254
255//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000256// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000257//===----------------------------------------------------------------------===//
258
Seth Cantrellb4802962015-03-04 03:12:10 +0000259clang::analyze_format_string::ArgType::MatchKind
260ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000261 if (Ptr) {
262 // It has to be a pointer.
263 const PointerType *PT = argTy->getAs<PointerType>();
264 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000265 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000266
267 // We cannot write through a const qualified pointer.
268 if (PT->getPointeeType().isConstQualified())
Seth Cantrellb4802962015-03-04 03:12:10 +0000269 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000270
271 argTy = PT->getPointeeType();
272 }
273
Ted Kremenek02087932010-07-16 02:11:22 +0000274 switch (K) {
275 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000276 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000277
278 case UnknownTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000279 return Match;
280
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000281 case AnyCharTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000282 if (const EnumType *ETy = argTy->getAs<EnumType>())
283 argTy = ETy->getDecl()->getIntegerType();
284
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000285 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
286 switch (BT->getKind()) {
287 default:
288 break;
289 case BuiltinType::Char_S:
290 case BuiltinType::SChar:
291 case BuiltinType::UChar:
292 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000293 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000294 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000295 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000296 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000297
Ted Kremenek02087932010-07-16 02:11:22 +0000298 case SpecificTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000299 if (const EnumType *ETy = argTy->getAs<EnumType>())
300 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek02087932010-07-16 02:11:22 +0000301 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000302
Nick Lewycky45ccba62011-12-02 23:21:43 +0000303 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000304 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000305 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000306 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000307 switch (BT->getKind()) {
308 default:
309 break;
310 case BuiltinType::Char_S:
311 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000312 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000313 case BuiltinType::UChar:
314 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
315 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000316 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000317 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000318 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000319 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000320 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000321 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000322 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000323 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000324 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000325 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000326 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000327 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000328 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000329 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000330 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000331 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000332 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000333 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000334 }
335
336 case CStrTy: {
337 const PointerType *PT = argTy->getAs<PointerType>();
338 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000339 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000340 QualType pointeeTy = PT->getPointeeType();
341 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
342 switch (BT->getKind()) {
343 case BuiltinType::Void:
344 case BuiltinType::Char_U:
345 case BuiltinType::UChar:
346 case BuiltinType::Char_S:
347 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000348 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000349 default:
350 break;
351 }
352
Seth Cantrellb4802962015-03-04 03:12:10 +0000353 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000354 }
355
356 case WCStrTy: {
357 const PointerType *PT = argTy->getAs<PointerType>();
358 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000359 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000360 QualType pointeeTy =
361 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000362 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000363 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000364
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000365 case WIntTy: {
Seth Cantrellb4802962015-03-04 03:12:10 +0000366
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000367 QualType PromoArg =
368 argTy->isPromotableIntegerType()
369 ? C.getPromotedIntegerType(argTy) : argTy;
Seth Cantrellb4802962015-03-04 03:12:10 +0000370
James Molloy36365542012-05-04 10:55:22 +0000371 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000372 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000373
James Molloy36365542012-05-04 10:55:22 +0000374 // If the promoted argument is the corresponding signed type of the
375 // wint_t type, then it should match.
376 if (PromoArg->hasSignedIntegerRepresentation() &&
377 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000378 return Match;
James Molloy36365542012-05-04 10:55:22 +0000379
Seth Cantrellb4802962015-03-04 03:12:10 +0000380 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000381 }
Ted Kremenek02087932010-07-16 02:11:22 +0000382
383 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000384 if (argTy->isVoidPointerType()) {
385 return Match;
386 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
387 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
388 return NoMatchPedantic;
389 } else {
390 return NoMatch;
391 }
Ted Kremenek02087932010-07-16 02:11:22 +0000392
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000393 case ObjCPointerTy: {
394 if (argTy->getAs<ObjCObjectPointerType>() ||
395 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000396 return Match;
397
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000398 // Handle implicit toll-free bridging.
399 if (const PointerType *PT = argTy->getAs<PointerType>()) {
400 // Things such as CFTypeRef are really just opaque pointers
401 // to C structs representing CF types that can often be bridged
402 // to Objective-C objects. Since the compiler doesn't know which
403 // structs can be toll-free bridged, we just accept them all.
404 QualType pointee = PT->getPointeeType();
405 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000406 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000407 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000408 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000409 }
Ted Kremenek02087932010-07-16 02:11:22 +0000410 }
411
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000412 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000413}
414
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000415QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000416 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000417 switch (K) {
418 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000419 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000420 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000421 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000422 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000423 Res = C.CharTy;
424 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000425 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000426 Res = T;
427 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000428 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000429 Res = C.getPointerType(C.CharTy);
430 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000431 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000432 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000433 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000434 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000435 Res = C.ObjCBuiltinIdTy;
436 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000437 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000438 Res = C.VoidPtrTy;
439 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000440 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000441 Res = C.getWIntType();
442 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000443 }
Ted Kremenek02087932010-07-16 02:11:22 +0000444 }
445
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000446 if (Ptr)
447 Res = C.getPointerType(Res);
448 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000449}
450
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000451std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000452 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000453
454 std::string Alias;
455 if (Name) {
456 // Use a specific name for this type, e.g. "size_t".
457 Alias = Name;
458 if (Ptr) {
459 // If ArgType is actually a pointer to T, append an asterisk.
460 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
461 }
462 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
463 if (S == Alias)
464 Alias.clear();
465 }
466
467 if (!Alias.empty())
468 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000469 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000470}
471
472
Ted Kremenek02087932010-07-16 02:11:22 +0000473//===----------------------------------------------------------------------===//
474// Methods on OptionalAmount.
475//===----------------------------------------------------------------------===//
476
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000477ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000478analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
479 return Ctx.IntTy;
480}
481
482//===----------------------------------------------------------------------===//
483// Methods on LengthModifier.
484//===----------------------------------------------------------------------===//
485
486const char *
487analyze_format_string::LengthModifier::toString() const {
488 switch (kind) {
489 case AsChar:
490 return "hh";
491 case AsShort:
492 return "h";
493 case AsLong: // or AsWideChar
494 return "l";
495 case AsLongLong:
496 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000497 case AsQuad:
498 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000499 case AsIntMax:
500 return "j";
501 case AsSizeT:
502 return "z";
503 case AsPtrDiff:
504 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000505 case AsInt32:
506 return "I32";
507 case AsInt3264:
508 return "I";
509 case AsInt64:
510 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000511 case AsLongDouble:
512 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000513 case AsAllocate:
514 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000515 case AsMAllocate:
516 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000517 case AsWide:
518 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000519 case None:
520 return "";
521 }
Craig Topper25542942014-05-20 04:30:07 +0000522 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000523}
524
525//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000526// Methods on ConversionSpecifier.
527//===----------------------------------------------------------------------===//
528
529const char *ConversionSpecifier::toString() const {
530 switch (kind) {
531 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000532 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000533 case iArg: return "i";
534 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000535 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000536 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000537 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000538 case xArg: return "x";
539 case XArg: return "X";
540 case fArg: return "f";
541 case FArg: return "F";
542 case eArg: return "e";
543 case EArg: return "E";
544 case gArg: return "g";
545 case GArg: return "G";
546 case aArg: return "a";
547 case AArg: return "A";
548 case cArg: return "c";
549 case sArg: return "s";
550 case pArg: return "p";
551 case nArg: return "n";
552 case PercentArg: return "%";
553 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000554 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000555
David Majnemer3cba4952013-08-21 21:54:46 +0000556 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000557 case CArg: return "C";
558 case SArg: return "S";
559
560 // Objective-C specific specifiers.
561 case ObjCObjArg: return "@";
562
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000563 // FreeBSD kernel specific specifiers.
564 case FreeBSDbArg: return "b";
565 case FreeBSDDArg: return "D";
566 case FreeBSDrArg: return "r";
567 case FreeBSDyArg: return "y";
568
Hans Wennborga8b042d2011-12-09 11:11:07 +0000569 // GlibC specific specifiers.
570 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000571
572 // MS specific specifiers.
573 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000574 }
Craig Topper25542942014-05-20 04:30:07 +0000575 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000576}
577
David Blaikie05785d12013-02-20 22:23:23 +0000578Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000579ConversionSpecifier::getStandardSpecifier() const {
580 ConversionSpecifier::Kind NewKind;
581
582 switch (getKind()) {
583 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000584 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000585 case DArg:
586 NewKind = dArg;
587 break;
588 case UArg:
589 NewKind = uArg;
590 break;
591 case OArg:
592 NewKind = oArg;
593 break;
594 }
595
596 ConversionSpecifier FixedCS(*this);
597 FixedCS.setKind(NewKind);
598 return FixedCS;
599}
600
Hans Wennborga8b042d2011-12-09 11:11:07 +0000601//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000602// Methods on OptionalAmount.
603//===----------------------------------------------------------------------===//
604
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000605void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000606 switch (hs) {
607 case Invalid:
608 case NotSpecified:
609 return;
610 case Arg:
611 if (UsesDotPrefix)
612 os << ".";
613 if (usesPositionalArg())
614 os << "*" << getPositionalArgIndex() << "$";
615 else
616 os << "*";
617 break;
618 case Constant:
619 if (UsesDotPrefix)
620 os << ".";
621 os << amt;
622 break;
623 }
624}
625
Jordan Rose92303592012-09-08 04:00:03 +0000626bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000627 switch (LM.getKind()) {
628 case LengthModifier::None:
629 return true;
630
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000631 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000632 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000633 if (Target.getTriple().isOSMSVCRT()) {
634 switch (CS.getKind()) {
635 case ConversionSpecifier::cArg:
636 case ConversionSpecifier::CArg:
637 case ConversionSpecifier::sArg:
638 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000639 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000640 return true;
641 default:
642 break;
643 }
644 }
645 // Fall through.
646 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000647 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000648 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000649 case LengthModifier::AsIntMax:
650 case LengthModifier::AsSizeT:
651 case LengthModifier::AsPtrDiff:
652 switch (CS.getKind()) {
653 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000654 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000655 case ConversionSpecifier::iArg:
656 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000657 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000658 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000659 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000660 case ConversionSpecifier::xArg:
661 case ConversionSpecifier::XArg:
662 case ConversionSpecifier::nArg:
663 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000664 case ConversionSpecifier::FreeBSDrArg:
665 case ConversionSpecifier::FreeBSDyArg:
666 return Target.getTriple().isOSFreeBSD();
Ted Kremenekea28f832010-07-20 20:04:42 +0000667 default:
668 return false;
669 }
670
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000671 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000672 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000673 switch (CS.getKind()) {
674 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000675 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000676 case ConversionSpecifier::iArg:
677 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000678 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000679 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000680 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000681 case ConversionSpecifier::xArg:
682 case ConversionSpecifier::XArg:
683 case ConversionSpecifier::aArg:
684 case ConversionSpecifier::AArg:
685 case ConversionSpecifier::fArg:
686 case ConversionSpecifier::FArg:
687 case ConversionSpecifier::eArg:
688 case ConversionSpecifier::EArg:
689 case ConversionSpecifier::gArg:
690 case ConversionSpecifier::GArg:
691 case ConversionSpecifier::nArg:
692 case ConversionSpecifier::cArg:
693 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000694 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000695 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000696 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000697 case ConversionSpecifier::FreeBSDrArg:
698 case ConversionSpecifier::FreeBSDyArg:
699 return Target.getTriple().isOSFreeBSD();
Ted Kremenekea28f832010-07-20 20:04:42 +0000700 default:
701 return false;
702 }
703
704 case LengthModifier::AsLongDouble:
705 switch (CS.getKind()) {
706 case ConversionSpecifier::aArg:
707 case ConversionSpecifier::AArg:
708 case ConversionSpecifier::fArg:
709 case ConversionSpecifier::FArg:
710 case ConversionSpecifier::eArg:
711 case ConversionSpecifier::EArg:
712 case ConversionSpecifier::gArg:
713 case ConversionSpecifier::GArg:
714 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000715 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000716 case ConversionSpecifier::dArg:
717 case ConversionSpecifier::iArg:
718 case ConversionSpecifier::oArg:
719 case ConversionSpecifier::uArg:
720 case ConversionSpecifier::xArg:
721 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000722 return !Target.getTriple().isOSDarwin() &&
723 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000724 default:
725 return false;
726 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000727
728 case LengthModifier::AsAllocate:
729 switch (CS.getKind()) {
730 case ConversionSpecifier::sArg:
731 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000732 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000733 return true;
734 default:
735 return false;
736 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000737
738 case LengthModifier::AsMAllocate:
739 switch (CS.getKind()) {
740 case ConversionSpecifier::cArg:
741 case ConversionSpecifier::CArg:
742 case ConversionSpecifier::sArg:
743 case ConversionSpecifier::SArg:
744 case ConversionSpecifier::ScanListArg:
745 return true;
746 default:
747 return false;
748 }
David Majnemer3cba4952013-08-21 21:54:46 +0000749 case LengthModifier::AsInt32:
750 case LengthModifier::AsInt3264:
751 case LengthModifier::AsInt64:
752 switch (CS.getKind()) {
753 case ConversionSpecifier::dArg:
754 case ConversionSpecifier::iArg:
755 case ConversionSpecifier::oArg:
756 case ConversionSpecifier::uArg:
757 case ConversionSpecifier::xArg:
758 case ConversionSpecifier::XArg:
759 return Target.getTriple().isOSMSVCRT();
760 default:
761 return false;
762 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000763 case LengthModifier::AsWide:
764 switch (CS.getKind()) {
765 case ConversionSpecifier::cArg:
766 case ConversionSpecifier::CArg:
767 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000768 case ConversionSpecifier::SArg:
769 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000770 return Target.getTriple().isOSMSVCRT();
771 default:
772 return false;
773 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000774 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000775 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000776}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000777
778bool FormatSpecifier::hasStandardLengthModifier() const {
779 switch (LM.getKind()) {
780 case LengthModifier::None:
781 case LengthModifier::AsChar:
782 case LengthModifier::AsShort:
783 case LengthModifier::AsLong:
784 case LengthModifier::AsLongLong:
785 case LengthModifier::AsIntMax:
786 case LengthModifier::AsSizeT:
787 case LengthModifier::AsPtrDiff:
788 case LengthModifier::AsLongDouble:
789 return true;
790 case LengthModifier::AsAllocate:
791 case LengthModifier::AsMAllocate:
792 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000793 case LengthModifier::AsInt32:
794 case LengthModifier::AsInt3264:
795 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000796 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000797 return false;
798 }
799 llvm_unreachable("Invalid LengthModifier Kind!");
800}
801
802bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
803 switch (CS.getKind()) {
804 case ConversionSpecifier::cArg:
805 case ConversionSpecifier::dArg:
806 case ConversionSpecifier::iArg:
807 case ConversionSpecifier::oArg:
808 case ConversionSpecifier::uArg:
809 case ConversionSpecifier::xArg:
810 case ConversionSpecifier::XArg:
811 case ConversionSpecifier::fArg:
812 case ConversionSpecifier::FArg:
813 case ConversionSpecifier::eArg:
814 case ConversionSpecifier::EArg:
815 case ConversionSpecifier::gArg:
816 case ConversionSpecifier::GArg:
817 case ConversionSpecifier::aArg:
818 case ConversionSpecifier::AArg:
819 case ConversionSpecifier::sArg:
820 case ConversionSpecifier::pArg:
821 case ConversionSpecifier::nArg:
822 case ConversionSpecifier::ObjCObjArg:
823 case ConversionSpecifier::ScanListArg:
824 case ConversionSpecifier::PercentArg:
825 return true;
826 case ConversionSpecifier::CArg:
827 case ConversionSpecifier::SArg:
828 return LangOpt.ObjC1 || LangOpt.ObjC2;
829 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000830 case ConversionSpecifier::FreeBSDbArg:
831 case ConversionSpecifier::FreeBSDDArg:
832 case ConversionSpecifier::FreeBSDrArg:
833 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000834 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000835 case ConversionSpecifier::DArg:
836 case ConversionSpecifier::OArg:
837 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000838 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000839 return false;
840 }
841 llvm_unreachable("Invalid ConversionSpecifier Kind!");
842}
843
844bool FormatSpecifier::hasStandardLengthConversionCombination() const {
845 if (LM.getKind() == LengthModifier::AsLongDouble) {
846 switch(CS.getKind()) {
847 case ConversionSpecifier::dArg:
848 case ConversionSpecifier::iArg:
849 case ConversionSpecifier::oArg:
850 case ConversionSpecifier::uArg:
851 case ConversionSpecifier::xArg:
852 case ConversionSpecifier::XArg:
853 return false;
854 default:
855 return true;
856 }
857 }
858 return true;
859}
Hans Wennborg08574d32012-07-27 19:17:46 +0000860
David Blaikie05785d12013-02-20 22:23:23 +0000861Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000862 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
863 if (LM.getKind() == LengthModifier::AsLongDouble ||
864 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000865 LengthModifier FixedLM(LM);
866 FixedLM.setKind(LengthModifier::AsLongLong);
867 return FixedLM;
868 }
Jordan Rose92303592012-09-08 04:00:03 +0000869 }
870
David Blaikie7a30dc52013-02-21 01:47:18 +0000871 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000872}
873
Hans Wennborg08574d32012-07-27 19:17:46 +0000874bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
875 LengthModifier &LM) {
876 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
877 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
878
879 for (;;) {
880 const IdentifierInfo *Identifier = Typedef->getIdentifier();
881 if (Identifier->getName() == "size_t") {
882 LM.setKind(LengthModifier::AsSizeT);
883 return true;
884 } else if (Identifier->getName() == "ssize_t") {
885 // Not C99, but common in Unix.
886 LM.setKind(LengthModifier::AsSizeT);
887 return true;
888 } else if (Identifier->getName() == "intmax_t") {
889 LM.setKind(LengthModifier::AsIntMax);
890 return true;
891 } else if (Identifier->getName() == "uintmax_t") {
892 LM.setKind(LengthModifier::AsIntMax);
893 return true;
894 } else if (Identifier->getName() == "ptrdiff_t") {
895 LM.setKind(LengthModifier::AsPtrDiff);
896 return true;
897 }
898
899 QualType T = Typedef->getUnderlyingType();
900 if (!isa<TypedefType>(T))
901 break;
902
903 Typedef = cast<TypedefType>(T)->getDecl();
904 }
905 return false;
906}