blob: ed87c92101365a015bb3322351f4d0943cffcabd [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;
Hans Wennborgcec9ce42012-07-30 17:11:32 +0000333 case ConversionSpecifier::nArg:
334 return Ctx.getPointerType(Ctx.IntTy);
Ted Kremeneke6ca97f2012-01-25 00:04:09 +0000335 case ConversionSpecifier::ObjCObjArg:
336 return ArgTypeResult::ObjCPointerTy;
Ted Kremenek87260c72010-02-24 00:05:54 +0000337 default:
338 break;
339 }
Ted Kremenek7f70dc82010-02-26 19:18:41 +0000340
Ted Kremenek33567d22010-01-29 22:59:32 +0000341 // FIXME: Handle other cases.
Ted Kremenek40888ad2010-01-29 23:00:35 +0000342 return ArgTypeResult();
Ted Kremenek33567d22010-01-29 22:59:32 +0000343}
344
Hans Wennborgbe6126a2012-02-15 09:59:46 +0000345bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
346 ASTContext &Ctx, bool IsObjCLiteral) {
Hans Wennborgcec9ce42012-07-30 17:11:32 +0000347 // %n is different from other conversion specifiers; don't try to fix it.
348 if (CS.getKind() == ConversionSpecifier::nArg)
349 return false;
350
Jordan Rose153acdb2012-05-30 21:53:13 +0000351 // Handle Objective-C objects first. Note that while the '%@' specifier will
352 // not warn for structure pointer or void pointer arguments (because that's
353 // how CoreFoundation objects are implemented), we only show a fixit for '%@'
354 // if we know it's an object (block, id, class, or __attribute__((NSObject))).
355 if (QT->isObjCRetainableType()) {
356 if (!IsObjCLiteral)
357 return false;
358
359 CS.setKind(ConversionSpecifier::ObjCObjArg);
360
361 // Disable irrelevant flags
362 HasThousandsGrouping = false;
363 HasPlusPrefix = false;
364 HasSpacePrefix = false;
365 HasAlternativeForm = false;
366 HasLeadingZeroes = false;
367 Precision.setHowSpecified(OptionalAmount::NotSpecified);
368 LM.setKind(LengthModifier::None);
369
370 return true;
371 }
372
373 // Handle strings next (char *, wchar_t *)
Tom Care3bfc5f42010-06-09 04:11:11 +0000374 if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
Ted Kremenek99196b12010-07-20 20:03:58 +0000375 CS.setKind(ConversionSpecifier::sArg);
Tom Care3bfc5f42010-06-09 04:11:11 +0000376
Tom Care876e9942010-06-11 04:22:02 +0000377 // Disable irrelevant flags
378 HasAlternativeForm = 0;
379 HasLeadingZeroes = 0;
380
Tom Care3bfc5f42010-06-09 04:11:11 +0000381 // Set the long length modifier for wide characters
382 if (QT->getPointeeType()->isWideCharType())
383 LM.setKind(LengthModifier::AsWideChar);
Hans Wennborg117348c2011-12-09 10:51:29 +0000384 else
385 LM.setKind(LengthModifier::None);
Tom Care3bfc5f42010-06-09 04:11:11 +0000386
387 return true;
388 }
389
Jordan Rose033a9c02012-06-04 22:49:02 +0000390 // If it's an enum, get its underlying type.
391 if (const EnumType *ETy = QT->getAs<EnumType>())
392 QT = ETy->getDecl()->getIntegerType();
393
Tom Care3bfc5f42010-06-09 04:11:11 +0000394 // We can only work with builtin types.
Tom Care3bfc5f42010-06-09 04:11:11 +0000395 const BuiltinType *BT = QT->getAs<BuiltinType>();
John McCall2dde35b2011-10-18 22:28:37 +0000396 if (!BT)
397 return false;
Tom Care876e9942010-06-11 04:22:02 +0000398
Tom Care3bfc5f42010-06-09 04:11:11 +0000399 // Set length modifier
400 switch (BT->getKind()) {
Eli Friedman06d88ec2011-04-27 22:06:20 +0000401 case BuiltinType::Bool:
402 case BuiltinType::WChar_U:
403 case BuiltinType::WChar_S:
404 case BuiltinType::Char16:
405 case BuiltinType::Char32:
406 case BuiltinType::UInt128:
407 case BuiltinType::Int128:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000408 case BuiltinType::Half:
John McCall2dde35b2011-10-18 22:28:37 +0000409 // Various types which are non-trivial to correct.
Eli Friedman06d88ec2011-04-27 22:06:20 +0000410 return false;
411
John McCall2dde35b2011-10-18 22:28:37 +0000412#define SIGNED_TYPE(Id, SingletonId)
413#define UNSIGNED_TYPE(Id, SingletonId)
414#define FLOATING_TYPE(Id, SingletonId)
415#define BUILTIN_TYPE(Id, SingletonId) \
416 case BuiltinType::Id:
417#include "clang/AST/BuiltinTypes.def"
Eli Friedman06d88ec2011-04-27 22:06:20 +0000418 // Misc other stuff which doesn't make sense here.
419 return false;
420
421 case BuiltinType::UInt:
422 case BuiltinType::Int:
423 case BuiltinType::Float:
424 case BuiltinType::Double:
Tom Care876e9942010-06-11 04:22:02 +0000425 LM.setKind(LengthModifier::None);
Tom Care3bfc5f42010-06-09 04:11:11 +0000426 break;
Tom Care876e9942010-06-11 04:22:02 +0000427
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +0000428 case BuiltinType::Char_U:
429 case BuiltinType::UChar:
430 case BuiltinType::Char_S:
431 case BuiltinType::SChar:
432 LM.setKind(LengthModifier::AsChar);
433 break;
434
435 case BuiltinType::Short:
436 case BuiltinType::UShort:
437 LM.setKind(LengthModifier::AsShort);
438 break;
439
Tom Care3bfc5f42010-06-09 04:11:11 +0000440 case BuiltinType::Long:
441 case BuiltinType::ULong:
442 LM.setKind(LengthModifier::AsLong);
443 break;
444
445 case BuiltinType::LongLong:
446 case BuiltinType::ULongLong:
447 LM.setKind(LengthModifier::AsLongLong);
448 break;
449
450 case BuiltinType::LongDouble:
451 LM.setKind(LengthModifier::AsLongDouble);
452 break;
453 }
454
Hans Wennborga7da2152011-10-18 08:10:06 +0000455 // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
Hans Wennborg46847782012-07-27 19:17:46 +0000456 if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus0x))
457 namedTypeToLengthModifier(QT, LM);
Hans Wennborga7da2152011-10-18 08:10:06 +0000458
Hans Wennborgbe6126a2012-02-15 09:59:46 +0000459 // If fixing the length modifier was enough, we are done.
460 const analyze_printf::ArgTypeResult &ATR = getArgType(Ctx, IsObjCLiteral);
461 if (hasValidLengthModifier() && ATR.isValid() && ATR.matchesType(Ctx, QT))
462 return true;
463
Tom Care3bfc5f42010-06-09 04:11:11 +0000464 // Set conversion specifier and disable any flags which do not apply to it.
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +0000465 // Let typedefs to char fall through to int, as %c is silly for uint8_t.
466 if (isa<TypedefType>(QT) && QT->isAnyCharacterType()) {
Ted Kremenek92a6feb2010-07-20 20:03:49 +0000467 CS.setKind(ConversionSpecifier::cArg);
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +0000468 LM.setKind(LengthModifier::None);
Tom Care3bfc5f42010-06-09 04:11:11 +0000469 Precision.setHowSpecified(OptionalAmount::NotSpecified);
470 HasAlternativeForm = 0;
471 HasLeadingZeroes = 0;
Tom Caree4ee9662010-06-17 19:00:27 +0000472 HasPlusPrefix = 0;
Tom Care3bfc5f42010-06-09 04:11:11 +0000473 }
474 // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
Douglas Gregor0c293ea2010-06-22 23:07:26 +0000475 else if (QT->isRealFloatingType()) {
Tom Care3bfc5f42010-06-09 04:11:11 +0000476 CS.setKind(ConversionSpecifier::fArg);
477 }
Tom Care3bfc5f42010-06-09 04:11:11 +0000478 else if (QT->isSignedIntegerType()) {
479 CS.setKind(ConversionSpecifier::dArg);
480 HasAlternativeForm = 0;
481 }
Douglas Gregorc8c4b402010-06-09 05:25:34 +0000482 else if (QT->isUnsignedIntegerType()) {
Hans Wennborgbe6126a2012-02-15 09:59:46 +0000483 CS.setKind(ConversionSpecifier::uArg);
Tom Care3bfc5f42010-06-09 04:11:11 +0000484 HasAlternativeForm = 0;
Tom Caree4ee9662010-06-17 19:00:27 +0000485 HasPlusPrefix = 0;
Chad Rosier30601782011-08-17 23:08:45 +0000486 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +0000487 llvm_unreachable("Unexpected type");
Tom Care3bfc5f42010-06-09 04:11:11 +0000488 }
489
490 return true;
491}
492
Chris Lattner5f9e2722011-07-23 10:55:15 +0000493void PrintfSpecifier::toString(raw_ostream &os) const {
Tom Care3bfc5f42010-06-09 04:11:11 +0000494 // Whilst some features have no defined order, we are using the order
NAKAMURA Takumi00995302011-01-27 07:09:49 +0000495 // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
Tom Care3bfc5f42010-06-09 04:11:11 +0000496 os << "%";
497
498 // Positional args
499 if (usesPositionalArg()) {
500 os << getPositionalArgIndex() << "$";
501 }
502
503 // Conversion flags
504 if (IsLeftJustified) os << "-";
505 if (HasPlusPrefix) os << "+";
506 if (HasSpacePrefix) os << " ";
507 if (HasAlternativeForm) os << "#";
508 if (HasLeadingZeroes) os << "0";
509
510 // Minimum field width
511 FieldWidth.toString(os);
512 // Precision
513 Precision.toString(os);
514 // Length modifier
515 os << LM.toString();
516 // Conversion specifier
517 os << CS.toString();
518}
Tom Caree4ee9662010-06-17 19:00:27 +0000519
Ted Kremenek826a3452010-07-16 02:11:22 +0000520bool PrintfSpecifier::hasValidPlusPrefix() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000521 if (!HasPlusPrefix)
522 return true;
523
524 // The plus prefix only makes sense for signed conversions
525 switch (CS.getKind()) {
526 case ConversionSpecifier::dArg:
527 case ConversionSpecifier::iArg:
528 case ConversionSpecifier::fArg:
529 case ConversionSpecifier::FArg:
530 case ConversionSpecifier::eArg:
531 case ConversionSpecifier::EArg:
532 case ConversionSpecifier::gArg:
533 case ConversionSpecifier::GArg:
534 case ConversionSpecifier::aArg:
535 case ConversionSpecifier::AArg:
536 return true;
537
538 default:
539 return false;
540 }
541}
542
Ted Kremenek826a3452010-07-16 02:11:22 +0000543bool PrintfSpecifier::hasValidAlternativeForm() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000544 if (!HasAlternativeForm)
545 return true;
546
Anders Carlsson2e6c19a2010-11-21 18:34:21 +0000547 // Alternate form flag only valid with the oxXaAeEfFgG conversions
Tom Caree4ee9662010-06-17 19:00:27 +0000548 switch (CS.getKind()) {
549 case ConversionSpecifier::oArg:
550 case ConversionSpecifier::xArg:
Anders Carlsson2e6c19a2010-11-21 18:34:21 +0000551 case ConversionSpecifier::XArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000552 case ConversionSpecifier::aArg:
553 case ConversionSpecifier::AArg:
554 case ConversionSpecifier::eArg:
555 case ConversionSpecifier::EArg:
556 case ConversionSpecifier::fArg:
557 case ConversionSpecifier::FArg:
558 case ConversionSpecifier::gArg:
559 case ConversionSpecifier::GArg:
560 return true;
561
562 default:
563 return false;
564 }
565}
566
Ted Kremenek826a3452010-07-16 02:11:22 +0000567bool PrintfSpecifier::hasValidLeadingZeros() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000568 if (!HasLeadingZeroes)
569 return true;
570
571 // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
572 switch (CS.getKind()) {
573 case ConversionSpecifier::dArg:
574 case ConversionSpecifier::iArg:
575 case ConversionSpecifier::oArg:
576 case ConversionSpecifier::uArg:
577 case ConversionSpecifier::xArg:
578 case ConversionSpecifier::XArg:
579 case ConversionSpecifier::aArg:
580 case ConversionSpecifier::AArg:
581 case ConversionSpecifier::eArg:
582 case ConversionSpecifier::EArg:
583 case ConversionSpecifier::fArg:
584 case ConversionSpecifier::FArg:
585 case ConversionSpecifier::gArg:
586 case ConversionSpecifier::GArg:
587 return true;
588
589 default:
590 return false;
591 }
592}
593
Ted Kremenek826a3452010-07-16 02:11:22 +0000594bool PrintfSpecifier::hasValidSpacePrefix() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000595 if (!HasSpacePrefix)
596 return true;
597
598 // The space prefix only makes sense for signed conversions
599 switch (CS.getKind()) {
600 case ConversionSpecifier::dArg:
601 case ConversionSpecifier::iArg:
602 case ConversionSpecifier::fArg:
603 case ConversionSpecifier::FArg:
604 case ConversionSpecifier::eArg:
605 case ConversionSpecifier::EArg:
606 case ConversionSpecifier::gArg:
607 case ConversionSpecifier::GArg:
608 case ConversionSpecifier::aArg:
609 case ConversionSpecifier::AArg:
610 return true;
611
612 default:
613 return false;
614 }
615}
616
Ted Kremenek826a3452010-07-16 02:11:22 +0000617bool PrintfSpecifier::hasValidLeftJustified() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000618 if (!IsLeftJustified)
619 return true;
620
621 // The left justified flag is valid for all conversions except n
622 switch (CS.getKind()) {
Ted Kremenek35d353b2010-07-20 20:04:10 +0000623 case ConversionSpecifier::nArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000624 return false;
625
626 default:
627 return true;
628 }
629}
630
Ted Kremenek65197b42011-01-08 05:28:46 +0000631bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
632 if (!HasThousandsGrouping)
633 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000634
Ted Kremenek65197b42011-01-08 05:28:46 +0000635 switch (CS.getKind()) {
636 case ConversionSpecifier::dArg:
637 case ConversionSpecifier::iArg:
638 case ConversionSpecifier::uArg:
639 case ConversionSpecifier::fArg:
640 case ConversionSpecifier::FArg:
641 case ConversionSpecifier::gArg:
642 case ConversionSpecifier::GArg:
643 return true;
644 default:
645 return false;
646 }
647}
648
Ted Kremenek826a3452010-07-16 02:11:22 +0000649bool PrintfSpecifier::hasValidPrecision() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000650 if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
651 return true;
652
653 // Precision is only valid with the diouxXaAeEfFgGs conversions
654 switch (CS.getKind()) {
655 case ConversionSpecifier::dArg:
656 case ConversionSpecifier::iArg:
657 case ConversionSpecifier::oArg:
658 case ConversionSpecifier::uArg:
659 case ConversionSpecifier::xArg:
660 case ConversionSpecifier::XArg:
661 case ConversionSpecifier::aArg:
662 case ConversionSpecifier::AArg:
663 case ConversionSpecifier::eArg:
664 case ConversionSpecifier::EArg:
665 case ConversionSpecifier::fArg:
666 case ConversionSpecifier::FArg:
667 case ConversionSpecifier::gArg:
668 case ConversionSpecifier::GArg:
Ted Kremenek99196b12010-07-20 20:03:58 +0000669 case ConversionSpecifier::sArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000670 return true;
671
672 default:
673 return false;
674 }
675}
Ted Kremenek826a3452010-07-16 02:11:22 +0000676bool PrintfSpecifier::hasValidFieldWidth() const {
Tom Caree4ee9662010-06-17 19:00:27 +0000677 if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
678 return true;
679
680 // The field width is valid for all conversions except n
681 switch (CS.getKind()) {
Ted Kremenek35d353b2010-07-20 20:04:10 +0000682 case ConversionSpecifier::nArg:
Tom Caree4ee9662010-06-17 19:00:27 +0000683 return false;
684
685 default:
686 return true;
687 }
688}