Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1 | //== PrintfFormatString.cpp - Analysis of printf format strings --*- C++ -*-==// |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 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 | // 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Analyses/FormatString.h" |
| 16 | #include "FormatStringParsing.h" |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 17 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 18 | using clang::analyze_format_string::ArgTypeResult; |
| 19 | using clang::analyze_format_string::FormatStringHandler; |
| 20 | using clang::analyze_format_string::LengthModifier; |
| 21 | using clang::analyze_format_string::OptionalAmount; |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 22 | using clang::analyze_format_string::ConversionSpecifier; |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 23 | using clang::analyze_printf::PrintfSpecifier; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 24 | |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 25 | using namespace clang; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 27 | typedef clang::analyze_format_string::SpecifierResult<PrintfSpecifier> |
| 28 | PrintfSpecifierResult; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Methods for parsing format strings. |
| 32 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 34 | using analyze_format_string::ParseNonPositionAmount; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 36 | static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS, |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 37 | const char *Start, const char *&Beg, const char *E, |
| 38 | unsigned *argIndex) { |
| 39 | if (argIndex) { |
| 40 | FS.setPrecision(ParseNonPositionAmount(Beg, E, *argIndex)); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 41 | } else { |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 42 | const OptionalAmount Amt = ParsePositionAmount(H, Start, Beg, E, |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 43 | analyze_format_string::PrecisionPos); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 44 | if (Amt.isInvalid()) |
| 45 | return true; |
| 46 | FS.setPrecision(Amt); |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 51 | static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H, |
Ted Kremenek | 74d56a1 | 2010-02-04 20:46:58 +0000 | [diff] [blame] | 52 | const char *&Beg, |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 53 | const char *E, |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 54 | unsigned &argIndex, |
| 55 | const LangOptions &LO) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 57 | using namespace clang::analyze_format_string; |
Ted Kremenek | 4b220fa | 2010-01-29 02:13:53 +0000 | [diff] [blame] | 58 | using namespace clang::analyze_printf; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 60 | const char *I = Beg; |
Ted Kremenek | c7ae51a | 2010-01-28 00:02:05 +0000 | [diff] [blame] | 61 | const char *Start = 0; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 62 | UpdateOnReturn <const char*> UpdateBeg(Beg, I); |
| 63 | |
| 64 | // Look for a '%' character that indicates the start of a format specifier. |
Ted Kremenek | e729acb | 2010-01-28 23:56:52 +0000 | [diff] [blame] | 65 | for ( ; I != E ; ++I) { |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 66 | char c = *I; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 67 | if (c == '\0') { |
| 68 | // Detect spurious null characters, which are likely errors. |
| 69 | H.HandleNullChar(I); |
| 70 | return true; |
| 71 | } |
| 72 | if (c == '%') { |
Ted Kremenek | e729acb | 2010-01-28 23:56:52 +0000 | [diff] [blame] | 73 | Start = I++; // Record the start of the format specifier. |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 78 | // No format specifier found? |
| 79 | if (!Start) |
| 80 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 82 | if (I == E) { |
| 83 | // No more characters left? |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 84 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 88 | PrintfSpecifier FS; |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 89 | if (ParseArgPosition(H, FS, Start, I, E)) |
| 90 | return true; |
| 91 | |
| 92 | if (I == E) { |
| 93 | // No more characters left? |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 94 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 98 | // Look for flags (if any). |
| 99 | bool hasMore = true; |
| 100 | for ( ; I != E; ++I) { |
| 101 | switch (*I) { |
| 102 | default: hasMore = false; break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 103 | case '\'': |
Ted Kremenek | bd18d45 | 2011-01-08 05:28:38 +0000 | [diff] [blame] | 104 | // FIXME: POSIX specific. Always accept? |
| 105 | FS.setHasThousandsGrouping(I); |
| 106 | break; |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 107 | 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 Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 112 | } |
| 113 | if (!hasMore) |
| 114 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 115 | } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 116 | |
| 117 | if (I == E) { |
| 118 | // No more characters left? |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 119 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 123 | // Look for the field width (if any). |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 124 | if (ParseFieldWidth(H, FS, Start, I, E, |
| 125 | FS.usesPositionalArg() ? 0 : &argIndex)) |
| 126 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 128 | if (I == E) { |
| 129 | // No more characters left? |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 130 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 131 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Look for the precision (if any). |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 135 | if (*I == '.') { |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 136 | ++I; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 137 | if (I == E) { |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 138 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 139 | return true; |
| 140 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 142 | if (ParsePrecision(H, FS, Start, I, E, |
| 143 | FS.usesPositionalArg() ? 0 : &argIndex)) |
| 144 | return true; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 145 | |
| 146 | if (I == E) { |
| 147 | // No more characters left? |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 148 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Look for the length modifier. |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 154 | if (ParseLengthModifier(FS, I, E, LO) && I == E) { |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 155 | // No more characters left? |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 156 | H.HandleIncompleteSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
Ted Kremenek | df17f9d | 2010-02-09 00:04:09 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 160 | if (*I == '\0') { |
Ted Kremenek | df17f9d | 2010-02-09 00:04:09 +0000 | [diff] [blame] | 161 | // Detect spurious null characters, which are likely errors. |
| 162 | H.HandleNullChar(I); |
| 163 | return true; |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 164 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 166 | // Finally, look for the conversion specifier. |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 167 | const char *conversionPosition = I++; |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 168 | ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 169 | switch (*conversionPosition) { |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 170 | default: |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 171 | break; |
Ted Kremenek | c7cbb9b | 2010-01-28 00:55:28 +0000 | [diff] [blame] | 172 | // C99: 7.19.6.1 (section 8). |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 173 | 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 Kremenek | 92a6feb | 2010-07-20 20:03:49 +0000 | [diff] [blame] | 180 | case 'c': k = ConversionSpecifier::cArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 181 | case 'd': k = ConversionSpecifier::dArg; break; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 182 | case 'e': k = ConversionSpecifier::eArg; break; |
| 183 | case 'f': k = ConversionSpecifier::fArg; break; |
| 184 | case 'g': k = ConversionSpecifier::gArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 185 | case 'i': k = ConversionSpecifier::iArg; break; |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 186 | case 'n': k = ConversionSpecifier::nArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 187 | case 'o': k = ConversionSpecifier::oArg; break; |
Ted Kremenek | 6693205 | 2010-07-20 20:04:04 +0000 | [diff] [blame] | 188 | case 'p': k = ConversionSpecifier::pArg; break; |
Ted Kremenek | 99196b1 | 2010-07-20 20:03:58 +0000 | [diff] [blame] | 189 | case 's': k = ConversionSpecifier::sArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 190 | case 'u': k = ConversionSpecifier::uArg; break; |
| 191 | case 'x': k = ConversionSpecifier::xArg; break; |
Ted Kremenek | bd18d45 | 2011-01-08 05:28:38 +0000 | [diff] [blame] | 192 | // POSIX specific. |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 193 | case 'C': k = ConversionSpecifier::CArg; break; |
Ted Kremenek | de183a4 | 2010-07-20 20:04:15 +0000 | [diff] [blame] | 194 | case 'S': k = ConversionSpecifier::SArg; break; |
Ted Kremenek | c7cbb9b | 2010-01-28 00:55:28 +0000 | [diff] [blame] | 195 | // Objective-C. |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 196 | case '@': k = ConversionSpecifier::ObjCObjArg; break; |
Ted Kremenek | df17f9d | 2010-02-09 00:04:09 +0000 | [diff] [blame] | 197 | // Glibc specific. |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 198 | case 'm': k = ConversionSpecifier::PrintErrno; break; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 200 | PrintfConversionSpecifier CS(conversionPosition, k); |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 201 | FS.setConversionSpecifier(CS); |
Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 202 | if (CS.consumesDataArgument() && !FS.usesPositionalArg()) |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 203 | FS.setArgIndex(argIndex++); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 204 | |
| 205 | if (k == ConversionSpecifier::InvalidSpecifier) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 206 | // Assume the conversion takes one argument. |
Ted Kremenek | 7966297 | 2010-12-02 20:57:51 +0000 | [diff] [blame] | 207 | return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, I - Start); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 208 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 209 | return PrintfSpecifierResult(Start, FS); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 212 | bool clang::analyze_format_string::ParsePrintfString(FormatStringHandler &H, |
| 213 | const char *I, |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 214 | const char *E, |
| 215 | const LangOptions &LO) { |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 216 | |
| 217 | unsigned argIndex = 0; |
| 218 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 219 | // Keep looking for a format specifier until we have exhausted the string. |
| 220 | while (I != E) { |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 221 | const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex, |
| 222 | LO); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 223 | // 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 Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 226 | return true;; |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 227 | // Did we exhaust the string or encounter an error that |
| 228 | // we can recover from? |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 229 | if (!FSR.hasValue()) |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 230 | continue; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 231 | // We have a format specifier. Pass it to the callback. |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 232 | if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(), |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 233 | I - FSR.getStart())) |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 234 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 235 | } |
| 236 | assert(I == E && "Format string not exhausted"); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 240 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 241 | // Methods on PrintfSpecifier. |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 242 | //===----------------------------------------------------------------------===// |
| 243 | |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 244 | ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx, |
| 245 | bool IsObjCLiteral) const { |
Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 246 | const PrintfConversionSpecifier &CS = getConversionSpecifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 248 | if (!CS.consumesDataArgument()) |
| 249 | return ArgTypeResult::Invalid(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | 9325eaf | 2010-08-24 22:24:51 +0000 | [diff] [blame] | 251 | if (CS.getKind() == ConversionSpecifier::cArg) |
| 252 | switch (LM.getKind()) { |
| 253 | case LengthModifier::None: return Ctx.IntTy; |
Hans Wennborg | f4f0c60 | 2011-12-09 12:22:12 +0000 | [diff] [blame] | 254 | case LengthModifier::AsLong: |
| 255 | return ArgTypeResult(ArgTypeResult::WIntTy, "wint_t"); |
Ted Kremenek | 9325eaf | 2010-08-24 22:24:51 +0000 | [diff] [blame] | 256 | default: |
| 257 | return ArgTypeResult::Invalid(); |
| 258 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 260 | if (CS.isIntArg()) |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 261 | switch (LM.getKind()) { |
| 262 | case LengthModifier::AsLongDouble: |
Ted Kremenek | 9d24c2c | 2012-01-24 21:29:54 +0000 | [diff] [blame] | 263 | // GNU extension. |
| 264 | return Ctx.LongLongTy; |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 265 | case LengthModifier::None: return Ctx.IntTy; |
Ted Kremenek | 6ca4a9a | 2011-10-25 04:20:41 +0000 | [diff] [blame] | 266 | case LengthModifier::AsChar: return ArgTypeResult::AnyCharTy; |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 267 | case LengthModifier::AsShort: return Ctx.ShortTy; |
| 268 | case LengthModifier::AsLong: return Ctx.LongTy; |
Hans Wennborg | 32addd5 | 2012-02-16 16:34:54 +0000 | [diff] [blame] | 269 | case LengthModifier::AsLongLong: |
| 270 | case LengthModifier::AsQuad: |
| 271 | return Ctx.LongLongTy; |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 272 | case LengthModifier::AsIntMax: |
| 273 | return ArgTypeResult(Ctx.getIntMaxType(), "intmax_t"); |
Hans Wennborg | 29e97cb | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 274 | case LengthModifier::AsSizeT: |
| 275 | // FIXME: How to get the corresponding signed version of size_t? |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 276 | return ArgTypeResult(); |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 277 | case LengthModifier::AsPtrDiff: |
| 278 | return ArgTypeResult(Ctx.getPointerDiffType(), "ptrdiff_t"); |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 279 | case LengthModifier::AsAllocate: |
Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame] | 280 | case LengthModifier::AsMAllocate: |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 281 | return ArgTypeResult::Invalid(); |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (CS.isUIntArg()) |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 285 | switch (LM.getKind()) { |
| 286 | case LengthModifier::AsLongDouble: |
Ted Kremenek | 9d24c2c | 2012-01-24 21:29:54 +0000 | [diff] [blame] | 287 | // GNU extension. |
| 288 | return Ctx.UnsignedLongLongTy; |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 289 | 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 Wennborg | 32addd5 | 2012-02-16 16:34:54 +0000 | [diff] [blame] | 293 | case LengthModifier::AsLongLong: |
| 294 | case LengthModifier::AsQuad: |
| 295 | return Ctx.UnsignedLongLongTy; |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 296 | case LengthModifier::AsIntMax: |
| 297 | return ArgTypeResult(Ctx.getUIntMaxType(), "uintmax_t"); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 298 | case LengthModifier::AsSizeT: |
Hans Wennborg | a792aff | 2011-12-07 10:33:11 +0000 | [diff] [blame] | 299 | return ArgTypeResult(Ctx.getSizeType(), "size_t"); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 300 | case LengthModifier::AsPtrDiff: |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 301 | // FIXME: How to get the corresponding unsigned |
| 302 | // version of ptrdiff_t? |
| 303 | return ArgTypeResult(); |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 304 | case LengthModifier::AsAllocate: |
Hans Wennborg | 37969b7 | 2012-01-12 17:11:12 +0000 | [diff] [blame] | 305 | case LengthModifier::AsMAllocate: |
Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 306 | return ArgTypeResult::Invalid(); |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 307 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | f911eba | 2010-02-01 23:23:50 +0000 | [diff] [blame] | 309 | if (CS.isDoubleArg()) { |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 310 | if (LM.getKind() == LengthModifier::AsLongDouble) |
Ted Kremenek | f911eba | 2010-02-01 23:23:50 +0000 | [diff] [blame] | 311 | return Ctx.LongDoubleTy; |
Ted Kremenek | c9a89fe | 2010-01-30 01:02:18 +0000 | [diff] [blame] | 312 | return Ctx.DoubleTy; |
Ted Kremenek | f911eba | 2010-02-01 23:23:50 +0000 | [diff] [blame] | 313 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 315 | switch (CS.getKind()) { |
Hans Wennborg | 6308218 | 2011-12-09 15:57:33 +0000 | [diff] [blame] | 316 | case ConversionSpecifier::sArg: |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 317 | if (LM.getKind() == LengthModifier::AsWideChar) { |
| 318 | if (IsObjCLiteral) |
| 319 | return Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()); |
Hans Wennborg | f4f0c60 | 2011-12-09 12:22:12 +0000 | [diff] [blame] | 320 | return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *"); |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 321 | } |
Hans Wennborg | f4f0c60 | 2011-12-09 12:22:12 +0000 | [diff] [blame] | 322 | return ArgTypeResult::CStrTy; |
Ted Kremenek | de183a4 | 2010-07-20 20:04:15 +0000 | [diff] [blame] | 323 | case ConversionSpecifier::SArg: |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 324 | if (IsObjCLiteral) |
| 325 | return Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()); |
Hans Wennborg | f4f0c60 | 2011-12-09 12:22:12 +0000 | [diff] [blame] | 326 | return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *"); |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 327 | case ConversionSpecifier::CArg: |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 328 | if (IsObjCLiteral) |
| 329 | return Ctx.UnsignedShortTy; |
Hans Wennborg | f4f0c60 | 2011-12-09 12:22:12 +0000 | [diff] [blame] | 330 | return ArgTypeResult(Ctx.WCharTy, "wchar_t"); |
Ted Kremenek | 6693205 | 2010-07-20 20:04:04 +0000 | [diff] [blame] | 331 | case ConversionSpecifier::pArg: |
Ted Kremenek | 13927a4 | 2010-06-16 21:23:04 +0000 | [diff] [blame] | 332 | return ArgTypeResult::CPointerTy; |
Hans Wennborg | cec9ce4 | 2012-07-30 17:11:32 +0000 | [diff] [blame] | 333 | case ConversionSpecifier::nArg: |
| 334 | return Ctx.getPointerType(Ctx.IntTy); |
Ted Kremenek | e6ca97f | 2012-01-25 00:04:09 +0000 | [diff] [blame] | 335 | case ConversionSpecifier::ObjCObjArg: |
| 336 | return ArgTypeResult::ObjCPointerTy; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 337 | default: |
| 338 | break; |
| 339 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 341 | // FIXME: Handle other cases. |
Ted Kremenek | 40888ad | 2010-01-29 23:00:35 +0000 | [diff] [blame] | 342 | return ArgTypeResult(); |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 345 | bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, |
| 346 | ASTContext &Ctx, bool IsObjCLiteral) { |
Hans Wennborg | cec9ce4 | 2012-07-30 17:11:32 +0000 | [diff] [blame] | 347 | // %n is different from other conversion specifiers; don't try to fix it. |
| 348 | if (CS.getKind() == ConversionSpecifier::nArg) |
| 349 | return false; |
| 350 | |
Jordan Rose | 153acdb | 2012-05-30 21:53:13 +0000 | [diff] [blame] | 351 | // 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 Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 374 | if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) { |
Ted Kremenek | 99196b1 | 2010-07-20 20:03:58 +0000 | [diff] [blame] | 375 | CS.setKind(ConversionSpecifier::sArg); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 376 | |
Tom Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 377 | // Disable irrelevant flags |
| 378 | HasAlternativeForm = 0; |
| 379 | HasLeadingZeroes = 0; |
| 380 | |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 381 | // Set the long length modifier for wide characters |
| 382 | if (QT->getPointeeType()->isWideCharType()) |
| 383 | LM.setKind(LengthModifier::AsWideChar); |
Hans Wennborg | 117348c | 2011-12-09 10:51:29 +0000 | [diff] [blame] | 384 | else |
| 385 | LM.setKind(LengthModifier::None); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 386 | |
| 387 | return true; |
| 388 | } |
| 389 | |
Jordan Rose | 033a9c0 | 2012-06-04 22:49:02 +0000 | [diff] [blame] | 390 | // If it's an enum, get its underlying type. |
| 391 | if (const EnumType *ETy = QT->getAs<EnumType>()) |
| 392 | QT = ETy->getDecl()->getIntegerType(); |
| 393 | |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 394 | // We can only work with builtin types. |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 395 | const BuiltinType *BT = QT->getAs<BuiltinType>(); |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 396 | if (!BT) |
| 397 | return false; |
Tom Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 398 | |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 399 | // Set length modifier |
| 400 | switch (BT->getKind()) { |
Eli Friedman | 06d88ec | 2011-04-27 22:06:20 +0000 | [diff] [blame] | 401 | 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 Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 408 | case BuiltinType::Half: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 409 | // Various types which are non-trivial to correct. |
Eli Friedman | 06d88ec | 2011-04-27 22:06:20 +0000 | [diff] [blame] | 410 | return false; |
| 411 | |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 412 | #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 Friedman | 06d88ec | 2011-04-27 22:06:20 +0000 | [diff] [blame] | 418 | // 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 Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 425 | LM.setKind(LengthModifier::None); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 426 | break; |
Tom Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 428 | 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 Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 440 | 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 Wennborg | a7da215 | 2011-10-18 08:10:06 +0000 | [diff] [blame] | 455 | // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99. |
Hans Wennborg | 4684778 | 2012-07-27 19:17:46 +0000 | [diff] [blame] | 456 | if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus0x)) |
| 457 | namedTypeToLengthModifier(QT, LM); |
Hans Wennborg | a7da215 | 2011-10-18 08:10:06 +0000 | [diff] [blame] | 458 | |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 459 | // 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 Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 464 | // Set conversion specifier and disable any flags which do not apply to it. |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 465 | // Let typedefs to char fall through to int, as %c is silly for uint8_t. |
| 466 | if (isa<TypedefType>(QT) && QT->isAnyCharacterType()) { |
Ted Kremenek | 92a6feb | 2010-07-20 20:03:49 +0000 | [diff] [blame] | 467 | CS.setKind(ConversionSpecifier::cArg); |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 468 | LM.setKind(LengthModifier::None); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 469 | Precision.setHowSpecified(OptionalAmount::NotSpecified); |
| 470 | HasAlternativeForm = 0; |
| 471 | HasLeadingZeroes = 0; |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 472 | HasPlusPrefix = 0; |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 473 | } |
| 474 | // Test for Floating type first as LongDouble can pass isUnsignedIntegerType |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 475 | else if (QT->isRealFloatingType()) { |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 476 | CS.setKind(ConversionSpecifier::fArg); |
| 477 | } |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 478 | else if (QT->isSignedIntegerType()) { |
| 479 | CS.setKind(ConversionSpecifier::dArg); |
| 480 | HasAlternativeForm = 0; |
| 481 | } |
Douglas Gregor | c8c4b40 | 2010-06-09 05:25:34 +0000 | [diff] [blame] | 482 | else if (QT->isUnsignedIntegerType()) { |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 483 | CS.setKind(ConversionSpecifier::uArg); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 484 | HasAlternativeForm = 0; |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 485 | HasPlusPrefix = 0; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 486 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 487 | llvm_unreachable("Unexpected type"); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | return true; |
| 491 | } |
| 492 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 493 | void PrintfSpecifier::toString(raw_ostream &os) const { |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 494 | // Whilst some features have no defined order, we are using the order |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 495 | // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1) |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 496 | 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 Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 520 | bool PrintfSpecifier::hasValidPlusPrefix() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 521 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 543 | bool PrintfSpecifier::hasValidAlternativeForm() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 544 | if (!HasAlternativeForm) |
| 545 | return true; |
| 546 | |
Anders Carlsson | 2e6c19a | 2010-11-21 18:34:21 +0000 | [diff] [blame] | 547 | // Alternate form flag only valid with the oxXaAeEfFgG conversions |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 548 | switch (CS.getKind()) { |
| 549 | case ConversionSpecifier::oArg: |
| 550 | case ConversionSpecifier::xArg: |
Anders Carlsson | 2e6c19a | 2010-11-21 18:34:21 +0000 | [diff] [blame] | 551 | case ConversionSpecifier::XArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 552 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 567 | bool PrintfSpecifier::hasValidLeadingZeros() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 568 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 594 | bool PrintfSpecifier::hasValidSpacePrefix() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 595 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 617 | bool PrintfSpecifier::hasValidLeftJustified() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 618 | if (!IsLeftJustified) |
| 619 | return true; |
| 620 | |
| 621 | // The left justified flag is valid for all conversions except n |
| 622 | switch (CS.getKind()) { |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 623 | case ConversionSpecifier::nArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 624 | return false; |
| 625 | |
| 626 | default: |
| 627 | return true; |
| 628 | } |
| 629 | } |
| 630 | |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 631 | bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const { |
| 632 | if (!HasThousandsGrouping) |
| 633 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 635 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 649 | bool PrintfSpecifier::hasValidPrecision() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 650 | 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 Kremenek | 99196b1 | 2010-07-20 20:03:58 +0000 | [diff] [blame] | 669 | case ConversionSpecifier::sArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 670 | return true; |
| 671 | |
| 672 | default: |
| 673 | return false; |
| 674 | } |
| 675 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 676 | bool PrintfSpecifier::hasValidFieldWidth() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 677 | 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 Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 682 | case ConversionSpecifier::nArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 683 | return false; |
| 684 | |
| 685 | default: |
| 686 | return true; |
| 687 | } |
| 688 | } |