blob: 0853164df7508ad9c90eca5c8c67ab56b4cf7316 [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"
16
17using clang::analyze_format_string::ArgTypeResult;
18using clang::analyze_format_string::FormatStringHandler;
19using clang::analyze_format_string::FormatSpecifier;
20using clang::analyze_format_string::LengthModifier;
21using clang::analyze_format_string::OptionalAmount;
22using clang::analyze_format_string::PositionContext;
Ted Kremeneka412a492010-07-20 20:04:42 +000023using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek826a3452010-07-16 02:11:22 +000024using namespace clang;
25
26// Key function to FormatStringHandler.
27FormatStringHandler::~FormatStringHandler() {}
28
29//===----------------------------------------------------------------------===//
30// Functions for parsing format strings components in both printf and
31// scanf format strings.
32//===----------------------------------------------------------------------===//
33
34OptionalAmount
35clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
36 const char *I = Beg;
37 UpdateOnReturn <const char*> UpdateBeg(Beg, I);
38
39 unsigned accumulator = 0;
40 bool hasDigits = false;
41
42 for ( ; I != E; ++I) {
43 char c = *I;
44 if (c >= '0' && c <= '9') {
45 hasDigits = true;
46 accumulator = (accumulator * 10) + (c - '0');
47 continue;
48 }
49
50 if (hasDigits)
51 return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
52 false);
53
54 break;
55 }
56
57 return OptionalAmount();
58}
59
60OptionalAmount
61clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
62 const char *E,
63 unsigned &argIndex) {
64 if (*Beg == '*') {
65 ++Beg;
66 return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
67 }
68
69 return ParseAmount(Beg, E);
70}
71
72OptionalAmount
73clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
74 const char *Start,
75 const char *&Beg,
76 const char *E,
77 PositionContext p) {
78 if (*Beg == '*') {
79 const char *I = Beg + 1;
80 const OptionalAmount &Amt = ParseAmount(I, E);
81
82 if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
83 H.HandleInvalidPosition(Beg, I - Beg, p);
84 return OptionalAmount(false);
85 }
86
87 if (I == E) {
88 // No more characters left?
89 H.HandleIncompleteSpecifier(Start, E - Start);
90 return OptionalAmount(false);
91 }
92
93 assert(Amt.getHowSpecified() == OptionalAmount::Constant);
94
95 if (*I == '$') {
96 // Handle positional arguments
97
98 // Special case: '*0$', since this is an easy mistake.
99 if (Amt.getConstantAmount() == 0) {
100 H.HandleZeroPosition(Beg, I - Beg + 1);
101 return OptionalAmount(false);
102 }
103
104 const char *Tmp = Beg;
105 Beg = ++I;
106
107 return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
108 Tmp, 0, true);
109 }
110
111 H.HandleInvalidPosition(Beg, I - Beg, p);
112 return OptionalAmount(false);
113 }
114
115 return ParseAmount(Beg, E);
116}
117
118
119bool
120clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
121 FormatSpecifier &CS,
122 const char *Start,
123 const char *&Beg, const char *E,
124 unsigned *argIndex) {
125 // FIXME: Support negative field widths.
126 if (argIndex) {
127 CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
128 }
129 else {
130 const OptionalAmount Amt =
131 ParsePositionAmount(H, Start, Beg, E,
132 analyze_format_string::FieldWidthPos);
133
134 if (Amt.isInvalid())
135 return true;
136 CS.setFieldWidth(Amt);
137 }
138 return false;
139}
140
141bool
142clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
143 FormatSpecifier &FS,
144 const char *Start,
145 const char *&Beg,
146 const char *E) {
147 const char *I = Beg;
148
149 const OptionalAmount &Amt = ParseAmount(I, E);
150
151 if (I == E) {
152 // No more characters left?
153 H.HandleIncompleteSpecifier(Start, E - Start);
154 return true;
155 }
156
157 if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
158 // Special case: '%0$', since this is an easy mistake.
159 if (Amt.getConstantAmount() == 0) {
160 H.HandleZeroPosition(Start, I - Start);
161 return true;
162 }
163
164 FS.setArgIndex(Amt.getConstantAmount() - 1);
165 FS.setUsesPositionalArg();
166 // Update the caller's pointer if we decided to consume
167 // these characters.
168 Beg = I;
169 return false;
170 }
171
172 return false;
173}
174
175bool
176clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
177 const char *&I,
178 const char *E) {
179 LengthModifier::Kind lmKind = LengthModifier::None;
180 const char *lmPosition = I;
181 switch (*I) {
182 default:
183 return false;
184 case 'h':
185 ++I;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000186 lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
187 : LengthModifier::AsShort;
Ted Kremenek826a3452010-07-16 02:11:22 +0000188 break;
189 case 'l':
190 ++I;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000191 lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
192 : LengthModifier::AsLong;
Ted Kremenek826a3452010-07-16 02:11:22 +0000193 break;
194 case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
195 case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
196 case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
197 case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
198 case 'q': lmKind = LengthModifier::AsLongLong; ++I; break;
199 }
200 LengthModifier lm(lmPosition, lmKind);
201 FS.setLengthModifier(lm);
202 return true;
203}
204
205//===----------------------------------------------------------------------===//
206// Methods on ArgTypeResult.
207//===----------------------------------------------------------------------===//
208
209bool ArgTypeResult::matchesType(ASTContext &C, QualType argTy) const {
210 switch (K) {
211 case InvalidTy:
David Blaikieb219cfc2011-09-23 05:06:16 +0000212 llvm_unreachable("ArgTypeResult must be valid");
Ted Kremenek826a3452010-07-16 02:11:22 +0000213
214 case UnknownTy:
215 return true;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000216
217 case AnyCharTy: {
218 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
219 switch (BT->getKind()) {
220 default:
221 break;
222 case BuiltinType::Char_S:
223 case BuiltinType::SChar:
224 case BuiltinType::UChar:
225 case BuiltinType::Char_U:
226 return true;
227 }
228 return false;
229 }
230
Hans Wennborga792aff2011-12-07 10:33:11 +0000231 case TypedefTy:
Ted Kremenek826a3452010-07-16 02:11:22 +0000232 case SpecificTy: {
233 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Nick Lewycky687b5df2011-12-02 23:21:43 +0000234 if (T == argTy)
Ted Kremenek826a3452010-07-16 02:11:22 +0000235 return true;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000236 // Check for "compatible types".
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000237 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek826a3452010-07-16 02:11:22 +0000238 switch (BT->getKind()) {
239 default:
240 break;
241 case BuiltinType::Char_S:
242 case BuiltinType::SChar:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000243 return T == C.UnsignedCharTy;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000244 case BuiltinType::Char_U:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000245 case BuiltinType::UChar:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000246 return T == C.SignedCharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000247 case BuiltinType::Short:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000248 return T == C.UnsignedShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000249 case BuiltinType::UShort:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000250 return T == C.ShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000251 case BuiltinType::Int:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000252 return T == C.UnsignedIntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000253 case BuiltinType::UInt:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000254 return T == C.IntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000255 case BuiltinType::Long:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000256 return T == C.UnsignedLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000257 case BuiltinType::ULong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000258 return T == C.LongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000259 case BuiltinType::LongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000260 return T == C.UnsignedLongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000261 case BuiltinType::ULongLong:
Nick Lewycky687b5df2011-12-02 23:21:43 +0000262 return T == C.LongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000263 }
264 return false;
265 }
266
267 case CStrTy: {
268 const PointerType *PT = argTy->getAs<PointerType>();
269 if (!PT)
270 return false;
271 QualType pointeeTy = PT->getPointeeType();
272 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
273 switch (BT->getKind()) {
274 case BuiltinType::Void:
275 case BuiltinType::Char_U:
276 case BuiltinType::UChar:
277 case BuiltinType::Char_S:
278 case BuiltinType::SChar:
279 return true;
280 default:
281 break;
282 }
283
284 return false;
285 }
286
287 case WCStrTy: {
288 const PointerType *PT = argTy->getAs<PointerType>();
289 if (!PT)
290 return false;
291 QualType pointeeTy =
292 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
293 return pointeeTy == C.getWCharType();
294 }
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000295
296 case WIntTy: {
297 // Instead of doing a lookup for the definition of 'wint_t' (which
298 // is defined by the system headers) instead see if wchar_t and
299 // the argument type promote to the same type.
300 QualType PromoWChar =
301 C.getWCharType()->isPromotableIntegerType()
302 ? C.getPromotedIntegerType(C.getWCharType()) : C.getWCharType();
303 QualType PromoArg =
304 argTy->isPromotableIntegerType()
305 ? C.getPromotedIntegerType(argTy) : argTy;
306
307 PromoWChar = C.getCanonicalType(PromoWChar).getUnqualifiedType();
308 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
309
310 return PromoWChar == PromoArg;
311 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000312
313 case CPointerTy:
Anders Carlsson62425992010-11-06 14:58:53 +0000314 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
315 argTy->isNullPtrType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000316
317 case ObjCPointerTy:
Daniel Dunbard6a4d182011-06-28 23:33:55 +0000318 return argTy->getAs<ObjCObjectPointerType>() != NULL;
Ted Kremenek826a3452010-07-16 02:11:22 +0000319 }
320
321 // FIXME: Should be unreachable, but Clang is currently emitting
322 // a warning.
323 return false;
324}
325
326QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
327 switch (K) {
328 case InvalidTy:
David Blaikieb219cfc2011-09-23 05:06:16 +0000329 llvm_unreachable("No representative type for Invalid ArgTypeResult");
Ted Kremenek826a3452010-07-16 02:11:22 +0000330 case UnknownTy:
331 return QualType();
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000332 case AnyCharTy:
333 return C.CharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000334 case SpecificTy:
Hans Wennborga792aff2011-12-07 10:33:11 +0000335 case TypedefTy:
Ted Kremenek826a3452010-07-16 02:11:22 +0000336 return T;
337 case CStrTy:
338 return C.getPointerType(C.CharTy);
339 case WCStrTy:
340 return C.getPointerType(C.getWCharType());
341 case ObjCPointerTy:
342 return C.ObjCBuiltinIdTy;
343 case CPointerTy:
344 return C.VoidPtrTy;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000345 case WIntTy: {
346 QualType WC = C.getWCharType();
347 return WC->isPromotableIntegerType() ? C.getPromotedIntegerType(WC) : WC;
348 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000349 }
350
351 // FIXME: Should be unreachable, but Clang is currently emitting
352 // a warning.
353 return QualType();
354}
355
Hans Wennborga792aff2011-12-07 10:33:11 +0000356std::string ArgTypeResult::getRepresentativeTypeName(ASTContext &C) const {
357 if (K != TypedefTy)
358 return std::string("'") + getRepresentativeType(C).getAsString() + "'";
359 return std::string("'") + Name + "' (aka '" + T.getAsString() + "')";
360}
361
362
Ted Kremenek826a3452010-07-16 02:11:22 +0000363//===----------------------------------------------------------------------===//
364// Methods on OptionalAmount.
365//===----------------------------------------------------------------------===//
366
367ArgTypeResult
368analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
369 return Ctx.IntTy;
370}
371
372//===----------------------------------------------------------------------===//
373// Methods on LengthModifier.
374//===----------------------------------------------------------------------===//
375
376const char *
377analyze_format_string::LengthModifier::toString() const {
378 switch (kind) {
379 case AsChar:
380 return "hh";
381 case AsShort:
382 return "h";
383 case AsLong: // or AsWideChar
384 return "l";
385 case AsLongLong:
386 return "ll";
387 case AsIntMax:
388 return "j";
389 case AsSizeT:
390 return "z";
391 case AsPtrDiff:
392 return "t";
393 case AsLongDouble:
394 return "L";
395 case None:
396 return "";
397 }
398 return NULL;
399}
400
401//===----------------------------------------------------------------------===//
402// Methods on OptionalAmount.
403//===----------------------------------------------------------------------===//
404
Chris Lattner5f9e2722011-07-23 10:55:15 +0000405void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000406 switch (hs) {
407 case Invalid:
408 case NotSpecified:
409 return;
410 case Arg:
411 if (UsesDotPrefix)
412 os << ".";
413 if (usesPositionalArg())
414 os << "*" << getPositionalArgIndex() << "$";
415 else
416 os << "*";
417 break;
418 case Constant:
419 if (UsesDotPrefix)
420 os << ".";
421 os << amt;
422 break;
423 }
424}
425
Ted Kremeneka412a492010-07-20 20:04:42 +0000426//===----------------------------------------------------------------------===//
Michael J. Spencer96827eb2010-07-27 04:46:02 +0000427// Methods on ConversionSpecifier.
Ted Kremeneka412a492010-07-20 20:04:42 +0000428//===----------------------------------------------------------------------===//
429
430bool FormatSpecifier::hasValidLengthModifier() const {
431 switch (LM.getKind()) {
432 case LengthModifier::None:
433 return true;
434
435 // Handle most integer flags
436 case LengthModifier::AsChar:
437 case LengthModifier::AsShort:
438 case LengthModifier::AsLongLong:
439 case LengthModifier::AsIntMax:
440 case LengthModifier::AsSizeT:
441 case LengthModifier::AsPtrDiff:
442 switch (CS.getKind()) {
443 case ConversionSpecifier::dArg:
444 case ConversionSpecifier::iArg:
445 case ConversionSpecifier::oArg:
446 case ConversionSpecifier::uArg:
447 case ConversionSpecifier::xArg:
448 case ConversionSpecifier::XArg:
449 case ConversionSpecifier::nArg:
450 return true;
451 default:
452 return false;
453 }
454
455 // Handle 'l' flag
456 case LengthModifier::AsLong:
457 switch (CS.getKind()) {
458 case ConversionSpecifier::dArg:
459 case ConversionSpecifier::iArg:
460 case ConversionSpecifier::oArg:
461 case ConversionSpecifier::uArg:
462 case ConversionSpecifier::xArg:
463 case ConversionSpecifier::XArg:
464 case ConversionSpecifier::aArg:
465 case ConversionSpecifier::AArg:
466 case ConversionSpecifier::fArg:
467 case ConversionSpecifier::FArg:
468 case ConversionSpecifier::eArg:
469 case ConversionSpecifier::EArg:
470 case ConversionSpecifier::gArg:
471 case ConversionSpecifier::GArg:
472 case ConversionSpecifier::nArg:
473 case ConversionSpecifier::cArg:
474 case ConversionSpecifier::sArg:
475 return true;
476 default:
477 return false;
478 }
479
480 case LengthModifier::AsLongDouble:
481 switch (CS.getKind()) {
482 case ConversionSpecifier::aArg:
483 case ConversionSpecifier::AArg:
484 case ConversionSpecifier::fArg:
485 case ConversionSpecifier::FArg:
486 case ConversionSpecifier::eArg:
487 case ConversionSpecifier::EArg:
488 case ConversionSpecifier::gArg:
489 case ConversionSpecifier::GArg:
490 return true;
491 default:
492 return false;
493 }
494 }
495 return false;
496}