blob: a6cfba228e9b795b22422fad29168d8c80d07004 [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;
186 lmKind = (I != E && *I == 'h') ?
187 ++I, LengthModifier::AsChar : LengthModifier::AsShort;
188 break;
189 case 'l':
190 ++I;
191 lmKind = (I != E && *I == 'l') ?
192 ++I, LengthModifier::AsLongLong : LengthModifier::AsLong;
193 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:
212 assert(false && "ArgTypeResult must be valid");
213 return true;
214
215 case UnknownTy:
216 return true;
217
218 case SpecificTy: {
219 argTy = C.getCanonicalType(argTy).getUnqualifiedType();
220 if (T == argTy)
221 return true;
222 if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
223 switch (BT->getKind()) {
224 default:
225 break;
226 case BuiltinType::Char_S:
227 case BuiltinType::SChar:
228 return T == C.UnsignedCharTy;
229 case BuiltinType::Char_U:
230 case BuiltinType::UChar:
231 return T == C.SignedCharTy;
232 case BuiltinType::Short:
233 return T == C.UnsignedShortTy;
234 case BuiltinType::UShort:
235 return T == C.ShortTy;
236 case BuiltinType::Int:
237 return T == C.UnsignedIntTy;
238 case BuiltinType::UInt:
239 return T == C.IntTy;
240 case BuiltinType::Long:
241 return T == C.UnsignedLongTy;
242 case BuiltinType::ULong:
243 return T == C.LongTy;
244 case BuiltinType::LongLong:
245 return T == C.UnsignedLongLongTy;
246 case BuiltinType::ULongLong:
247 return T == C.LongLongTy;
248 }
249 return false;
250 }
251
252 case CStrTy: {
253 const PointerType *PT = argTy->getAs<PointerType>();
254 if (!PT)
255 return false;
256 QualType pointeeTy = PT->getPointeeType();
257 if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
258 switch (BT->getKind()) {
259 case BuiltinType::Void:
260 case BuiltinType::Char_U:
261 case BuiltinType::UChar:
262 case BuiltinType::Char_S:
263 case BuiltinType::SChar:
264 return true;
265 default:
266 break;
267 }
268
269 return false;
270 }
271
272 case WCStrTy: {
273 const PointerType *PT = argTy->getAs<PointerType>();
274 if (!PT)
275 return false;
276 QualType pointeeTy =
277 C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
278 return pointeeTy == C.getWCharType();
279 }
280
281 case CPointerTy:
282 return argTy->getAs<PointerType>() != NULL ||
Eli Friedmana7e68452010-08-22 01:00:03 +0000283 argTy->getAs<ObjCObjectPointerType>() != NULL;
Ted Kremenek826a3452010-07-16 02:11:22 +0000284
285 case ObjCPointerTy:
286 return argTy->getAs<ObjCObjectPointerType>() != NULL;
287 }
288
289 // FIXME: Should be unreachable, but Clang is currently emitting
290 // a warning.
291 return false;
292}
293
294QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
295 switch (K) {
296 case InvalidTy:
297 assert(false && "No representative type for Invalid ArgTypeResult");
298 // Fall-through.
299 case UnknownTy:
300 return QualType();
301 case SpecificTy:
302 return T;
303 case CStrTy:
304 return C.getPointerType(C.CharTy);
305 case WCStrTy:
306 return C.getPointerType(C.getWCharType());
307 case ObjCPointerTy:
308 return C.ObjCBuiltinIdTy;
309 case CPointerTy:
310 return C.VoidPtrTy;
311 }
312
313 // FIXME: Should be unreachable, but Clang is currently emitting
314 // a warning.
315 return QualType();
316}
317
318//===----------------------------------------------------------------------===//
319// Methods on OptionalAmount.
320//===----------------------------------------------------------------------===//
321
322ArgTypeResult
323analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
324 return Ctx.IntTy;
325}
326
327//===----------------------------------------------------------------------===//
328// Methods on LengthModifier.
329//===----------------------------------------------------------------------===//
330
331const char *
332analyze_format_string::LengthModifier::toString() const {
333 switch (kind) {
334 case AsChar:
335 return "hh";
336 case AsShort:
337 return "h";
338 case AsLong: // or AsWideChar
339 return "l";
340 case AsLongLong:
341 return "ll";
342 case AsIntMax:
343 return "j";
344 case AsSizeT:
345 return "z";
346 case AsPtrDiff:
347 return "t";
348 case AsLongDouble:
349 return "L";
350 case None:
351 return "";
352 }
353 return NULL;
354}
355
356//===----------------------------------------------------------------------===//
357// Methods on OptionalAmount.
358//===----------------------------------------------------------------------===//
359
Ted Kremeneka412a492010-07-20 20:04:42 +0000360void OptionalAmount::toString(llvm::raw_ostream &os) const {
Ted Kremenek826a3452010-07-16 02:11:22 +0000361 switch (hs) {
362 case Invalid:
363 case NotSpecified:
364 return;
365 case Arg:
366 if (UsesDotPrefix)
367 os << ".";
368 if (usesPositionalArg())
369 os << "*" << getPositionalArgIndex() << "$";
370 else
371 os << "*";
372 break;
373 case Constant:
374 if (UsesDotPrefix)
375 os << ".";
376 os << amt;
377 break;
378 }
379}
380
Ted Kremeneka412a492010-07-20 20:04:42 +0000381//===----------------------------------------------------------------------===//
Michael J. Spencer96827eb2010-07-27 04:46:02 +0000382// Methods on ConversionSpecifier.
Ted Kremeneka412a492010-07-20 20:04:42 +0000383//===----------------------------------------------------------------------===//
384
385bool FormatSpecifier::hasValidLengthModifier() const {
386 switch (LM.getKind()) {
387 case LengthModifier::None:
388 return true;
389
390 // Handle most integer flags
391 case LengthModifier::AsChar:
392 case LengthModifier::AsShort:
393 case LengthModifier::AsLongLong:
394 case LengthModifier::AsIntMax:
395 case LengthModifier::AsSizeT:
396 case LengthModifier::AsPtrDiff:
397 switch (CS.getKind()) {
398 case ConversionSpecifier::dArg:
399 case ConversionSpecifier::iArg:
400 case ConversionSpecifier::oArg:
401 case ConversionSpecifier::uArg:
402 case ConversionSpecifier::xArg:
403 case ConversionSpecifier::XArg:
404 case ConversionSpecifier::nArg:
405 return true;
406 default:
407 return false;
408 }
409
410 // Handle 'l' flag
411 case LengthModifier::AsLong:
412 switch (CS.getKind()) {
413 case ConversionSpecifier::dArg:
414 case ConversionSpecifier::iArg:
415 case ConversionSpecifier::oArg:
416 case ConversionSpecifier::uArg:
417 case ConversionSpecifier::xArg:
418 case ConversionSpecifier::XArg:
419 case ConversionSpecifier::aArg:
420 case ConversionSpecifier::AArg:
421 case ConversionSpecifier::fArg:
422 case ConversionSpecifier::FArg:
423 case ConversionSpecifier::eArg:
424 case ConversionSpecifier::EArg:
425 case ConversionSpecifier::gArg:
426 case ConversionSpecifier::GArg:
427 case ConversionSpecifier::nArg:
428 case ConversionSpecifier::cArg:
429 case ConversionSpecifier::sArg:
430 return true;
431 default:
432 return false;
433 }
434
435 case LengthModifier::AsLongDouble:
436 switch (CS.getKind()) {
437 case ConversionSpecifier::aArg:
438 case ConversionSpecifier::AArg:
439 case ConversionSpecifier::fArg:
440 case ConversionSpecifier::FArg:
441 case ConversionSpecifier::eArg:
442 case ConversionSpecifier::EArg:
443 case ConversionSpecifier::gArg:
444 case ConversionSpecifier::GArg:
445 return true;
446 default:
447 return false;
448 }
449 }
450 return false;
451}
452
453