blob: 1c42ec0e87c197c50f8f1341c39110b387bc4af0 [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.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000029FormatStringHandler::~FormatStringHandler() {}
Ted Kremenek02087932010-07-16 02:11:22 +000030
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;
Richard Trieucc3949d2016-02-18 22:34:54 +0000193 if (I != E && *I == 'h') {
194 ++I;
195 lmKind = LengthModifier::AsChar;
196 } else {
197 lmKind = LengthModifier::AsShort;
198 }
Ted Kremenek02087932010-07-16 02:11:22 +0000199 break;
200 case 'l':
201 ++I;
Richard Trieucc3949d2016-02-18 22:34:54 +0000202 if (I != E && *I == 'l') {
203 ++I;
204 lmKind = LengthModifier::AsLongLong;
205 } else {
206 lmKind = LengthModifier::AsLong;
207 }
Ted Kremenek02087932010-07-16 02:11:22 +0000208 break;
209 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
210 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
211 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
212 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000213 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborg23926bd2011-12-15 10:25:47 +0000214 case 'a':
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000215 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
Hans Wennborg23926bd2011-12-15 10:25:47 +0000216 // For scanf in C90, look at the next character to see if this should
217 // be parsed as the GNU extension 'a' length modifier. If not, this
218 // will be parsed as a conversion specifier.
219 ++I;
220 if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
221 lmKind = LengthModifier::AsAllocate;
222 break;
223 }
224 --I;
225 }
226 return false;
Hans Wennborg6073e312012-01-12 17:11:12 +0000227 case 'm':
228 if (IsScanf) {
229 lmKind = LengthModifier::AsMAllocate;
230 ++I;
231 break;
232 }
233 return false;
David Majnemer3cba4952013-08-21 21:54:46 +0000234 // printf: AsInt64, AsInt32, AsInt3264
235 // scanf: AsInt64
236 case 'I':
237 if (I + 1 != E && I + 2 != E) {
238 if (I[1] == '6' && I[2] == '4') {
239 I += 3;
240 lmKind = LengthModifier::AsInt64;
241 break;
242 }
243 if (IsScanf)
244 return false;
245
246 if (I[1] == '3' && I[2] == '2') {
247 I += 3;
248 lmKind = LengthModifier::AsInt32;
249 break;
250 }
251 }
252 ++I;
253 lmKind = LengthModifier::AsInt3264;
254 break;
Hans Wennborg68f42b92014-09-04 21:39:46 +0000255 case 'w':
256 lmKind = LengthModifier::AsWide; ++I; break;
Ted Kremenek02087932010-07-16 02:11:22 +0000257 }
258 LengthModifier lm(lmPosition, lmKind);
259 FS.setLengthModifier(lm);
260 return true;
261}
262
263//===----------------------------------------------------------------------===//
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000264// Methods on ArgType.
Ted Kremenek02087932010-07-16 02:11:22 +0000265//===----------------------------------------------------------------------===//
266
Seth Cantrellb4802962015-03-04 03:12:10 +0000267clang::analyze_format_string::ArgType::MatchKind
268ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000269 if (Ptr) {
270 // It has to be a pointer.
271 const PointerType *PT = argTy->getAs<PointerType>();
272 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000273 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000274
275 // We cannot write through a const qualified pointer.
276 if (PT->getPointeeType().isConstQualified())
Seth Cantrellb4802962015-03-04 03:12:10 +0000277 return NoMatch;
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000278
279 argTy = PT->getPointeeType();
280 }
281
Ted Kremenek02087932010-07-16 02:11:22 +0000282 switch (K) {
283 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000284 llvm_unreachable("ArgType must be valid");
Ted Kremenek02087932010-07-16 02:11:22 +0000285
286 case UnknownTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000287 return Match;
288
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000289 case AnyCharTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000290 if (const EnumType *ETy = argTy->getAs<EnumType>())
291 argTy = ETy->getDecl()->getIntegerType();
292
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000293 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
294 switch (BT->getKind()) {
295 default:
296 break;
297 case BuiltinType::Char_S:
298 case BuiltinType::SChar:
299 case BuiltinType::UChar:
300 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000301 return Match;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000302 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000303 return NoMatch;
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000304 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000305
Ted Kremenek02087932010-07-16 02:11:22 +0000306 case SpecificTy: {
Jordan Rose98709982012-06-04 22:48:57 +0000307 if (const EnumType *ETy = argTy->getAs<EnumType>())
308 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek02087932010-07-16 02:11:22 +0000309 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Rose98709982012-06-04 22:48:57 +0000310
Nick Lewycky45ccba62011-12-02 23:21:43 +0000311 if (T == argTy)
Seth Cantrellb4802962015-03-04 03:12:10 +0000312 return Match;
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000313 // Check for "compatible types".
Ted Kremenekd0c2afd2011-07-14 17:05:32 +0000314 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek02087932010-07-16 02:11:22 +0000315 switch (BT->getKind()) {
316 default:
317 break;
318 case BuiltinType::Char_S:
319 case BuiltinType::SChar:
Ted Kremenekcc47e0f2011-07-13 17:35:14 +0000320 case BuiltinType::Char_U:
Seth Cantrellb4802962015-03-04 03:12:10 +0000321 case BuiltinType::UChar:
322 return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
323 : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000324 case BuiltinType::Short:
Seth Cantrellb4802962015-03-04 03:12:10 +0000325 return T == C.UnsignedShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000326 case BuiltinType::UShort:
Seth Cantrellb4802962015-03-04 03:12:10 +0000327 return T == C.ShortTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000328 case BuiltinType::Int:
Seth Cantrellb4802962015-03-04 03:12:10 +0000329 return T == C.UnsignedIntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000330 case BuiltinType::UInt:
Seth Cantrellb4802962015-03-04 03:12:10 +0000331 return T == C.IntTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000332 case BuiltinType::Long:
Seth Cantrellb4802962015-03-04 03:12:10 +0000333 return T == C.UnsignedLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000334 case BuiltinType::ULong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000335 return T == C.LongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000336 case BuiltinType::LongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000337 return T == C.UnsignedLongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000338 case BuiltinType::ULongLong:
Seth Cantrellb4802962015-03-04 03:12:10 +0000339 return T == C.LongLongTy ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000340 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000341 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000342 }
343
344 case CStrTy: {
345 const PointerType *PT = argTy->getAs<PointerType>();
346 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000347 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000348 QualType pointeeTy = PT->getPointeeType();
349 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
350 switch (BT->getKind()) {
351 case BuiltinType::Void:
352 case BuiltinType::Char_U:
353 case BuiltinType::UChar:
354 case BuiltinType::Char_S:
355 case BuiltinType::SChar:
Seth Cantrellb4802962015-03-04 03:12:10 +0000356 return Match;
Ted Kremenek02087932010-07-16 02:11:22 +0000357 default:
358 break;
359 }
360
Seth Cantrellb4802962015-03-04 03:12:10 +0000361 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000362 }
363
364 case WCStrTy: {
365 const PointerType *PT = argTy->getAs<PointerType>();
366 if (!PT)
Seth Cantrellb4802962015-03-04 03:12:10 +0000367 return NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000368 QualType pointeeTy =
369 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000370 return pointeeTy == C.getWideCharType() ? Match : NoMatch;
Ted Kremenek02087932010-07-16 02:11:22 +0000371 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000372
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000373 case WIntTy: {
Seth Cantrellb4802962015-03-04 03:12:10 +0000374
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000375 QualType PromoArg =
376 argTy->isPromotableIntegerType()
377 ? C.getPromotedIntegerType(argTy) : argTy;
Seth Cantrellb4802962015-03-04 03:12:10 +0000378
James Molloy36365542012-05-04 10:55:22 +0000379 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000380 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
Seth Cantrellb4802962015-03-04 03:12:10 +0000381
James Molloy36365542012-05-04 10:55:22 +0000382 // If the promoted argument is the corresponding signed type of the
383 // wint_t type, then it should match.
384 if (PromoArg->hasSignedIntegerRepresentation() &&
385 C.getCorrespondingUnsignedType(PromoArg) == WInt)
Seth Cantrellb4802962015-03-04 03:12:10 +0000386 return Match;
James Molloy36365542012-05-04 10:55:22 +0000387
Seth Cantrellb4802962015-03-04 03:12:10 +0000388 return WInt == PromoArg ? Match : NoMatch;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000389 }
Ted Kremenek02087932010-07-16 02:11:22 +0000390
391 case CPointerTy:
Seth Cantrellb4802962015-03-04 03:12:10 +0000392 if (argTy->isVoidPointerType()) {
393 return Match;
394 } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
395 argTy->isBlockPointerType() || argTy->isNullPtrType()) {
396 return NoMatchPedantic;
397 } else {
398 return NoMatch;
399 }
Ted Kremenek02087932010-07-16 02:11:22 +0000400
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000401 case ObjCPointerTy: {
402 if (argTy->getAs<ObjCObjectPointerType>() ||
403 argTy->getAs<BlockPointerType>())
Seth Cantrellb4802962015-03-04 03:12:10 +0000404 return Match;
405
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000406 // Handle implicit toll-free bridging.
407 if (const PointerType *PT = argTy->getAs<PointerType>()) {
408 // Things such as CFTypeRef are really just opaque pointers
409 // to C structs representing CF types that can often be bridged
410 // to Objective-C objects. Since the compiler doesn't know which
411 // structs can be toll-free bridged, we just accept them all.
412 QualType pointee = PT->getPointeeType();
413 if (pointee->getAsStructureType() || pointee->isVoidType())
Seth Cantrellb4802962015-03-04 03:12:10 +0000414 return Match;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000415 }
Seth Cantrellb4802962015-03-04 03:12:10 +0000416 return NoMatch;
Ted Kremenekd22b98a2012-02-06 21:45:29 +0000417 }
Ted Kremenek02087932010-07-16 02:11:22 +0000418 }
419
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000420 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek02087932010-07-16 02:11:22 +0000421}
422
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000423QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000424 QualType Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000425 switch (K) {
426 case InvalidTy:
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000427 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek02087932010-07-16 02:11:22 +0000428 case UnknownTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000429 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek74e82bd2011-10-25 04:20:41 +0000430 case AnyCharTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000431 Res = C.CharTy;
432 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000433 case SpecificTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000434 Res = T;
435 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000436 case CStrTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000437 Res = C.getPointerType(C.CharTy);
438 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000439 case WCStrTy:
Hans Wennborg0d81e012013-05-10 10:08:40 +0000440 Res = C.getPointerType(C.getWideCharType());
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000441 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000442 case ObjCPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000443 Res = C.ObjCBuiltinIdTy;
444 break;
Ted Kremenek02087932010-07-16 02:11:22 +0000445 case CPointerTy:
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000446 Res = C.VoidPtrTy;
447 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000448 case WIntTy: {
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000449 Res = C.getWIntType();
450 break;
Ted Kremenek5f0c0662010-08-24 22:24:51 +0000451 }
Ted Kremenek02087932010-07-16 02:11:22 +0000452 }
453
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000454 if (Ptr)
455 Res = C.getPointerType(Res);
456 return Res;
Ted Kremenek02087932010-07-16 02:11:22 +0000457}
458
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000459std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000460 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborgb1ab2a82012-08-07 08:59:46 +0000461
462 std::string Alias;
463 if (Name) {
464 // Use a specific name for this type, e.g. "size_t".
465 Alias = Name;
466 if (Ptr) {
467 // If ArgType is actually a pointer to T, append an asterisk.
468 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
469 }
470 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
471 if (S == Alias)
472 Alias.clear();
473 }
474
475 if (!Alias.empty())
476 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborga5b1aa92011-12-09 12:22:12 +0000477 return std::string("'") + S + "'";
Hans Wennborg772e9272011-12-07 10:33:11 +0000478}
479
480
Ted Kremenek02087932010-07-16 02:11:22 +0000481//===----------------------------------------------------------------------===//
482// Methods on OptionalAmount.
483//===----------------------------------------------------------------------===//
484
Hans Wennborgc3b3da02012-08-07 08:11:26 +0000485ArgType
Ted Kremenek02087932010-07-16 02:11:22 +0000486analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
487 return Ctx.IntTy;
488}
489
490//===----------------------------------------------------------------------===//
491// Methods on LengthModifier.
492//===----------------------------------------------------------------------===//
493
494const char *
495analyze_format_string::LengthModifier::toString() const {
496 switch (kind) {
497 case AsChar:
498 return "hh";
499 case AsShort:
500 return "h";
501 case AsLong: // or AsWideChar
502 return "l";
503 case AsLongLong:
504 return "ll";
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000505 case AsQuad:
506 return "q";
Ted Kremenek02087932010-07-16 02:11:22 +0000507 case AsIntMax:
508 return "j";
509 case AsSizeT:
510 return "z";
511 case AsPtrDiff:
512 return "t";
David Majnemer3cba4952013-08-21 21:54:46 +0000513 case AsInt32:
514 return "I32";
515 case AsInt3264:
516 return "I";
517 case AsInt64:
518 return "I64";
Ted Kremenek02087932010-07-16 02:11:22 +0000519 case AsLongDouble:
520 return "L";
Hans Wennborg23926bd2011-12-15 10:25:47 +0000521 case AsAllocate:
522 return "a";
Hans Wennborg6073e312012-01-12 17:11:12 +0000523 case AsMAllocate:
524 return "m";
Hans Wennborg68f42b92014-09-04 21:39:46 +0000525 case AsWide:
526 return "w";
Ted Kremenek02087932010-07-16 02:11:22 +0000527 case None:
528 return "";
529 }
Craig Topper25542942014-05-20 04:30:07 +0000530 return nullptr;
Ted Kremenek02087932010-07-16 02:11:22 +0000531}
532
533//===----------------------------------------------------------------------===//
Hans Wennborga8b042d2011-12-09 11:11:07 +0000534// Methods on ConversionSpecifier.
535//===----------------------------------------------------------------------===//
536
537const char *ConversionSpecifier::toString() const {
538 switch (kind) {
539 case dArg: return "d";
Jordan Rose510260c2012-09-13 02:11:03 +0000540 case DArg: return "D";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000541 case iArg: return "i";
542 case oArg: return "o";
Jordan Rose510260c2012-09-13 02:11:03 +0000543 case OArg: return "O";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000544 case uArg: return "u";
Jordan Rose510260c2012-09-13 02:11:03 +0000545 case UArg: return "U";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000546 case xArg: return "x";
547 case XArg: return "X";
548 case fArg: return "f";
549 case FArg: return "F";
550 case eArg: return "e";
551 case EArg: return "E";
552 case gArg: return "g";
553 case GArg: return "G";
554 case aArg: return "a";
555 case AArg: return "A";
556 case cArg: return "c";
557 case sArg: return "s";
558 case pArg: return "p";
559 case nArg: return "n";
560 case PercentArg: return "%";
561 case ScanListArg: return "[";
Craig Topper25542942014-05-20 04:30:07 +0000562 case InvalidSpecifier: return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000563
David Majnemer3cba4952013-08-21 21:54:46 +0000564 // POSIX unicode extensions.
Hans Wennborga8b042d2011-12-09 11:11:07 +0000565 case CArg: return "C";
566 case SArg: return "S";
567
568 // Objective-C specific specifiers.
569 case ObjCObjArg: return "@";
570
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000571 // FreeBSD kernel specific specifiers.
572 case FreeBSDbArg: return "b";
573 case FreeBSDDArg: return "D";
574 case FreeBSDrArg: return "r";
575 case FreeBSDyArg: return "y";
576
Hans Wennborga8b042d2011-12-09 11:11:07 +0000577 // GlibC specific specifiers.
578 case PrintErrno: return "m";
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000579
580 // MS specific specifiers.
581 case ZArg: return "Z";
Hans Wennborga8b042d2011-12-09 11:11:07 +0000582 }
Craig Topper25542942014-05-20 04:30:07 +0000583 return nullptr;
Hans Wennborga8b042d2011-12-09 11:11:07 +0000584}
585
David Blaikie05785d12013-02-20 22:23:23 +0000586Optional<ConversionSpecifier>
Jordan Rose4c266aa2012-09-13 02:11:15 +0000587ConversionSpecifier::getStandardSpecifier() const {
588 ConversionSpecifier::Kind NewKind;
589
590 switch (getKind()) {
591 default:
David Blaikie7a30dc52013-02-21 01:47:18 +0000592 return None;
Jordan Rose4c266aa2012-09-13 02:11:15 +0000593 case DArg:
594 NewKind = dArg;
595 break;
596 case UArg:
597 NewKind = uArg;
598 break;
599 case OArg:
600 NewKind = oArg;
601 break;
602 }
603
604 ConversionSpecifier FixedCS(*this);
605 FixedCS.setKind(NewKind);
606 return FixedCS;
607}
608
Hans Wennborga8b042d2011-12-09 11:11:07 +0000609//===----------------------------------------------------------------------===//
Ted Kremenek02087932010-07-16 02:11:22 +0000610// Methods on OptionalAmount.
611//===----------------------------------------------------------------------===//
612
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000613void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek02087932010-07-16 02:11:22 +0000614 switch (hs) {
615 case Invalid:
616 case NotSpecified:
617 return;
618 case Arg:
619 if (UsesDotPrefix)
620 os << ".";
621 if (usesPositionalArg())
622 os << "*" << getPositionalArgIndex() << "$";
623 else
624 os << "*";
625 break;
626 case Constant:
627 if (UsesDotPrefix)
628 os << ".";
629 os << amt;
630 break;
631 }
632}
633
Jordan Rose92303592012-09-08 04:00:03 +0000634bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremenekea28f832010-07-20 20:04:42 +0000635 switch (LM.getKind()) {
636 case LengthModifier::None:
637 return true;
638
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000639 // Handle most integer flags
Ted Kremenekea28f832010-07-20 20:04:42 +0000640 case LengthModifier::AsShort:
Hans Wennborg1b231582014-09-04 21:39:52 +0000641 if (Target.getTriple().isOSMSVCRT()) {
642 switch (CS.getKind()) {
643 case ConversionSpecifier::cArg:
644 case ConversionSpecifier::CArg:
645 case ConversionSpecifier::sArg:
646 case ConversionSpecifier::SArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000647 case ConversionSpecifier::ZArg:
Hans Wennborg1b231582014-09-04 21:39:52 +0000648 return true;
649 default:
650 break;
651 }
652 }
653 // Fall through.
654 case LengthModifier::AsChar:
Ted Kremenekea28f832010-07-20 20:04:42 +0000655 case LengthModifier::AsLongLong:
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000656 case LengthModifier::AsQuad:
Ted Kremenekea28f832010-07-20 20:04:42 +0000657 case LengthModifier::AsIntMax:
658 case LengthModifier::AsSizeT:
659 case LengthModifier::AsPtrDiff:
660 switch (CS.getKind()) {
661 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000662 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000663 case ConversionSpecifier::iArg:
664 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000665 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000666 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000667 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000668 case ConversionSpecifier::xArg:
669 case ConversionSpecifier::XArg:
670 case ConversionSpecifier::nArg:
671 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000672 case ConversionSpecifier::FreeBSDrArg:
673 case ConversionSpecifier::FreeBSDyArg:
674 return Target.getTriple().isOSFreeBSD();
Ted Kremenekea28f832010-07-20 20:04:42 +0000675 default:
676 return false;
677 }
678
Hans Wennborg9bc9bcc2012-02-16 16:34:54 +0000679 // Handle 'l' flag
Nico Weberce069f12014-08-29 21:05:44 +0000680 case LengthModifier::AsLong: // or AsWideChar
Ted Kremenekea28f832010-07-20 20:04:42 +0000681 switch (CS.getKind()) {
682 case ConversionSpecifier::dArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000683 case ConversionSpecifier::DArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000684 case ConversionSpecifier::iArg:
685 case ConversionSpecifier::oArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000686 case ConversionSpecifier::OArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000687 case ConversionSpecifier::uArg:
Jordan Rose510260c2012-09-13 02:11:03 +0000688 case ConversionSpecifier::UArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000689 case ConversionSpecifier::xArg:
690 case ConversionSpecifier::XArg:
691 case ConversionSpecifier::aArg:
692 case ConversionSpecifier::AArg:
693 case ConversionSpecifier::fArg:
694 case ConversionSpecifier::FArg:
695 case ConversionSpecifier::eArg:
696 case ConversionSpecifier::EArg:
697 case ConversionSpecifier::gArg:
698 case ConversionSpecifier::GArg:
699 case ConversionSpecifier::nArg:
700 case ConversionSpecifier::cArg:
701 case ConversionSpecifier::sArg:
Ted Kremenek91398922012-01-20 22:11:52 +0000702 case ConversionSpecifier::ScanListArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000703 case ConversionSpecifier::ZArg:
Ted Kremenekea28f832010-07-20 20:04:42 +0000704 return true;
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000705 case ConversionSpecifier::FreeBSDrArg:
706 case ConversionSpecifier::FreeBSDyArg:
707 return Target.getTriple().isOSFreeBSD();
Ted Kremenekea28f832010-07-20 20:04:42 +0000708 default:
709 return false;
710 }
711
712 case LengthModifier::AsLongDouble:
713 switch (CS.getKind()) {
714 case ConversionSpecifier::aArg:
715 case ConversionSpecifier::AArg:
716 case ConversionSpecifier::fArg:
717 case ConversionSpecifier::FArg:
718 case ConversionSpecifier::eArg:
719 case ConversionSpecifier::EArg:
720 case ConversionSpecifier::gArg:
721 case ConversionSpecifier::GArg:
722 return true;
Jordan Rose92303592012-09-08 04:00:03 +0000723 // GNU libc extension.
Ted Kremenek6fa57272012-01-24 21:29:54 +0000724 case ConversionSpecifier::dArg:
725 case ConversionSpecifier::iArg:
726 case ConversionSpecifier::oArg:
727 case ConversionSpecifier::uArg:
728 case ConversionSpecifier::xArg:
729 case ConversionSpecifier::XArg:
Jordan Rose92303592012-09-08 04:00:03 +0000730 return !Target.getTriple().isOSDarwin() &&
731 !Target.getTriple().isOSWindows();
Ted Kremenekea28f832010-07-20 20:04:42 +0000732 default:
733 return false;
734 }
Hans Wennborg23926bd2011-12-15 10:25:47 +0000735
736 case LengthModifier::AsAllocate:
737 switch (CS.getKind()) {
738 case ConversionSpecifier::sArg:
739 case ConversionSpecifier::SArg:
Hans Wennborgfd950872012-01-12 15:07:16 +0000740 case ConversionSpecifier::ScanListArg:
Hans Wennborg23926bd2011-12-15 10:25:47 +0000741 return true;
742 default:
743 return false;
744 }
Hans Wennborg6073e312012-01-12 17:11:12 +0000745
746 case LengthModifier::AsMAllocate:
747 switch (CS.getKind()) {
748 case ConversionSpecifier::cArg:
749 case ConversionSpecifier::CArg:
750 case ConversionSpecifier::sArg:
751 case ConversionSpecifier::SArg:
752 case ConversionSpecifier::ScanListArg:
753 return true;
754 default:
755 return false;
756 }
David Majnemer3cba4952013-08-21 21:54:46 +0000757 case LengthModifier::AsInt32:
758 case LengthModifier::AsInt3264:
759 case LengthModifier::AsInt64:
760 switch (CS.getKind()) {
761 case ConversionSpecifier::dArg:
762 case ConversionSpecifier::iArg:
763 case ConversionSpecifier::oArg:
764 case ConversionSpecifier::uArg:
765 case ConversionSpecifier::xArg:
766 case ConversionSpecifier::XArg:
767 return Target.getTriple().isOSMSVCRT();
768 default:
769 return false;
770 }
Hans Wennborg68f42b92014-09-04 21:39:46 +0000771 case LengthModifier::AsWide:
772 switch (CS.getKind()) {
773 case ConversionSpecifier::cArg:
774 case ConversionSpecifier::CArg:
775 case ConversionSpecifier::sArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000776 case ConversionSpecifier::SArg:
777 case ConversionSpecifier::ZArg:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000778 return Target.getTriple().isOSMSVCRT();
779 default:
780 return false;
781 }
Ted Kremenekea28f832010-07-20 20:04:42 +0000782 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000783 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremenekea28f832010-07-20 20:04:42 +0000784}
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000785
786bool FormatSpecifier::hasStandardLengthModifier() const {
787 switch (LM.getKind()) {
788 case LengthModifier::None:
789 case LengthModifier::AsChar:
790 case LengthModifier::AsShort:
791 case LengthModifier::AsLong:
792 case LengthModifier::AsLongLong:
793 case LengthModifier::AsIntMax:
794 case LengthModifier::AsSizeT:
795 case LengthModifier::AsPtrDiff:
796 case LengthModifier::AsLongDouble:
797 return true;
798 case LengthModifier::AsAllocate:
799 case LengthModifier::AsMAllocate:
800 case LengthModifier::AsQuad:
David Majnemer3cba4952013-08-21 21:54:46 +0000801 case LengthModifier::AsInt32:
802 case LengthModifier::AsInt3264:
803 case LengthModifier::AsInt64:
Hans Wennborg68f42b92014-09-04 21:39:46 +0000804 case LengthModifier::AsWide:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000805 return false;
806 }
807 llvm_unreachable("Invalid LengthModifier Kind!");
808}
809
Nico Webereffdb192015-05-18 02:41:17 +0000810bool FormatSpecifier::hasStandardConversionSpecifier(
811 const LangOptions &LangOpt) const {
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000812 switch (CS.getKind()) {
813 case ConversionSpecifier::cArg:
814 case ConversionSpecifier::dArg:
815 case ConversionSpecifier::iArg:
816 case ConversionSpecifier::oArg:
817 case ConversionSpecifier::uArg:
818 case ConversionSpecifier::xArg:
819 case ConversionSpecifier::XArg:
820 case ConversionSpecifier::fArg:
821 case ConversionSpecifier::FArg:
822 case ConversionSpecifier::eArg:
823 case ConversionSpecifier::EArg:
824 case ConversionSpecifier::gArg:
825 case ConversionSpecifier::GArg:
826 case ConversionSpecifier::aArg:
827 case ConversionSpecifier::AArg:
828 case ConversionSpecifier::sArg:
829 case ConversionSpecifier::pArg:
830 case ConversionSpecifier::nArg:
831 case ConversionSpecifier::ObjCObjArg:
832 case ConversionSpecifier::ScanListArg:
833 case ConversionSpecifier::PercentArg:
834 return true;
835 case ConversionSpecifier::CArg:
836 case ConversionSpecifier::SArg:
837 return LangOpt.ObjC1 || LangOpt.ObjC2;
838 case ConversionSpecifier::InvalidSpecifier:
Dimitry Andric6b5ed342015-02-19 22:32:33 +0000839 case ConversionSpecifier::FreeBSDbArg:
840 case ConversionSpecifier::FreeBSDDArg:
841 case ConversionSpecifier::FreeBSDrArg:
842 case ConversionSpecifier::FreeBSDyArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000843 case ConversionSpecifier::PrintErrno:
Jordan Rose510260c2012-09-13 02:11:03 +0000844 case ConversionSpecifier::DArg:
845 case ConversionSpecifier::OArg:
846 case ConversionSpecifier::UArg:
Hans Wennborgc597b4c2014-09-07 03:03:51 +0000847 case ConversionSpecifier::ZArg:
Hans Wennborgc9dd9462012-02-22 10:17:01 +0000848 return false;
849 }
850 llvm_unreachable("Invalid ConversionSpecifier Kind!");
851}
852
853bool FormatSpecifier::hasStandardLengthConversionCombination() const {
854 if (LM.getKind() == LengthModifier::AsLongDouble) {
855 switch(CS.getKind()) {
856 case ConversionSpecifier::dArg:
857 case ConversionSpecifier::iArg:
858 case ConversionSpecifier::oArg:
859 case ConversionSpecifier::uArg:
860 case ConversionSpecifier::xArg:
861 case ConversionSpecifier::XArg:
862 return false;
863 default:
864 return true;
865 }
866 }
867 return true;
868}
Hans Wennborg08574d32012-07-27 19:17:46 +0000869
David Blaikie05785d12013-02-20 22:23:23 +0000870Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose2f9cc042012-09-08 04:00:12 +0000871 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
872 if (LM.getKind() == LengthModifier::AsLongDouble ||
873 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rose92303592012-09-08 04:00:03 +0000874 LengthModifier FixedLM(LM);
875 FixedLM.setKind(LengthModifier::AsLongLong);
876 return FixedLM;
877 }
Jordan Rose92303592012-09-08 04:00:03 +0000878 }
879
David Blaikie7a30dc52013-02-21 01:47:18 +0000880 return None;
Jordan Rose92303592012-09-08 04:00:03 +0000881}
882
Hans Wennborg08574d32012-07-27 19:17:46 +0000883bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
884 LengthModifier &LM) {
885 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
886 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
887
888 for (;;) {
889 const IdentifierInfo *Identifier = Typedef->getIdentifier();
890 if (Identifier->getName() == "size_t") {
891 LM.setKind(LengthModifier::AsSizeT);
892 return true;
893 } else if (Identifier->getName() == "ssize_t") {
894 // Not C99, but common in Unix.
895 LM.setKind(LengthModifier::AsSizeT);
896 return true;
897 } else if (Identifier->getName() == "intmax_t") {
898 LM.setKind(LengthModifier::AsIntMax);
899 return true;
900 } else if (Identifier->getName() == "uintmax_t") {
901 LM.setKind(LengthModifier::AsIntMax);
902 return true;
903 } else if (Identifier->getName() == "ptrdiff_t") {
904 LM.setKind(LengthModifier::AsPtrDiff);
905 return true;
906 }
907
908 QualType T = Typedef->getUnderlyingType();
909 if (!isa<TypedefType>(T))
910 break;
911
912 Typedef = cast<TypedefType>(T)->getDecl();
913 }
914 return false;
915}