blob: e1049b3c685bf725eac9f3ce9aad15a6ae003122 [file] [log] [blame]
Ted Kremenek826a3452010-07-16 02:11:22 +00001//== PrintfFormatString.cpp - Analysis of printf format strings --*- C++ -*-==//
Ted Kremenek8f0a1c72010-01-27 23:43:25 +00002//
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// Handling of format string in printf and friends. The structure of format
11// strings for fprintf() are described in C99 7.19.6.1.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek826a3452010-07-16 02:11:22 +000015#include "clang/Analysis/Analyses/FormatString.h"
16#include "FormatStringParsing.h"
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000017
Ted Kremenek826a3452010-07-16 02:11:22 +000018using clang::analyze_format_string::ArgTypeResult;
19using clang::analyze_format_string::FormatStringHandler;
20using clang::analyze_format_string::LengthModifier;
21using clang::analyze_format_string::OptionalAmount;
Ted Kremenek6ecb9502010-07-20 20:04:27 +000022using clang::analyze_format_string::ConversionSpecifier;
Ted Kremenek826a3452010-07-16 02:11:22 +000023using clang::analyze_printf::PrintfSpecifier;
Ted Kremenekefaff192010-02-27 01:41:03 +000024
Ted Kremenek808015a2010-01-29 03:16:21 +000025using namespace clang;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000026
Ted Kremenek826a3452010-07-16 02:11:22 +000027typedef clang::analyze_format_string::SpecifierResult<PrintfSpecifier>
28 PrintfSpecifierResult;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000029
30//===----------------------------------------------------------------------===//
31// Methods for parsing format strings.
32//===----------------------------------------------------------------------===//
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000033
Ted Kremenek826a3452010-07-16 02:11:22 +000034using analyze_format_string::ParseNonPositionAmount;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000035
Ted Kremenek826a3452010-07-16 02:11:22 +000036static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS,
Ted Kremenekefaff192010-02-27 01:41:03 +000037 const char *Start, const char *&Beg, const char *E,
38 unsigned *argIndex) {
39 if (argIndex) {
40 FS.setPrecision(ParseNonPositionAmount(Beg, E, *argIndex));
Chad Rosier30601782011-08-17 23:08:45 +000041 } else {
Ted Kremenekefaff192010-02-27 01:41:03 +000042 const OptionalAmount Amt = ParsePositionAmount(H, Start, Beg, E,
Ted Kremenek826a3452010-07-16 02:11:22 +000043 analyze_format_string::PrecisionPos);
Ted Kremenekefaff192010-02-27 01:41:03 +000044 if (Amt.isInvalid())
45 return true;
46 FS.setPrecision(Amt);
47 }
48 return false;
49}
50
Ted Kremenek826a3452010-07-16 02:11:22 +000051static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
Ted Kremenek74d56a12010-02-04 20:46:58 +000052 const char *&Beg,
Ted Kremenek7f70dc82010-02-26 19:18:41 +000053 const char *E,
Hans Wennborgd02deeb2011-12-15 10:25:47 +000054 unsigned &argIndex,
55 const LangOptions &LO) {
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000056
Ted Kremenek6ecb9502010-07-20 20:04:27 +000057 using namespace clang::analyze_format_string;
Ted Kremenek4b220fa2010-01-29 02:13:53 +000058 using namespace clang::analyze_printf;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000059
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000060 const char *I = Beg;
Ted Kremenekc7ae51a2010-01-28 00:02:05 +000061 const char *Start = 0;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000062 UpdateOnReturn <const char*> UpdateBeg(Beg, I);
63
64 // Look for a '%' character that indicates the start of a format specifier.
Ted Kremeneke729acb2010-01-28 23:56:52 +000065 for ( ; I != E ; ++I) {
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000066 char c = *I;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000067 if (c == '\0') {
68 // Detect spurious null characters, which are likely errors.
69 H.HandleNullChar(I);
70 return true;
71 }
72 if (c == '%') {
Ted Kremeneke729acb2010-01-28 23:56:52 +000073 Start = I++; // Record the start of the format specifier.
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000074 break;
75 }
76 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000077
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000078 // No format specifier found?
79 if (!Start)
80 return false;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000081
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000082 if (I == E) {
83 // No more characters left?
Ted Kremenek826a3452010-07-16 02:11:22 +000084 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000085 return true;
86 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000087
Ted Kremenek826a3452010-07-16 02:11:22 +000088 PrintfSpecifier FS;
Ted Kremenekefaff192010-02-27 01:41:03 +000089 if (ParseArgPosition(H, FS, Start, I, E))
90 return true;
91
92 if (I == E) {
93 // No more characters left?
Ted Kremenek826a3452010-07-16 02:11:22 +000094 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenekefaff192010-02-27 01:41:03 +000095 return true;
96 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +000097
Ted Kremenek8f0a1c72010-01-27 23:43:25 +000098 // Look for flags (if any).
99 bool hasMore = true;
100 for ( ; I != E; ++I) {
101 switch (*I) {
102 default: hasMore = false; break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000103 case '\'':
Ted Kremenekbd18d452011-01-08 05:28:38 +0000104 // FIXME: POSIX specific. Always accept?
105 FS.setHasThousandsGrouping(I);
106 break;
Tom Caree4ee9662010-06-17 19:00:27 +0000107 case '-': FS.setIsLeftJustified(I); break;
108 case '+': FS.setHasPlusPrefix(I); break;
109 case ' ': FS.setHasSpacePrefix(I); break;
110 case '#': FS.setHasAlternativeForm(I); break;
111 case '0': FS.setHasLeadingZeros(I); break;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000112 }
113 if (!hasMore)
114 break;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000115 }
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000116
117 if (I == E) {
118 // No more characters left?
Ted Kremenek826a3452010-07-16 02:11:22 +0000119 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000120 return true;
121 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000122
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000123 // Look for the field width (if any).
Ted Kremenekefaff192010-02-27 01:41:03 +0000124 if (ParseFieldWidth(H, FS, Start, I, E,
125 FS.usesPositionalArg() ? 0 : &argIndex))
126 return true;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000127
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000128 if (I == E) {
129 // No more characters left?
Ted Kremenek826a3452010-07-16 02:11:22 +0000130 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000131 return true;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000132 }
133
134 // Look for the precision (if any).
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000135 if (*I == '.') {
Ted Kremenek808015a2010-01-29 03:16:21 +0000136 ++I;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000137 if (I == E) {
Ted Kremenek826a3452010-07-16 02:11:22 +0000138 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000139 return true;
140 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000141
Ted Kremenekefaff192010-02-27 01:41:03 +0000142 if (ParsePrecision(H, FS, Start, I, E,
143 FS.usesPositionalArg() ? 0 : &argIndex))
144 return true;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000145
146 if (I == E) {
147 // No more characters left?
Ted Kremenek826a3452010-07-16 02:11:22 +0000148 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000149 return true;
150 }
151 }
152
153 // Look for the length modifier.
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000154 if (ParseLengthModifier(FS, I, E, LO) && I == E) {
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000155 // No more characters left?
Ted Kremenek826a3452010-07-16 02:11:22 +0000156 H.HandleIncompleteSpecifier(Start, E - Start);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000157 return true;
158 }
Ted Kremenekdf17f9d2010-02-09 00:04:09 +0000159
Ted Kremenek4dcb18f2010-01-29 20:29:53 +0000160 if (*I == '\0') {
Ted Kremenekdf17f9d2010-02-09 00:04:09 +0000161 // Detect spurious null characters, which are likely errors.
162 H.HandleNullChar(I);
163 return true;
Ted Kremenek4dcb18f2010-01-29 20:29:53 +0000164 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000165
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000166 // Finally, look for the conversion specifier.
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000167 const char *conversionPosition = I++;
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000168 ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier;
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000169 switch (*conversionPosition) {
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000170 default:
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000171 break;
Ted Kremenekc7cbb9b2010-01-28 00:55:28 +0000172 // C99: 7.19.6.1 (section 8).
Ted Kremenek87260c72010-02-24 00:05:54 +0000173 case '%': k = ConversionSpecifier::PercentArg; break;
174 case 'A': k = ConversionSpecifier::AArg; break;
175 case 'E': k = ConversionSpecifier::EArg; break;
176 case 'F': k = ConversionSpecifier::FArg; break;
177 case 'G': k = ConversionSpecifier::GArg; break;
178 case 'X': k = ConversionSpecifier::XArg; break;
179 case 'a': k = ConversionSpecifier::aArg; break;
Ted Kremenek92a6feb2010-07-20 20:03:49 +0000180 case 'c': k = ConversionSpecifier::cArg; break;
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000181 case 'd': k = ConversionSpecifier::dArg; break;
Ted Kremenek87260c72010-02-24 00:05:54 +0000182 case 'e': k = ConversionSpecifier::eArg; break;
183 case 'f': k = ConversionSpecifier::fArg; break;
184 case 'g': k = ConversionSpecifier::gArg; break;
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000185 case 'i': k = ConversionSpecifier::iArg; break;
Ted Kremenek35d353b2010-07-20 20:04:10 +0000186 case 'n': k = ConversionSpecifier::nArg; break;
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000187 case 'o': k = ConversionSpecifier::oArg; break;
Ted Kremenek66932052010-07-20 20:04:04 +0000188 case 'p': k = ConversionSpecifier::pArg; break;
Ted Kremenek99196b12010-07-20 20:03:58 +0000189 case 's': k = ConversionSpecifier::sArg; break;
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000190 case 'u': k = ConversionSpecifier::uArg; break;
191 case 'x': k = ConversionSpecifier::xArg; break;
Ted Kremenekbd18d452011-01-08 05:28:38 +0000192 // POSIX specific.
Ted Kremenek87260c72010-02-24 00:05:54 +0000193 case 'C': k = ConversionSpecifier::CArg; break;
Ted Kremenekde183a42010-07-20 20:04:15 +0000194 case 'S': k = ConversionSpecifier::SArg; break;
Ted Kremenekc7cbb9b2010-01-28 00:55:28 +0000195 // Objective-C.
Ted Kremenek4dcb18f2010-01-29 20:29:53 +0000196 case '@': k = ConversionSpecifier::ObjCObjArg; break;
Ted Kremenekdf17f9d2010-02-09 00:04:09 +0000197 // Glibc specific.
Ted Kremenek4dcb18f2010-01-29 20:29:53 +0000198 case 'm': k = ConversionSpecifier::PrintErrno; break;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000199 }
Ted Kremenek6ecb9502010-07-20 20:04:27 +0000200 PrintfConversionSpecifier CS(conversionPosition, k);
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000201 FS.setConversionSpecifier(CS);
Ted Kremenekefaff192010-02-27 01:41:03 +0000202 if (CS.consumesDataArgument() && !FS.usesPositionalArg())
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000203 FS.setArgIndex(argIndex++);
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000204
205 if (k == ConversionSpecifier::InvalidSpecifier) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000206 // Assume the conversion takes one argument.
Ted Kremenek79662972010-12-02 20:57:51 +0000207 return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, I - Start);
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000208 }
Ted Kremenek826a3452010-07-16 02:11:22 +0000209 return PrintfSpecifierResult(Start, FS);
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000210}
211
Ted Kremenek826a3452010-07-16 02:11:22 +0000212bool clang::analyze_format_string::ParsePrintfString(FormatStringHandler &H,
213 const char *I,
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000214 const char *E,
215 const LangOptions &LO) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000216
217 unsigned argIndex = 0;
218
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000219 // Keep looking for a format specifier until we have exhausted the string.
220 while (I != E) {
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000221 const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
222 LO);
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000223 // Did a fail-stop error of any kind occur when parsing the specifier?
224 // If so, don't do any more processing.
225 if (FSR.shouldStop())
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000226 return true;;
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000227 // Did we exhaust the string or encounter an error that
228 // we can recover from?
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000229 if (!FSR.hasValue())
Ted Kremenek26ac2e02010-01-29 02:40:24 +0000230 continue;
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000231 // We have a format specifier. Pass it to the callback.
Ted Kremenek826a3452010-07-16 02:11:22 +0000232 if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(),
Ted Kremeneka8d8fec2010-01-28 02:46:17 +0000233 I - FSR.getStart()))
Ted Kremenek4dcb18f2010-01-29 20:29:53 +0000234 return true;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000235 }
236 assert(I == E && "Format string not exhausted");
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000237 return false;
238}
Ted Kremenek8f0a1c72010-01-27 23:43:25 +0000239
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000240//===----------------------------------------------------------------------===//
Ted Kremenek826a3452010-07-16 02:11:22 +0000241// Methods on PrintfSpecifier.
Tom Care3bfc5f42010-06-09 04:11:11 +0000242//===----------------------------------------------------------------------===//
243
Nico Weber339b9072012-01-31 01:43:25 +0000244ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx,
245 bool IsObjCLiteral) const {
Ted Kremenek6ecb9502010-07-20 20:04:27 +0000246 const PrintfConversionSpecifier &CS = getConversionSpecifier();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000247
Ted Kremenek33567d22010-01-29 22:59:32 +0000248 if (!CS.consumesDataArgument())
249 return ArgTypeResult::Invalid();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000250
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000251 if (CS.getKind() == ConversionSpecifier::cArg)
252 switch (LM.getKind()) {
253 case LengthModifier::None: return Ctx.IntTy;
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000254 case LengthModifier::AsLong:
255 return ArgTypeResult(ArgTypeResult::WIntTy, "wint_t");
Ted Kremenek9325eaf2010-08-24 22:24:51 +0000256 default:
257 return ArgTypeResult::Invalid();
258 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000259
Ted Kremenek33567d22010-01-29 22:59:32 +0000260 if (CS.isIntArg())
Tom Care3bfc5f42010-06-09 04:11:11 +0000261 switch (LM.getKind()) {
262 case LengthModifier::AsLongDouble:
Ted Kremenek9d24c2c2012-01-24 21:29:54 +0000263 // GNU extension.
264 return Ctx.LongLongTy;
Tom Care3bfc5f42010-06-09 04:11:11 +0000265 case LengthModifier::None: return Ctx.IntTy;
Ted Kremenek6ca4a9a2011-10-25 04:20:41 +0000266 case LengthModifier::AsChar: return ArgTypeResult::AnyCharTy;
Tom Care3bfc5f42010-06-09 04:11:11 +0000267 case LengthModifier::AsShort: return Ctx.ShortTy;
268 case LengthModifier::AsLong: return Ctx.LongTy;
Hans Wennborg32addd52012-02-16 16:34:54 +0000269 case LengthModifier::AsLongLong:
270 case LengthModifier::AsQuad:
271 return Ctx.LongLongTy;
Hans Wennborga792aff2011-12-07 10:33:11 +0000272 case LengthModifier::AsIntMax:
273 return ArgTypeResult(Ctx.getIntMaxType(), "intmax_t");
Hans Wennborg29e97cb2011-10-27 08:29:09 +0000274 case LengthModifier::AsSizeT:
275 // FIXME: How to get the corresponding signed version of size_t?
Ted Kremenek33567d22010-01-29 22:59:32 +0000276 return ArgTypeResult();
Hans Wennborga792aff2011-12-07 10:33:11 +0000277 case LengthModifier::AsPtrDiff:
278 return ArgTypeResult(Ctx.getPointerDiffType(), "ptrdiff_t");
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000279 case LengthModifier::AsAllocate:
Hans Wennborg37969b72012-01-12 17:11:12 +0000280 case LengthModifier::AsMAllocate:
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000281 return ArgTypeResult::Invalid();
Ted Kremenek33567d22010-01-29 22:59:32 +0000282 }
283
284 if (CS.isUIntArg())
Tom Care3bfc5f42010-06-09 04:11:11 +0000285 switch (LM.getKind()) {
286 case LengthModifier::AsLongDouble:
Ted Kremenek9d24c2c2012-01-24 21:29:54 +0000287 // GNU extension.
288 return Ctx.UnsignedLongLongTy;
Tom Care3bfc5f42010-06-09 04:11:11 +0000289 case LengthModifier::None: return Ctx.UnsignedIntTy;
290 case LengthModifier::AsChar: return Ctx.UnsignedCharTy;
291 case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
292 case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
Hans Wennborg32addd52012-02-16 16:34:54 +0000293 case LengthModifier::AsLongLong:
294 case LengthModifier::AsQuad:
295 return Ctx.UnsignedLongLongTy;
Hans Wennborga792aff2011-12-07 10:33:11 +0000296 case LengthModifier::AsIntMax:
297 return ArgTypeResult(Ctx.getUIntMaxType(), "uintmax_t");
Tom Care3bfc5f42010-06-09 04:11:11 +0000298 case LengthModifier::AsSizeT:
Hans Wennborga792aff2011-12-07 10:33:11 +0000299 return ArgTypeResult(Ctx.getSizeType(), "size_t");
Tom Care3bfc5f42010-06-09 04:11:11 +0000300 case LengthModifier::AsPtrDiff:
Ted Kremenek33567d22010-01-29 22:59:32 +0000301 // FIXME: How to get the corresponding unsigned
302 // version of ptrdiff_t?
303 return ArgTypeResult();
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000304 case LengthModifier::AsAllocate:
Hans Wennborg37969b72012-01-12 17:11:12 +0000305 case LengthModifier::AsMAllocate:
Hans Wennborgd02deeb2011-12-15 10:25:47 +0000306 return ArgTypeResult::Invalid();
Ted Kremenek33567d22010-01-29 22:59:32 +0000307 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +0000308
Ted Kremenekf911eba2010-02-01 23:23:50 +0000309 if (CS.isDoubleArg()) {
Tom Care3bfc5f42010-06-09 04:11:11 +0000310 if (LM.getKind() == LengthModifier::AsLongDouble)
Ted Kremenekf911eba2010-02-01 23:23:50 +0000311 return Ctx.LongDoubleTy;
Ted Kremenekc9a89fe2010-01-30 01:02:18 +0000312 return Ctx.DoubleTy;
Ted Kremenekf911eba2010-02-01 23:23:50 +0000313 }
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000314
Ted Kremenek87260c72010-02-24 00:05:54 +0000315 switch (CS.getKind()) {
Hans Wennborg63082182011-12-09 15:57:33 +0000316 case ConversionSpecifier::sArg:
Nico Weber339b9072012-01-31 01:43:25 +0000317 if (LM.getKind() == LengthModifier::AsWideChar) {
318 if (IsObjCLiteral)
319 return Ctx.getPointerType(Ctx.UnsignedShortTy.withConst());
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000320 return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *");
Nico Weber339b9072012-01-31 01:43:25 +0000321 }
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000322 return ArgTypeResult::CStrTy;
Ted Kremenekde183a42010-07-20 20:04:15 +0000323 case ConversionSpecifier::SArg:
Nico Weber339b9072012-01-31 01:43:25 +0000324 if (IsObjCLiteral)
325 return Ctx.getPointerType(Ctx.UnsignedShortTy.withConst());
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000326 return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *");
Ted Kremenek87260c72010-02-24 00:05:54 +0000327 case ConversionSpecifier::CArg:
Nico Weber339b9072012-01-31 01:43:25 +0000328 if (IsObjCLiteral)
329 return Ctx.UnsignedShortTy;
Hans Wennborgf4f0c602011-12-09 12:22:12 +0000330 return ArgTypeResult(Ctx.WCharTy, "wchar_t");
Ted Kremenek66932052010-07-20 20:04:04 +0000331 case ConversionSpecifier::pArg:
Ted Kremenek13927a42010-06-16 21:23:04 +0000332 return ArgTypeResult::CPointerTy;
Ted Kremeneke6ca97f2012-01-25 00:04:09 +0000333 case ConversionSpecifier::ObjCObjArg:
334 return ArgTypeResult::ObjCPointerTy;
Ted Kremenek87260c72010-02-24 00:05:54 +0000335 default:
336 break;
337 }
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000338
Ted Kremenek33567d22010-01-29 22:59:32 +0000339 // FIXME: Handle other cases.
Ted Kremenek40888ad2010-01-29 23:00:35 +0000340 return ArgTypeResult();
Ted Kremenek33567d22010-01-29 22:59:32 +0000341}
342
Hans Wennborgbe6126a2012-02-15 09:59:46 +0000343bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
344 ASTContext &Ctx, bool IsObjCLiteral) {
Tom Care3bfc5f42010-06-09 04:11:11 +0000345 // Handle strings first (char *, wchar_t *)
346 if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
Ted Kremenek99196b12010-07-20 20:03:58 +0000347 CS.setKind(ConversionSpecifier::sArg);
Tom Care3bfc5f42010-06-09 04:11:11 +0000348
Tom Care876e9942010-06-11 04:22:02 +0000349 // Disable irrelevant flags
350 HasAlternativeForm = 0;
351 HasLeadingZeroes = 0;
352
Tom Care3bfc5f42010-06-09 04:11:11 +0000353 // Set the long length modifier for wide characters
354 if (QT->getPointeeType()->isWideCharType())
355 LM.setKind(LengthModifier::AsWideChar);
Hans Wennborg117348c2011-12-09 10:51:29 +0000356 else
357 LM.setKind(LengthModifier::None);
Tom Care3bfc5f42010-06-09 04:11:11 +0000358
359 return true;
360 }
361
362 // We can only work with builtin types.
Tom Care3bfc5f42010-06-09 04:11:11 +0000363 const BuiltinType *BT = QT->getAs<BuiltinType>();
John McCall2dde35b2011-10-18 22:28:37 +0000364 if (!BT)
365 return false;
Tom Care876e9942010-06-11 04:22:02 +0000366
Tom Care3bfc5f42010-06-09 04:11:11 +0000367 // Set length modifier
368 switch (BT->getKind()) {
Eli Friedman06d88ec2011-04-27 22:06:20 +0000369 case BuiltinType::Bool:
370 case BuiltinType::WChar_U:
371 case BuiltinType::WChar_S:
372 case BuiltinType::Char16:
373 case BuiltinType::Char32:
374 case BuiltinType::UInt128:
375 case BuiltinType::Int128:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000376 case BuiltinType::Half:
John McCall2dde35b2011-10-18 22:28:37 +0000377 // Various types which are non-trivial to correct.
Eli Friedman06d88ec2011-04-27 22:06:20 +0000378 return false;
379
John McCall2dde35b2011-10-18 22:28:37 +0000380#define SIGNED_TYPE(Id, SingletonId)
381#define UNSIGNED_TYPE(Id, SingletonId)
382#define FLOATING_TYPE(Id, SingletonId)
383#define BUILTIN_TYPE(Id, SingletonId) \
384 case BuiltinType::Id:
385#include "clang/AST/BuiltinTypes.def"
Eli Friedman06d88ec2011-04-27 22:06:20 +0000386 // Misc other stuff which doesn't make sense here.
387 return false;
388
389 case BuiltinType::UInt:
390 case BuiltinType::Int:
391 case BuiltinType::Float:
392 case BuiltinType::Double:
Tom Care876e9942010-06-11 04:22:02 +0000393 LM.setKind(LengthModifier::None);
Tom Care3bfc5f42010-06-09 04:11:11 +0000394 break;
Tom Care876e9942010-06-11 04:22:02 +0000395
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +0000396 case BuiltinType::Char_U:
397 case BuiltinType::UChar:
398 case BuiltinType::Char_S:
399 case BuiltinType::SChar:
400 LM.setKind(LengthModifier::AsChar);
401 break;
402
403 case BuiltinType::Short:
404 case BuiltinType::UShort:
405 LM.setKind(LengthModifier::AsShort);
406 break;
407
Tom Care3bfc5f42010-06-09 04:11:11 +0000408 case BuiltinType::Long:
409 case BuiltinType::ULong:
410 LM.setKind(LengthModifier::AsLong);
411 break;
412
413 case BuiltinType::LongLong:
414 case BuiltinType::ULongLong:
415 LM.setKind(LengthModifier::AsLongLong);
416 break;
417
418 case BuiltinType::LongDouble:
419 LM.setKind(LengthModifier::AsLongDouble);
420 break;
421 }
422
Hans Wennborga7da2152011-10-18 08:10:06 +0000423 // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
424 if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus0x)) {
425 const IdentifierInfo *Identifier = QT.getBaseTypeIdentifier();
426 if (Identifier->getName() == "size_t") {
427 LM.setKind(LengthModifier::AsSizeT);
428 } else if (Identifier->getName() == "ssize_t") {
429 // Not C99, but common in Unix.
430 LM.setKind(LengthModifier::AsSizeT);
431 } else if (Identifier->getName() == "intmax_t") {
432 LM.setKind(LengthModifier::AsIntMax);
433 } else if (Identifier->getName() == "uintmax_t") {
434 LM.setKind(LengthModifier::AsIntMax);
435 } else if (Identifier->getName() == "ptrdiff_t") {
436 LM.setKind(LengthModifier::AsPtrDiff);
437 }
438 }
439
Hans Wennborgbe6126a2012-02-15 09:59:46 +0000440 // If fixing the length modifier was enough, we are done.
441 const analyze_printf::ArgTypeResult &ATR = getArgType(Ctx, IsObjCLiteral);
442 if (hasValidLengthModifier() && ATR.isValid() && ATR.matchesType(Ctx, QT))
443 return true;
444
Tom Care3bfc5f42010-06-09 04:11:11 +0000445 // Set conversion specifier and disable any flags which do not apply to it.
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +0000446 // Let typedefs to char fall through to int, as %c is silly for uint8_t.
447 if (isa<TypedefType>(QT) && QT->isAnyCharacterType()) {
Ted Kremenek92a6feb2010-07-20 20:03:49 +0000448 CS.setKind(ConversionSpecifier::cArg);
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +0000449 LM.setKind(LengthModifier::None);
Tom Care3bfc5f42010-06-09 04:11:11 +0000450 Precision.setHowSpecified(OptionalAmount::NotSpecified);
451 HasAlternativeForm = 0;
452 HasLeadingZeroes = 0;
Tom Caree4ee9662010-06-17 19:00:27 +0000453 HasPlusPrefix = 0;
Tom Care3bfc5f42010-06-09 04:11:11 +0000454 }
455 // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000456 else if (QT->isRealFloatingType()) {
Tom Care3bfc5f42010-06-09 04:11:11 +0000457 CS.setKind(ConversionSpecifier::fArg);
458 }
Tom Care3bfc5f42010-06-09 04:11:11 +0000459 else if (QT->isSignedIntegerType()) {
460 CS.setKind(ConversionSpecifier::dArg);
461 HasAlternativeForm = 0;
462 }
Douglas Gregorc8c4b402010-06-09 05:25:34 +0000463 else if (QT->isUnsignedIntegerType()) {
Hans Wennborgbe6126a2012-02-15 09:59:46 +0000464 CS.setKind(ConversionSpecifier::uArg);
Tom Care3bfc5f42010-06-09 04:11:11 +0000465 HasAlternativeForm = 0;
Tom Caree4ee9662010-06-17 19:00:27 +0000466 HasPlusPrefix = 0;
Chad Rosier30601782011-08-17 23:08:45 +0000467 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +0000468 llvm_unreachable("Unexpected type");
Tom Care3bfc5f42010-06-09 04:11:11 +0000469 }
470
471 return true;
472}
473
Chris Lattner5f9e2722011-07-23 10:55:15 +0000474void PrintfSpecifier::toString(raw_ostream &os) const {
Tom Care3bfc5f42010-06-09 04:11:11 +0000475 // Whilst some features have no defined order, we are using the order
NAKAMURA Takumi00995302011-01-27 07:09:49 +0000476 // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
Tom Care3bfc5f42010-06-09 04:11:11 +0000477 os << "%";
478
479 // Positional args
480 if (usesPositionalArg()) {
481 os << getPositionalArgIndex() << "$";
482 }
483
484 // Conversion flags
485 if (IsLeftJustified) os << "-";
486 if (HasPlusPrefix) os << "+";
487 if (HasSpacePrefix) os << " ";
488 if (HasAlternativeForm) os << "#";
489 if (HasLeadingZeroes) os << "0";
490
491 // Minimum field width
492 FieldWidth.toString(os);
493 // Precision
494 Precision.toString(os);
495 // Length modifier
496 os << LM.toString();
497 // Conversion specifier
498 os << CS.toString();
499}
Tom Caree4ee9662010-06-17 19:00:27 +0000500
Ted Kremenek826a3452010-07-16 02:11:22 +0000501bool PrintfSpecifier::hasValidPlusPrefix() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000502 if (!HasPlusPrefix)
503 return true;
504
505 // The plus prefix only makes sense for signed conversions
506 switch (CS.getKind()) {
507 case ConversionSpecifier::dArg:
508 case ConversionSpecifier::iArg:
509 case ConversionSpecifier::fArg:
510 case ConversionSpecifier::FArg:
511 case ConversionSpecifier::eArg:
512 case ConversionSpecifier::EArg:
513 case ConversionSpecifier::gArg:
514 case ConversionSpecifier::GArg:
515 case ConversionSpecifier::aArg:
516 case ConversionSpecifier::AArg:
517 return true;
518
519 default:
520 return false;
521 }
522}
523
Ted Kremenek826a3452010-07-16 02:11:22 +0000524bool PrintfSpecifier::hasValidAlternativeForm() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000525 if (!HasAlternativeForm)
526 return true;
527
Anders Carlsson2e6c19a2010-11-21 18:34:21 +0000528 // Alternate form flag only valid with the oxXaAeEfFgG conversions
Tom Caree4ee9662010-06-17 19:00:27 +0000529 switch (CS.getKind()) {
530 case ConversionSpecifier::oArg:
531 case ConversionSpecifier::xArg:
Anders Carlsson2e6c19a2010-11-21 18:34:21 +0000532 case ConversionSpecifier::XArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000533 case ConversionSpecifier::aArg:
534 case ConversionSpecifier::AArg:
535 case ConversionSpecifier::eArg:
536 case ConversionSpecifier::EArg:
537 case ConversionSpecifier::fArg:
538 case ConversionSpecifier::FArg:
539 case ConversionSpecifier::gArg:
540 case ConversionSpecifier::GArg:
541 return true;
542
543 default:
544 return false;
545 }
546}
547
Ted Kremenek826a3452010-07-16 02:11:22 +0000548bool PrintfSpecifier::hasValidLeadingZeros() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000549 if (!HasLeadingZeroes)
550 return true;
551
552 // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
553 switch (CS.getKind()) {
554 case ConversionSpecifier::dArg:
555 case ConversionSpecifier::iArg:
556 case ConversionSpecifier::oArg:
557 case ConversionSpecifier::uArg:
558 case ConversionSpecifier::xArg:
559 case ConversionSpecifier::XArg:
560 case ConversionSpecifier::aArg:
561 case ConversionSpecifier::AArg:
562 case ConversionSpecifier::eArg:
563 case ConversionSpecifier::EArg:
564 case ConversionSpecifier::fArg:
565 case ConversionSpecifier::FArg:
566 case ConversionSpecifier::gArg:
567 case ConversionSpecifier::GArg:
568 return true;
569
570 default:
571 return false;
572 }
573}
574
Ted Kremenek826a3452010-07-16 02:11:22 +0000575bool PrintfSpecifier::hasValidSpacePrefix() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000576 if (!HasSpacePrefix)
577 return true;
578
579 // The space prefix only makes sense for signed conversions
580 switch (CS.getKind()) {
581 case ConversionSpecifier::dArg:
582 case ConversionSpecifier::iArg:
583 case ConversionSpecifier::fArg:
584 case ConversionSpecifier::FArg:
585 case ConversionSpecifier::eArg:
586 case ConversionSpecifier::EArg:
587 case ConversionSpecifier::gArg:
588 case ConversionSpecifier::GArg:
589 case ConversionSpecifier::aArg:
590 case ConversionSpecifier::AArg:
591 return true;
592
593 default:
594 return false;
595 }
596}
597
Ted Kremenek826a3452010-07-16 02:11:22 +0000598bool PrintfSpecifier::hasValidLeftJustified() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000599 if (!IsLeftJustified)
600 return true;
601
602 // The left justified flag is valid for all conversions except n
603 switch (CS.getKind()) {
Ted Kremenek35d353b2010-07-20 20:04:10 +0000604 case ConversionSpecifier::nArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000605 return false;
606
607 default:
608 return true;
609 }
610}
611
Ted Kremenek65197b42011-01-08 05:28:46 +0000612bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
613 if (!HasThousandsGrouping)
614 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Ted Kremenek65197b42011-01-08 05:28:46 +0000616 switch (CS.getKind()) {
617 case ConversionSpecifier::dArg:
618 case ConversionSpecifier::iArg:
619 case ConversionSpecifier::uArg:
620 case ConversionSpecifier::fArg:
621 case ConversionSpecifier::FArg:
622 case ConversionSpecifier::gArg:
623 case ConversionSpecifier::GArg:
624 return true;
625 default:
626 return false;
627 }
628}
629
Ted Kremenek826a3452010-07-16 02:11:22 +0000630bool PrintfSpecifier::hasValidPrecision() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000631 if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
632 return true;
633
634 // Precision is only valid with the diouxXaAeEfFgGs conversions
635 switch (CS.getKind()) {
636 case ConversionSpecifier::dArg:
637 case ConversionSpecifier::iArg:
638 case ConversionSpecifier::oArg:
639 case ConversionSpecifier::uArg:
640 case ConversionSpecifier::xArg:
641 case ConversionSpecifier::XArg:
642 case ConversionSpecifier::aArg:
643 case ConversionSpecifier::AArg:
644 case ConversionSpecifier::eArg:
645 case ConversionSpecifier::EArg:
646 case ConversionSpecifier::fArg:
647 case ConversionSpecifier::FArg:
648 case ConversionSpecifier::gArg:
649 case ConversionSpecifier::GArg:
Ted Kremenek99196b12010-07-20 20:03:58 +0000650 case ConversionSpecifier::sArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000651 return true;
652
653 default:
654 return false;
655 }
656}
Ted Kremenek826a3452010-07-16 02:11:22 +0000657bool PrintfSpecifier::hasValidFieldWidth() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000658 if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
659 return true;
660
661 // The field width is valid for all conversions except n
662 switch (CS.getKind()) {
Ted Kremenek35d353b2010-07-20 20:04:10 +0000663 case ConversionSpecifier::nArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000664 return false;
665
666 default:
667 return true;
668 }
669}