blob: 6498ded4e374df02b8b10b35ff9fd8eefefc15ed [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();
233 if (T == argTy)
234 return true;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000235 // Check for "compatible types".
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000236 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
Ted Kremenek826a3452010-07-16 02:11:22 +0000237 switch (BT->getKind()) {
238 default:
239 break;
240 case BuiltinType::Char_S:
241 case BuiltinType::SChar:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000242 return T == C.UnsignedCharTy;
Ted Kremenekdc00d812011-07-13 17:35:14 +0000243 case BuiltinType::Char_U:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000244 case BuiltinType::UChar:
245 return T == C.SignedCharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000246 case BuiltinType::Short:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000247 return T == C.UnsignedShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000248 case BuiltinType::UShort:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000249 return T == C.ShortTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000250 case BuiltinType::Int:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000251 return T == C.UnsignedIntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000252 case BuiltinType::UInt:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000253 return T == C.IntTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000254 case BuiltinType::Long:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000255 return T == C.UnsignedLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000256 case BuiltinType::ULong:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000257 return T == C.LongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000258 case BuiltinType::LongLong:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000259 return T == C.UnsignedLongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000260 case BuiltinType::ULongLong:
Ted Kremenek1ad35be2011-07-14 17:05:32 +0000261 return T == C.LongLongTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000262 }
263 return false;
264 }
265
266 case CStrTy: {
267 const PointerType *PT = argTy->getAs<PointerType>();
268 if (!PT)
269 return false;
270 QualType pointeeTy = PT->getPointeeType();
271 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
272 switch (BT->getKind()) {
273 case BuiltinType::Void:
274 case BuiltinType::Char_U:
275 case BuiltinType::UChar:
276 case BuiltinType::Char_S:
277 case BuiltinType::SChar:
278 return true;
279 default:
280 break;
281 }
282
283 return false;
284 }
285
286 case WCStrTy: {
287 const PointerType *PT = argTy->getAs<PointerType>();
288 if (!PT)
289 return false;
290 QualType pointeeTy =
291 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
292 return pointeeTy == C.getWCharType();
293 }
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000294
295 case WIntTy: {
296 // Instead of doing a lookup for the definition of 'wint_t' (which
297 // is defined by the system headers) instead see if wchar_t and
298 // the argument type promote to the same type.
299 QualType PromoWChar =
300 C.getWCharType()->isPromotableIntegerType()
301 ? C.getPromotedIntegerType(C.getWCharType()) : C.getWCharType();
302 QualType PromoArg =
303 argTy->isPromotableIntegerType()
304 ? C.getPromotedIntegerType(argTy) : argTy;
305
306 PromoWChar = C.getCanonicalType(PromoWChar).getUnqualifiedType();
307 PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
308
309 return PromoWChar == PromoArg;
310 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000311
312 case CPointerTy:
Anders Carlsson62425992010-11-06 14:58:53 +0000313 return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
314 argTy->isNullPtrType();
Ted Kremenek826a3452010-07-16 02:11:22 +0000315
316 case ObjCPointerTy:
Daniel Dunbard6a4d182011-06-28 23:33:55 +0000317 return argTy->getAs<ObjCObjectPointerType>() != NULL;
Ted Kremenek826a3452010-07-16 02:11:22 +0000318 }
319
320 // FIXME: Should be unreachable, but Clang is currently emitting
321 // a warning.
322 return false;
323}
324
325QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
326 switch (K) {
327 case InvalidTy:
David Blaikieb219cfc2011-09-23 05:06:16 +0000328 llvm_unreachable("No representative type for Invalid ArgTypeResult");
Ted Kremenek826a3452010-07-16 02:11:22 +0000329 case UnknownTy:
330 return QualType();
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000331 case AnyCharTy:
332 return C.CharTy;
Ted Kremenek826a3452010-07-16 02:11:22 +0000333 case SpecificTy:
334 return T;
335 case CStrTy:
336 return C.getPointerType(C.CharTy);
337 case WCStrTy:
338 return C.getPointerType(C.getWCharType());
339 case ObjCPointerTy:
340 return C.ObjCBuiltinIdTy;
341 case CPointerTy:
342 return C.VoidPtrTy;
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000343 case WIntTy: {
344 QualType WC = C.getWCharType();
345 return WC->isPromotableIntegerType() ? C.getPromotedIntegerType(WC) : WC;
346 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000347 }
348
349 // FIXME: Should be unreachable, but Clang is currently emitting
350 // a warning.
351 return QualType();
352}
353
354//===----------------------------------------------------------------------===//
355// Methods on OptionalAmount.
356//===----------------------------------------------------------------------===//
357
358ArgTypeResult
359analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
360 return Ctx.IntTy;
361}
362
363//===----------------------------------------------------------------------===//
364// Methods on LengthModifier.
365//===----------------------------------------------------------------------===//
366
367const char *
368analyze_format_string::LengthModifier::toString() const {
369 switch (kind) {
370 case AsChar:
371 return "hh";
372 case AsShort:
373 return "h";
374 case AsLong: // or AsWideChar
375 return "l";
376 case AsLongLong:
377 return "ll";
378 case AsIntMax:
379 return "j";
380 case AsSizeT:
381 return "z";
382 case AsPtrDiff:
383 return "t";
384 case AsLongDouble:
385 return "L";
386 case None:
387 return "";
388 }
389 return NULL;
390}
391
392//===----------------------------------------------------------------------===//
393// Methods on OptionalAmount.
394//===----------------------------------------------------------------------===//
395
Chris Lattner5f9e2722011-07-23 10:55:15 +0000396void OptionalAmount::toString(raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000397 switch (hs) {
398 case Invalid:
399 case NotSpecified:
400 return;
401 case Arg:
402 if (UsesDotPrefix)
403 os << ".";
404 if (usesPositionalArg())
405 os << "*" << getPositionalArgIndex() << "$";
406 else
407 os << "*";
408 break;
409 case Constant:
410 if (UsesDotPrefix)
411 os << ".";
412 os << amt;
413 break;
414 }
415}
416
Ted Kremeneka412a492010-07-20 20:04:42 +0000417//===----------------------------------------------------------------------===//
Michael J. Spencer96827eb2010-07-27 04:46:02 +0000418// Methods on ConversionSpecifier.
Ted Kremeneka412a492010-07-20 20:04:42 +0000419//===----------------------------------------------------------------------===//
420
421bool FormatSpecifier::hasValidLengthModifier() const {
422 switch (LM.getKind()) {
423 case LengthModifier::None:
424 return true;
425
426 // Handle most integer flags
427 case LengthModifier::AsChar:
428 case LengthModifier::AsShort:
429 case LengthModifier::AsLongLong:
430 case LengthModifier::AsIntMax:
431 case LengthModifier::AsSizeT:
432 case LengthModifier::AsPtrDiff:
433 switch (CS.getKind()) {
434 case ConversionSpecifier::dArg:
435 case ConversionSpecifier::iArg:
436 case ConversionSpecifier::oArg:
437 case ConversionSpecifier::uArg:
438 case ConversionSpecifier::xArg:
439 case ConversionSpecifier::XArg:
440 case ConversionSpecifier::nArg:
441 return true;
442 default:
443 return false;
444 }
445
446 // Handle 'l' flag
447 case LengthModifier::AsLong:
448 switch (CS.getKind()) {
449 case ConversionSpecifier::dArg:
450 case ConversionSpecifier::iArg:
451 case ConversionSpecifier::oArg:
452 case ConversionSpecifier::uArg:
453 case ConversionSpecifier::xArg:
454 case ConversionSpecifier::XArg:
455 case ConversionSpecifier::aArg:
456 case ConversionSpecifier::AArg:
457 case ConversionSpecifier::fArg:
458 case ConversionSpecifier::FArg:
459 case ConversionSpecifier::eArg:
460 case ConversionSpecifier::EArg:
461 case ConversionSpecifier::gArg:
462 case ConversionSpecifier::GArg:
463 case ConversionSpecifier::nArg:
464 case ConversionSpecifier::cArg:
465 case ConversionSpecifier::sArg:
466 return true;
467 default:
468 return false;
469 }
470
471 case LengthModifier::AsLongDouble:
472 switch (CS.getKind()) {
473 case ConversionSpecifier::aArg:
474 case ConversionSpecifier::AArg:
475 case ConversionSpecifier::fArg:
476 case ConversionSpecifier::FArg:
477 case ConversionSpecifier::eArg:
478 case ConversionSpecifier::EArg:
479 case ConversionSpecifier::gArg:
480 case ConversionSpecifier::GArg:
481 return true;
482 default:
483 return false;
484 }
485 }
486 return false;
487}
488
489