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; |
Ted Kremenek | e6ca97f | 2012-01-25 00:04:09 +0000 | [diff] [blame] | 333 | case ConversionSpecifier::ObjCObjArg: |
| 334 | return ArgTypeResult::ObjCPointerTy; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 335 | default: |
| 336 | break; |
| 337 | } |
Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 339 | // FIXME: Handle other cases. |
Ted Kremenek | 40888ad | 2010-01-29 23:00:35 +0000 | [diff] [blame] | 340 | return ArgTypeResult(); |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 343 | bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, |
| 344 | ASTContext &Ctx, bool IsObjCLiteral) { |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 345 | // Handle strings first (char *, wchar_t *) |
| 346 | if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) { |
Ted Kremenek | 99196b1 | 2010-07-20 20:03:58 +0000 | [diff] [blame] | 347 | CS.setKind(ConversionSpecifier::sArg); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 348 | |
Tom Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 349 | // Disable irrelevant flags |
| 350 | HasAlternativeForm = 0; |
| 351 | HasLeadingZeroes = 0; |
| 352 | |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 353 | // Set the long length modifier for wide characters |
| 354 | if (QT->getPointeeType()->isWideCharType()) |
| 355 | LM.setKind(LengthModifier::AsWideChar); |
Hans Wennborg | 117348c | 2011-12-09 10:51:29 +0000 | [diff] [blame] | 356 | else |
| 357 | LM.setKind(LengthModifier::None); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | // We can only work with builtin types. |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 363 | const BuiltinType *BT = QT->getAs<BuiltinType>(); |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 364 | if (!BT) |
| 365 | return false; |
Tom Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 366 | |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 367 | // Set length modifier |
| 368 | switch (BT->getKind()) { |
Eli Friedman | 06d88ec | 2011-04-27 22:06:20 +0000 | [diff] [blame] | 369 | 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 Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 376 | case BuiltinType::Half: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 377 | // Various types which are non-trivial to correct. |
Eli Friedman | 06d88ec | 2011-04-27 22:06:20 +0000 | [diff] [blame] | 378 | return false; |
| 379 | |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 380 | #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 Friedman | 06d88ec | 2011-04-27 22:06:20 +0000 | [diff] [blame] | 386 | // 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 Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 393 | LM.setKind(LengthModifier::None); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 394 | break; |
Tom Care | 876e994 | 2010-06-11 04:22:02 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 396 | 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 Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 408 | 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 Wennborg | a7da215 | 2011-10-18 08:10:06 +0000 | [diff] [blame] | 423 | // 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 Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 440 | // 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 Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 445 | // 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] | 446 | // Let typedefs to char fall through to int, as %c is silly for uint8_t. |
| 447 | if (isa<TypedefType>(QT) && QT->isAnyCharacterType()) { |
Ted Kremenek | 92a6feb | 2010-07-20 20:03:49 +0000 | [diff] [blame] | 448 | CS.setKind(ConversionSpecifier::cArg); |
Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 449 | LM.setKind(LengthModifier::None); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 450 | Precision.setHowSpecified(OptionalAmount::NotSpecified); |
| 451 | HasAlternativeForm = 0; |
| 452 | HasLeadingZeroes = 0; |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 453 | HasPlusPrefix = 0; |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 454 | } |
| 455 | // Test for Floating type first as LongDouble can pass isUnsignedIntegerType |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 456 | else if (QT->isRealFloatingType()) { |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 457 | CS.setKind(ConversionSpecifier::fArg); |
| 458 | } |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 459 | else if (QT->isSignedIntegerType()) { |
| 460 | CS.setKind(ConversionSpecifier::dArg); |
| 461 | HasAlternativeForm = 0; |
| 462 | } |
Douglas Gregor | c8c4b40 | 2010-06-09 05:25:34 +0000 | [diff] [blame] | 463 | else if (QT->isUnsignedIntegerType()) { |
Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 464 | CS.setKind(ConversionSpecifier::uArg); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 465 | HasAlternativeForm = 0; |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 466 | HasPlusPrefix = 0; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 467 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 468 | llvm_unreachable("Unexpected type"); |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return true; |
| 472 | } |
| 473 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 474 | void PrintfSpecifier::toString(raw_ostream &os) const { |
Tom Care | 3bfc5f4 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 475 | // Whilst some features have no defined order, we are using the order |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 476 | // 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] | 477 | 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 Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 501 | bool PrintfSpecifier::hasValidPlusPrefix() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 502 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 524 | bool PrintfSpecifier::hasValidAlternativeForm() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 525 | if (!HasAlternativeForm) |
| 526 | return true; |
| 527 | |
Anders Carlsson | 2e6c19a | 2010-11-21 18:34:21 +0000 | [diff] [blame] | 528 | // Alternate form flag only valid with the oxXaAeEfFgG conversions |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 529 | switch (CS.getKind()) { |
| 530 | case ConversionSpecifier::oArg: |
| 531 | case ConversionSpecifier::xArg: |
Anders Carlsson | 2e6c19a | 2010-11-21 18:34:21 +0000 | [diff] [blame] | 532 | case ConversionSpecifier::XArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 533 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 548 | bool PrintfSpecifier::hasValidLeadingZeros() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 549 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 575 | bool PrintfSpecifier::hasValidSpacePrefix() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 576 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 598 | bool PrintfSpecifier::hasValidLeftJustified() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 599 | if (!IsLeftJustified) |
| 600 | return true; |
| 601 | |
| 602 | // The left justified flag is valid for all conversions except n |
| 603 | switch (CS.getKind()) { |
Ted Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 604 | case ConversionSpecifier::nArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 605 | return false; |
| 606 | |
| 607 | default: |
| 608 | return true; |
| 609 | } |
| 610 | } |
| 611 | |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 612 | bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const { |
| 613 | if (!HasThousandsGrouping) |
| 614 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | |
Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 616 | 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 Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 630 | bool PrintfSpecifier::hasValidPrecision() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 631 | 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 Kremenek | 99196b1 | 2010-07-20 20:03:58 +0000 | [diff] [blame] | 650 | case ConversionSpecifier::sArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 651 | return true; |
| 652 | |
| 653 | default: |
| 654 | return false; |
| 655 | } |
| 656 | } |
Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 657 | bool PrintfSpecifier::hasValidFieldWidth() const { |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 658 | 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 Kremenek | 35d353b | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 663 | case ConversionSpecifier::nArg: |
Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 664 | return false; |
| 665 | |
| 666 | default: |
| 667 | return true; |
| 668 | } |
| 669 | } |