blob: 0171bb7aec34a2c959c10d5324ab4e279b939bd7 [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
Ted Kremenek826a3452010-07-16 02:11:22 +0000231 case SpecificTy: {
232 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000233 QualType U = C.getCanonicalType(T);
234 if (U == 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:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000243 return U == 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:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000246 return U == C.SignedCharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000247 case BuiltinType::Short:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000248 return U == C.UnsignedShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000249 case BuiltinType::UShort:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000250 return U == C.ShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000251 case BuiltinType::Int:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000252 return U == C.UnsignedIntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000253 case BuiltinType::UInt:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000254 return U == C.IntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000255 case BuiltinType::Long:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000256 return U == C.UnsignedLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000257 case BuiltinType::ULong:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000258 return U == C.LongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000259 case BuiltinType::LongLong:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000260 return U == C.UnsignedLongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000261 case BuiltinType::ULongLong:
Hans Wennborg5fdc1b92011-12-02 19:22:15 +0000262 return U == 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:
335 return T;
336 case CStrTy:
337 return C.getPointerType(C.CharTy);
338 case WCStrTy:
339 return C.getPointerType(C.getWCharType());
340 case ObjCPointerTy:
341 return C.ObjCBuiltinIdTy;
342 case CPointerTy:
343 return C.VoidPtrTy;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000344 case WIntTy: {
345 QualType WC = C.getWCharType();
346 return WC->isPromotableIntegerType() ? C.getPromotedIntegerType(WC) : WC;
347 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000348 }
349
350 // FIXME: Should be unreachable, but Clang is currently emitting
351 // a warning.
352 return QualType();
353}
354
355//===----------------------------------------------------------------------===//
356// Methods on OptionalAmount.
357//===----------------------------------------------------------------------===//
358
359ArgTypeResult
360analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
361 return Ctx.IntTy;
362}
363
364//===----------------------------------------------------------------------===//
365// Methods on LengthModifier.
366//===----------------------------------------------------------------------===//
367
368const char *
369analyze_format_string::LengthModifier::toString() const {
370 switch (kind) {
371 case AsChar:
372 return "hh";
373 case AsShort:
374 return "h";
375 case AsLong: // or AsWideChar
376 return "l";
377 case AsLongLong:
378 return "ll";
379 case AsIntMax:
380 return "j";
381 case AsSizeT:
382 return "z";
383 case AsPtrDiff:
384 return "t";
385 case AsLongDouble:
386 return "L";
387 case None:
388 return "";
389 }
390 return NULL;
391}
392
393//===----------------------------------------------------------------------===//
394// Methods on OptionalAmount.
395//===----------------------------------------------------------------------===//
396
Chris Lattner5f9e2722011-07-23 10:55:15 +0000397void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000398 switch (hs) {
399 case Invalid:
400 case NotSpecified:
401 return;
402 case Arg:
403 if (UsesDotPrefix)
404 os << ".";
405 if (usesPositionalArg())
406 os << "*" << getPositionalArgIndex() << "$";
407 else
408 os << "*";
409 break;
410 case Constant:
411 if (UsesDotPrefix)
412 os << ".";
413 os << amt;
414 break;
415 }
416}
417
Ted Kremeneka412a492010-07-20 20:04:42 +0000418//===----------------------------------------------------------------------===//
Michael J. Spencer96827eb2010-07-27 04:46:02 +0000419// Methods on ConversionSpecifier.
Ted Kremeneka412a492010-07-20 20:04:42 +0000420//===----------------------------------------------------------------------===//
421
422bool FormatSpecifier::hasValidLengthModifier() const {
423 switch (LM.getKind()) {
424 case LengthModifier::None:
425 return true;
426
427 // Handle most integer flags
428 case LengthModifier::AsChar:
429 case LengthModifier::AsShort:
430 case LengthModifier::AsLongLong:
431 case LengthModifier::AsIntMax:
432 case LengthModifier::AsSizeT:
433 case LengthModifier::AsPtrDiff:
434 switch (CS.getKind()) {
435 case ConversionSpecifier::dArg:
436 case ConversionSpecifier::iArg:
437 case ConversionSpecifier::oArg:
438 case ConversionSpecifier::uArg:
439 case ConversionSpecifier::xArg:
440 case ConversionSpecifier::XArg:
441 case ConversionSpecifier::nArg:
442 return true;
443 default:
444 return false;
445 }
446
447 // Handle 'l' flag
448 case LengthModifier::AsLong:
449 switch (CS.getKind()) {
450 case ConversionSpecifier::dArg:
451 case ConversionSpecifier::iArg:
452 case ConversionSpecifier::oArg:
453 case ConversionSpecifier::uArg:
454 case ConversionSpecifier::xArg:
455 case ConversionSpecifier::XArg:
456 case ConversionSpecifier::aArg:
457 case ConversionSpecifier::AArg:
458 case ConversionSpecifier::fArg:
459 case ConversionSpecifier::FArg:
460 case ConversionSpecifier::eArg:
461 case ConversionSpecifier::EArg:
462 case ConversionSpecifier::gArg:
463 case ConversionSpecifier::GArg:
464 case ConversionSpecifier::nArg:
465 case ConversionSpecifier::cArg:
466 case ConversionSpecifier::sArg:
467 return true;
468 default:
469 return false;
470 }
471
472 case LengthModifier::AsLongDouble:
473 switch (CS.getKind()) {
474 case ConversionSpecifier::aArg:
475 case ConversionSpecifier::AArg:
476 case ConversionSpecifier::fArg:
477 case ConversionSpecifier::FArg:
478 case ConversionSpecifier::eArg:
479 case ConversionSpecifier::EArg:
480 case ConversionSpecifier::gArg:
481 case ConversionSpecifier::GArg:
482 return true;
483 default:
484 return false;
485 }
486 }
487 return false;
488}