blob: 4b18d8ea91179907ee520836b6e66b9a8bf717e5 [file] [log] [blame]
Ted Kremenek826a3452010-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 Wennborgd02deeb2011-12-15 10:25:47 +000016#include "clang/Basic/LangOptions.h"
Jordan Rosebbb6bb42012-09-08 04:00:03 +000017#include "clang/Basic/TargetInfo.h"
Ted Kremenek826a3452010-07-16 02:11:22 +000018
Hans Wennborgf3749f42012-08-07 08:11:26 +000019using clang::analyze_format_string::ArgType;
Ted Kremenek826a3452010-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 Kremeneka412a492010-07-20 20:04:42 +000025using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek826a3452010-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 Wennborgf8562642012-03-09 10:10:54 +0000160 // Warn that positional arguments are non-standard.
161 H.HandlePosition(Start, I - Start);
162
Ted Kremenek826a3452010-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 Wennborgd02deeb2011-12-15 10:25:47 +0000183 const char *E,
184 const LangOptions &LO,
185 bool IsScanf) {
Ted Kremenek826a3452010-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 Kremenek6ca4a9a2011-10-25 04:20:41 +0000193 lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
194 : LengthModifier::AsShort;
Ted Kremenek826a3452010-07-16 02:11:22 +0000195 break;
196 case 'l':
197 ++I;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000198 lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
199 : LengthModifier::AsLong;
Ted Kremenek826a3452010-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 Wennborg32addd52012-02-16 16:34:54 +0000205 case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000206 case 'a':
Richard Smith80ad52f2013-01-02 11:42:31 +0000207 if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
Hans Wennborgd02deeb2011-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 Wennborg37969b72012-01-12 17:11:12 +0000219 case 'm':
220 if (IsScanf) {
221 lmKind = LengthModifier::AsMAllocate;
222 ++I;
223 break;
224 }
225 return false;
Ted Kremenek826a3452010-07-16 02:11:22 +0000226 }
227 LengthModifier lm(lmPosition, lmKind);
228 FS.setLengthModifier(lm);
229 return true;
230}
231
232//===----------------------------------------------------------------------===//
Hans Wennborgf3749f42012-08-07 08:11:26 +0000233// Methods on ArgType.
Ted Kremenek826a3452010-07-16 02:11:22 +0000234//===----------------------------------------------------------------------===//
235
Hans Wennborgf3749f42012-08-07 08:11:26 +0000236bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
Hans Wennborg58e1e542012-08-07 08:59:46 +0000237 if (Ptr) {
238 // It has to be a pointer.
239 const PointerType *PT = argTy->getAs<PointerType>();
240 if (!PT)
241 return false;
242
243 // We cannot write through a const qualified pointer.
244 if (PT->getPointeeType().isConstQualified())
245 return false;
246
247 argTy = PT->getPointeeType();
248 }
249
Ted Kremenek826a3452010-07-16 02:11:22 +0000250 switch (K) {
251 case InvalidTy:
Hans Wennborgf3749f42012-08-07 08:11:26 +0000252 llvm_unreachable("ArgType must be valid");
Ted Kremenek826a3452010-07-16 02:11:22 +0000253
254 case UnknownTy:
255 return true;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000256
257 case AnyCharTy: {
Jordan Roseee0259d2012-06-04 22:48:57 +0000258 if (const EnumType *ETy = argTy->getAs<EnumType>())
259 argTy = ETy->getDecl()->getIntegerType();
260
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000261 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
262 switch (BT->getKind()) {
263 default:
264 break;
265 case BuiltinType::Char_S:
266 case BuiltinType::SChar:
267 case BuiltinType::UChar:
268 case BuiltinType::Char_U:
269 return true;
270 }
271 return false;
272 }
273
Ted Kremenek826a3452010-07-16 02:11:22 +0000274 case SpecificTy: {
Jordan Roseee0259d2012-06-04 22:48:57 +0000275 if (const EnumType *ETy = argTy->getAs<EnumType>())
276 argTy = ETy->getDecl()->getIntegerType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000277 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Jordan Roseee0259d2012-06-04 22:48:57 +0000278
Nick Lewycky687b5df2011-12-02 23:21:43 +0000279 if (T == argTy)
Ted Kremenek826a3452010-07-16 02:11:22 +0000280 return true;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000281 // Check for "compatible types".
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000282 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek826a3452010-07-16 02:11:22 +0000283 switch (BT->getKind()) {
284 default:
285 break;
286 case BuiltinType::Char_S:
287 case BuiltinType::SChar:
Ted Kremenekdc00d812011-07-13 17:35:14 +0000288 case BuiltinType::Char_U:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000289 case BuiltinType::UChar:
Hans Wennborg101d4e02012-05-08 17:21:31 +0000290 return T == C.UnsignedCharTy || T == C.SignedCharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000291 case BuiltinType::Short:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000292 return T == C.UnsignedShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000293 case BuiltinType::UShort:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000294 return T == C.ShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000295 case BuiltinType::Int:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000296 return T == C.UnsignedIntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000297 case BuiltinType::UInt:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000298 return T == C.IntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000299 case BuiltinType::Long:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000300 return T == C.UnsignedLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000301 case BuiltinType::ULong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000302 return T == C.LongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000303 case BuiltinType::LongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000304 return T == C.UnsignedLongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000305 case BuiltinType::ULongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000306 return T == C.LongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000307 }
308 return false;
309 }
310
311 case CStrTy: {
312 const PointerType *PT = argTy->getAs<PointerType>();
313 if (!PT)
314 return false;
315 QualType pointeeTy = PT->getPointeeType();
316 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
317 switch (BT->getKind()) {
318 case BuiltinType::Void:
319 case BuiltinType::Char_U:
320 case BuiltinType::UChar:
321 case BuiltinType::Char_S:
322 case BuiltinType::SChar:
323 return true;
324 default:
325 break;
326 }
327
328 return false;
329 }
330
331 case WCStrTy: {
332 const PointerType *PT = argTy->getAs<PointerType>();
333 if (!PT)
334 return false;
335 QualType pointeeTy =
336 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
337 return pointeeTy == C.getWCharType();
338 }
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000339
340 case WIntTy: {
James Molloy392da482012-05-04 10:55:22 +0000341
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000342 QualType PromoArg =
343 argTy->isPromotableIntegerType()
344 ? C.getPromotedIntegerType(argTy) : argTy;
345
James Molloy392da482012-05-04 10:55:22 +0000346 QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000347 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
348
James Molloy392da482012-05-04 10:55:22 +0000349 // If the promoted argument is the corresponding signed type of the
350 // wint_t type, then it should match.
351 if (PromoArg->hasSignedIntegerRepresentation() &&
352 C.getCorrespondingUnsignedType(PromoArg) == WInt)
353 return true;
354
355 return WInt == PromoArg;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000356 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000357
358 case CPointerTy:
Anders Carlsson62425992010-11-06 14:58:53 +0000359 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
Ted Kremenekafcd1952012-03-15 21:22:27 +0000360 argTy->isBlockPointerType() || argTy->isNullPtrType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000361
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000362 case ObjCPointerTy: {
363 if (argTy->getAs<ObjCObjectPointerType>() ||
364 argTy->getAs<BlockPointerType>())
365 return true;
366
367 // Handle implicit toll-free bridging.
368 if (const PointerType *PT = argTy->getAs<PointerType>()) {
369 // Things such as CFTypeRef are really just opaque pointers
370 // to C structs representing CF types that can often be bridged
371 // to Objective-C objects. Since the compiler doesn't know which
372 // structs can be toll-free bridged, we just accept them all.
373 QualType pointee = PT->getPointeeType();
374 if (pointee->getAsStructureType() || pointee->isVoidType())
375 return true;
376 }
377 return false;
378 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000379 }
380
Hans Wennborgf3749f42012-08-07 08:11:26 +0000381 llvm_unreachable("Invalid ArgType Kind!");
Ted Kremenek826a3452010-07-16 02:11:22 +0000382}
383
Hans Wennborgf3749f42012-08-07 08:11:26 +0000384QualType ArgType::getRepresentativeType(ASTContext &C) const {
Hans Wennborg58e1e542012-08-07 08:59:46 +0000385 QualType Res;
Ted Kremenek826a3452010-07-16 02:11:22 +0000386 switch (K) {
387 case InvalidTy:
Hans Wennborgf3749f42012-08-07 08:11:26 +0000388 llvm_unreachable("No representative type for Invalid ArgType");
Ted Kremenek826a3452010-07-16 02:11:22 +0000389 case UnknownTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000390 llvm_unreachable("No representative type for Unknown ArgType");
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000391 case AnyCharTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000392 Res = C.CharTy;
393 break;
Ted Kremenek826a3452010-07-16 02:11:22 +0000394 case SpecificTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000395 Res = T;
396 break;
Ted Kremenek826a3452010-07-16 02:11:22 +0000397 case CStrTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000398 Res = C.getPointerType(C.CharTy);
399 break;
Ted Kremenek826a3452010-07-16 02:11:22 +0000400 case WCStrTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000401 Res = C.getPointerType(C.getWCharType());
402 break;
Ted Kremenek826a3452010-07-16 02:11:22 +0000403 case ObjCPointerTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000404 Res = C.ObjCBuiltinIdTy;
405 break;
Ted Kremenek826a3452010-07-16 02:11:22 +0000406 case CPointerTy:
Hans Wennborg58e1e542012-08-07 08:59:46 +0000407 Res = C.VoidPtrTy;
408 break;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000409 case WIntTy: {
Hans Wennborg58e1e542012-08-07 08:59:46 +0000410 Res = C.getWIntType();
411 break;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000412 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000413 }
414
Hans Wennborg58e1e542012-08-07 08:59:46 +0000415 if (Ptr)
416 Res = C.getPointerType(Res);
417 return Res;
Ted Kremenek826a3452010-07-16 02:11:22 +0000418}
419
Hans Wennborgf3749f42012-08-07 08:11:26 +0000420std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000421 std::string S = getRepresentativeType(C).getAsString();
Hans Wennborg58e1e542012-08-07 08:59:46 +0000422
423 std::string Alias;
424 if (Name) {
425 // Use a specific name for this type, e.g. "size_t".
426 Alias = Name;
427 if (Ptr) {
428 // If ArgType is actually a pointer to T, append an asterisk.
429 Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
430 }
431 // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
432 if (S == Alias)
433 Alias.clear();
434 }
435
436 if (!Alias.empty())
437 return std::string("'") + Alias + "' (aka '" + S + "')";
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000438 return std::string("'") + S + "'";
Hans Wennborga792aff2011-12-07 10:33:11 +0000439}
440
441
Ted Kremenek826a3452010-07-16 02:11:22 +0000442//===----------------------------------------------------------------------===//
443// Methods on OptionalAmount.
444//===----------------------------------------------------------------------===//
445
Hans Wennborgf3749f42012-08-07 08:11:26 +0000446ArgType
Ted Kremenek826a3452010-07-16 02:11:22 +0000447analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
448 return Ctx.IntTy;
449}
450
451//===----------------------------------------------------------------------===//
452// Methods on LengthModifier.
453//===----------------------------------------------------------------------===//
454
455const char *
456analyze_format_string::LengthModifier::toString() const {
457 switch (kind) {
458 case AsChar:
459 return "hh";
460 case AsShort:
461 return "h";
462 case AsLong: // or AsWideChar
463 return "l";
464 case AsLongLong:
465 return "ll";
Hans Wennborg32addd52012-02-16 16:34:54 +0000466 case AsQuad:
467 return "q";
Ted Kremenek826a3452010-07-16 02:11:22 +0000468 case AsIntMax:
469 return "j";
470 case AsSizeT:
471 return "z";
472 case AsPtrDiff:
473 return "t";
474 case AsLongDouble:
475 return "L";
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000476 case AsAllocate:
477 return "a";
Hans Wennborg37969b72012-01-12 17:11:12 +0000478 case AsMAllocate:
479 return "m";
Ted Kremenek826a3452010-07-16 02:11:22 +0000480 case None:
481 return "";
482 }
483 return NULL;
484}
485
486//===----------------------------------------------------------------------===//
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000487// Methods on ConversionSpecifier.
488//===----------------------------------------------------------------------===//
489
490const char *ConversionSpecifier::toString() const {
491 switch (kind) {
492 case dArg: return "d";
Jordan Rose275b6f52012-09-13 02:11:03 +0000493 case DArg: return "D";
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000494 case iArg: return "i";
495 case oArg: return "o";
Jordan Rose275b6f52012-09-13 02:11:03 +0000496 case OArg: return "O";
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000497 case uArg: return "u";
Jordan Rose275b6f52012-09-13 02:11:03 +0000498 case UArg: return "U";
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000499 case xArg: return "x";
500 case XArg: return "X";
501 case fArg: return "f";
502 case FArg: return "F";
503 case eArg: return "e";
504 case EArg: return "E";
505 case gArg: return "g";
506 case GArg: return "G";
507 case aArg: return "a";
508 case AArg: return "A";
509 case cArg: return "c";
510 case sArg: return "s";
511 case pArg: return "p";
512 case nArg: return "n";
513 case PercentArg: return "%";
514 case ScanListArg: return "[";
515 case InvalidSpecifier: return NULL;
516
517 // MacOS X unicode extensions.
518 case CArg: return "C";
519 case SArg: return "S";
520
521 // Objective-C specific specifiers.
522 case ObjCObjArg: return "@";
523
524 // GlibC specific specifiers.
525 case PrintErrno: return "m";
526 }
527 return NULL;
528}
529
Jordan Rose670941c2012-09-13 02:11:15 +0000530llvm::Optional<ConversionSpecifier>
531ConversionSpecifier::getStandardSpecifier() const {
532 ConversionSpecifier::Kind NewKind;
533
534 switch (getKind()) {
535 default:
536 return llvm::Optional<ConversionSpecifier>();
537 case DArg:
538 NewKind = dArg;
539 break;
540 case UArg:
541 NewKind = uArg;
542 break;
543 case OArg:
544 NewKind = oArg;
545 break;
546 }
547
548 ConversionSpecifier FixedCS(*this);
549 FixedCS.setKind(NewKind);
550 return FixedCS;
551}
552
Hans Wennborgb8ec3e32011-12-09 11:11:07 +0000553//===----------------------------------------------------------------------===//
Ted Kremenek826a3452010-07-16 02:11:22 +0000554// Methods on OptionalAmount.
555//===----------------------------------------------------------------------===//
556
Chris Lattner5f9e2722011-07-23 10:55:15 +0000557void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000558 switch (hs) {
559 case Invalid:
560 case NotSpecified:
561 return;
562 case Arg:
563 if (UsesDotPrefix)
564 os << ".";
565 if (usesPositionalArg())
566 os << "*" << getPositionalArgIndex() << "$";
567 else
568 os << "*";
569 break;
570 case Constant:
571 if (UsesDotPrefix)
572 os << ".";
573 os << amt;
574 break;
575 }
576}
577
Jordan Rosebbb6bb42012-09-08 04:00:03 +0000578bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
Ted Kremeneka412a492010-07-20 20:04:42 +0000579 switch (LM.getKind()) {
580 case LengthModifier::None:
581 return true;
582
Hans Wennborg32addd52012-02-16 16:34:54 +0000583 // Handle most integer flags
Ted Kremeneka412a492010-07-20 20:04:42 +0000584 case LengthModifier::AsChar:
585 case LengthModifier::AsShort:
586 case LengthModifier::AsLongLong:
Hans Wennborg32addd52012-02-16 16:34:54 +0000587 case LengthModifier::AsQuad:
Ted Kremeneka412a492010-07-20 20:04:42 +0000588 case LengthModifier::AsIntMax:
589 case LengthModifier::AsSizeT:
590 case LengthModifier::AsPtrDiff:
591 switch (CS.getKind()) {
592 case ConversionSpecifier::dArg:
Jordan Rose275b6f52012-09-13 02:11:03 +0000593 case ConversionSpecifier::DArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000594 case ConversionSpecifier::iArg:
595 case ConversionSpecifier::oArg:
Jordan Rose275b6f52012-09-13 02:11:03 +0000596 case ConversionSpecifier::OArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000597 case ConversionSpecifier::uArg:
Jordan Rose275b6f52012-09-13 02:11:03 +0000598 case ConversionSpecifier::UArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000599 case ConversionSpecifier::xArg:
600 case ConversionSpecifier::XArg:
601 case ConversionSpecifier::nArg:
602 return true;
603 default:
604 return false;
605 }
606
Hans Wennborg32addd52012-02-16 16:34:54 +0000607 // Handle 'l' flag
Ted Kremeneka412a492010-07-20 20:04:42 +0000608 case LengthModifier::AsLong:
609 switch (CS.getKind()) {
610 case ConversionSpecifier::dArg:
Jordan Rose275b6f52012-09-13 02:11:03 +0000611 case ConversionSpecifier::DArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000612 case ConversionSpecifier::iArg:
613 case ConversionSpecifier::oArg:
Jordan Rose275b6f52012-09-13 02:11:03 +0000614 case ConversionSpecifier::OArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000615 case ConversionSpecifier::uArg:
Jordan Rose275b6f52012-09-13 02:11:03 +0000616 case ConversionSpecifier::UArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000617 case ConversionSpecifier::xArg:
618 case ConversionSpecifier::XArg:
619 case ConversionSpecifier::aArg:
620 case ConversionSpecifier::AArg:
621 case ConversionSpecifier::fArg:
622 case ConversionSpecifier::FArg:
623 case ConversionSpecifier::eArg:
624 case ConversionSpecifier::EArg:
625 case ConversionSpecifier::gArg:
626 case ConversionSpecifier::GArg:
627 case ConversionSpecifier::nArg:
628 case ConversionSpecifier::cArg:
629 case ConversionSpecifier::sArg:
Ted Kremenekef1440b2012-01-20 22:11:52 +0000630 case ConversionSpecifier::ScanListArg:
Ted Kremeneka412a492010-07-20 20:04:42 +0000631 return true;
632 default:
633 return false;
634 }
635
636 case LengthModifier::AsLongDouble:
637 switch (CS.getKind()) {
638 case ConversionSpecifier::aArg:
639 case ConversionSpecifier::AArg:
640 case ConversionSpecifier::fArg:
641 case ConversionSpecifier::FArg:
642 case ConversionSpecifier::eArg:
643 case ConversionSpecifier::EArg:
644 case ConversionSpecifier::gArg:
645 case ConversionSpecifier::GArg:
646 return true;
Jordan Rosebbb6bb42012-09-08 04:00:03 +0000647 // GNU libc extension.
Ted Kremenek9d24c2c2012-01-24 21:29:54 +0000648 case ConversionSpecifier::dArg:
649 case ConversionSpecifier::iArg:
650 case ConversionSpecifier::oArg:
651 case ConversionSpecifier::uArg:
652 case ConversionSpecifier::xArg:
653 case ConversionSpecifier::XArg:
Jordan Rosebbb6bb42012-09-08 04:00:03 +0000654 return !Target.getTriple().isOSDarwin() &&
655 !Target.getTriple().isOSWindows();
Ted Kremeneka412a492010-07-20 20:04:42 +0000656 default:
657 return false;
658 }
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000659
660 case LengthModifier::AsAllocate:
661 switch (CS.getKind()) {
662 case ConversionSpecifier::sArg:
663 case ConversionSpecifier::SArg:
Hans Wennborg28058d12012-01-12 15:07:16 +0000664 case ConversionSpecifier::ScanListArg:
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000665 return true;
666 default:
667 return false;
668 }
Hans Wennborg37969b72012-01-12 17:11:12 +0000669
670 case LengthModifier::AsMAllocate:
671 switch (CS.getKind()) {
672 case ConversionSpecifier::cArg:
673 case ConversionSpecifier::CArg:
674 case ConversionSpecifier::sArg:
675 case ConversionSpecifier::SArg:
676 case ConversionSpecifier::ScanListArg:
677 return true;
678 default:
679 return false;
680 }
Ted Kremeneka412a492010-07-20 20:04:42 +0000681 }
David Blaikie30263482012-01-20 21:50:17 +0000682 llvm_unreachable("Invalid LengthModifier Kind!");
Ted Kremeneka412a492010-07-20 20:04:42 +0000683}
Hans Wennborg76517422012-02-22 10:17:01 +0000684
685bool FormatSpecifier::hasStandardLengthModifier() const {
686 switch (LM.getKind()) {
687 case LengthModifier::None:
688 case LengthModifier::AsChar:
689 case LengthModifier::AsShort:
690 case LengthModifier::AsLong:
691 case LengthModifier::AsLongLong:
692 case LengthModifier::AsIntMax:
693 case LengthModifier::AsSizeT:
694 case LengthModifier::AsPtrDiff:
695 case LengthModifier::AsLongDouble:
696 return true;
697 case LengthModifier::AsAllocate:
698 case LengthModifier::AsMAllocate:
699 case LengthModifier::AsQuad:
700 return false;
701 }
702 llvm_unreachable("Invalid LengthModifier Kind!");
703}
704
705bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
706 switch (CS.getKind()) {
707 case ConversionSpecifier::cArg:
708 case ConversionSpecifier::dArg:
709 case ConversionSpecifier::iArg:
710 case ConversionSpecifier::oArg:
711 case ConversionSpecifier::uArg:
712 case ConversionSpecifier::xArg:
713 case ConversionSpecifier::XArg:
714 case ConversionSpecifier::fArg:
715 case ConversionSpecifier::FArg:
716 case ConversionSpecifier::eArg:
717 case ConversionSpecifier::EArg:
718 case ConversionSpecifier::gArg:
719 case ConversionSpecifier::GArg:
720 case ConversionSpecifier::aArg:
721 case ConversionSpecifier::AArg:
722 case ConversionSpecifier::sArg:
723 case ConversionSpecifier::pArg:
724 case ConversionSpecifier::nArg:
725 case ConversionSpecifier::ObjCObjArg:
726 case ConversionSpecifier::ScanListArg:
727 case ConversionSpecifier::PercentArg:
728 return true;
729 case ConversionSpecifier::CArg:
730 case ConversionSpecifier::SArg:
731 return LangOpt.ObjC1 || LangOpt.ObjC2;
732 case ConversionSpecifier::InvalidSpecifier:
733 case ConversionSpecifier::PrintErrno:
Jordan Rose275b6f52012-09-13 02:11:03 +0000734 case ConversionSpecifier::DArg:
735 case ConversionSpecifier::OArg:
736 case ConversionSpecifier::UArg:
Hans Wennborg76517422012-02-22 10:17:01 +0000737 return false;
738 }
739 llvm_unreachable("Invalid ConversionSpecifier Kind!");
740}
741
742bool FormatSpecifier::hasStandardLengthConversionCombination() const {
743 if (LM.getKind() == LengthModifier::AsLongDouble) {
744 switch(CS.getKind()) {
745 case ConversionSpecifier::dArg:
746 case ConversionSpecifier::iArg:
747 case ConversionSpecifier::oArg:
748 case ConversionSpecifier::uArg:
749 case ConversionSpecifier::xArg:
750 case ConversionSpecifier::XArg:
751 return false;
752 default:
753 return true;
754 }
755 }
756 return true;
757}
Hans Wennborg46847782012-07-27 19:17:46 +0000758
Jordan Rosebbb6bb42012-09-08 04:00:03 +0000759llvm::Optional<LengthModifier>
760FormatSpecifier::getCorrectedLengthModifier() const {
Jordan Rose8be066e2012-09-08 04:00:12 +0000761 if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
762 if (LM.getKind() == LengthModifier::AsLongDouble ||
763 LM.getKind() == LengthModifier::AsQuad) {
Jordan Rosebbb6bb42012-09-08 04:00:03 +0000764 LengthModifier FixedLM(LM);
765 FixedLM.setKind(LengthModifier::AsLongLong);
766 return FixedLM;
767 }
Jordan Rosebbb6bb42012-09-08 04:00:03 +0000768 }
769
770 return llvm::Optional<LengthModifier>();
771}
772
Hans Wennborg46847782012-07-27 19:17:46 +0000773bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
774 LengthModifier &LM) {
775 assert(isa<TypedefType>(QT) && "Expected a TypedefType");
776 const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
777
778 for (;;) {
779 const IdentifierInfo *Identifier = Typedef->getIdentifier();
780 if (Identifier->getName() == "size_t") {
781 LM.setKind(LengthModifier::AsSizeT);
782 return true;
783 } else if (Identifier->getName() == "ssize_t") {
784 // Not C99, but common in Unix.
785 LM.setKind(LengthModifier::AsSizeT);
786 return true;
787 } else if (Identifier->getName() == "intmax_t") {
788 LM.setKind(LengthModifier::AsIntMax);
789 return true;
790 } else if (Identifier->getName() == "uintmax_t") {
791 LM.setKind(LengthModifier::AsIntMax);
792 return true;
793 } else if (Identifier->getName() == "ptrdiff_t") {
794 LM.setKind(LengthModifier::AsPtrDiff);
795 return true;
796 }
797
798 QualType T = Typedef->getUnderlyingType();
799 if (!isa<TypedefType>(T))
800 break;
801
802 Typedef = cast<TypedefType>(T)->getDecl();
803 }
804 return false;
805}