Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 1 | //= PrintfFormatStrings.cpp - Analysis of printf format strings --*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // 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 | |
| 15 | #include "clang/Analysis/Analyses/PrintfFormatString.h" |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 17 | |
Ted Kremenek | 4b220fa | 2010-01-29 02:13:53 +0000 | [diff] [blame] | 18 | using clang::analyze_printf::FormatSpecifier; |
| 19 | using clang::analyze_printf::OptionalAmount; |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 20 | using clang::analyze_printf::ArgTypeResult; |
Ted Kremenek | 74d56a1 | 2010-02-04 20:46:58 +0000 | [diff] [blame] | 21 | using clang::analyze_printf::FormatStringHandler; |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 22 | using namespace clang; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | class FormatSpecifierResult { |
| 26 | FormatSpecifier FS; |
| 27 | const char *Start; |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 28 | bool Stop; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 29 | public: |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 30 | FormatSpecifierResult(bool stop = false) |
| 31 | : Start(0), Stop(stop) {} |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 32 | FormatSpecifierResult(const char *start, |
Ted Kremenek | d2dcece | 2010-01-28 02:02:59 +0000 | [diff] [blame] | 33 | const FormatSpecifier &fs) |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 34 | : FS(fs), Start(start), Stop(false) {} |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 37 | const char *getStart() const { return Start; } |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 38 | bool shouldStop() const { return Stop; } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 39 | bool hasValue() const { return Start != 0; } |
| 40 | const FormatSpecifier &getValue() const { |
| 41 | assert(hasValue()); |
| 42 | return FS; |
| 43 | } |
Ted Kremenek | d2dcece | 2010-01-28 02:02:59 +0000 | [diff] [blame] | 44 | const FormatSpecifier &getValue() { return FS; } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 45 | }; |
| 46 | } // end anonymous namespace |
| 47 | |
| 48 | template <typename T> |
| 49 | class UpdateOnReturn { |
| 50 | T &ValueToUpdate; |
| 51 | const T &ValueToCopy; |
| 52 | public: |
| 53 | UpdateOnReturn(T &valueToUpdate, const T &valueToCopy) |
| 54 | : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {} |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 56 | ~UpdateOnReturn() { |
| 57 | ValueToUpdate = ValueToCopy; |
| 58 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | // Methods for parsing format strings. |
| 63 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 64 | |
| 65 | static OptionalAmount ParseAmount(const char *&Beg, const char *E) { |
| 66 | const char *I = Beg; |
| 67 | UpdateOnReturn <const char*> UpdateBeg(Beg, I); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 69 | bool foundDigits = false; |
| 70 | unsigned accumulator = 0; |
| 71 | |
| 72 | for ( ; I != E; ++I) { |
| 73 | char c = *I; |
| 74 | if (c >= '0' && c <= '9') { |
| 75 | foundDigits = true; |
| 76 | accumulator += (accumulator * 10) + (c - '0'); |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | if (foundDigits) |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 81 | return OptionalAmount(accumulator, Beg); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 83 | if (c == '*') { |
| 84 | ++I; |
| 85 | return OptionalAmount(OptionalAmount::Arg, Beg); |
| 86 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 90 | |
| 91 | return OptionalAmount(); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 94 | static FormatSpecifierResult ParseFormatSpecifier(FormatStringHandler &H, |
Ted Kremenek | 74d56a1 | 2010-02-04 20:46:58 +0000 | [diff] [blame] | 95 | const char *&Beg, |
| 96 | const char *E) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 4b220fa | 2010-01-29 02:13:53 +0000 | [diff] [blame] | 98 | using namespace clang::analyze_printf; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 100 | const char *I = Beg; |
Ted Kremenek | c7ae51a | 2010-01-28 00:02:05 +0000 | [diff] [blame] | 101 | const char *Start = 0; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 102 | UpdateOnReturn <const char*> UpdateBeg(Beg, I); |
| 103 | |
| 104 | // Look for a '%' character that indicates the start of a format specifier. |
Ted Kremenek | e729acb | 2010-01-28 23:56:52 +0000 | [diff] [blame] | 105 | for ( ; I != E ; ++I) { |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 106 | char c = *I; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 107 | if (c == '\0') { |
| 108 | // Detect spurious null characters, which are likely errors. |
| 109 | H.HandleNullChar(I); |
| 110 | return true; |
| 111 | } |
| 112 | if (c == '%') { |
Ted Kremenek | e729acb | 2010-01-28 23:56:52 +0000 | [diff] [blame] | 113 | Start = I++; // Record the start of the format specifier. |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 118 | // No format specifier found? |
| 119 | if (!Start) |
| 120 | return false; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 122 | if (I == E) { |
| 123 | // No more characters left? |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 124 | H.HandleIncompleteFormatSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 125 | return true; |
| 126 | } |
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 | FormatSpecifier FS; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 130 | // Look for flags (if any). |
| 131 | bool hasMore = true; |
| 132 | for ( ; I != E; ++I) { |
| 133 | switch (*I) { |
| 134 | default: hasMore = false; break; |
| 135 | case '-': FS.setIsLeftJustified(); break; |
| 136 | case '+': FS.setHasPlusPrefix(); break; |
| 137 | case ' ': FS.setHasSpacePrefix(); break; |
| 138 | case '#': FS.setHasAlternativeForm(); break; |
| 139 | case '0': FS.setHasLeadingZeros(); break; |
| 140 | } |
| 141 | if (!hasMore) |
| 142 | break; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 143 | } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 144 | |
| 145 | if (I == E) { |
| 146 | // No more characters left? |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 147 | H.HandleIncompleteFormatSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 148 | return true; |
| 149 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 151 | // Look for the field width (if any). |
| 152 | FS.setFieldWidth(ParseAmount(I, E)); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 154 | if (I == E) { |
| 155 | // No more characters left? |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 156 | H.HandleIncompleteFormatSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 157 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Look for the precision (if any). |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 161 | if (*I == '.') { |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 162 | ++I; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 163 | if (I == E) { |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 164 | H.HandleIncompleteFormatSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 165 | return true; |
| 166 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 168 | FS.setPrecision(ParseAmount(I, E)); |
| 169 | |
| 170 | if (I == E) { |
| 171 | // No more characters left? |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 172 | H.HandleIncompleteFormatSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Look for the length modifier. |
| 178 | LengthModifier lm = None; |
| 179 | switch (*I) { |
| 180 | default: |
| 181 | break; |
| 182 | case 'h': |
| 183 | ++I; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 184 | lm = (I != E && *I == 'h') ? ++I, AsChar : AsShort; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 185 | break; |
| 186 | case 'l': |
| 187 | ++I; |
| 188 | lm = (I != E && *I == 'l') ? ++I, AsLongLong : AsLong; |
| 189 | break; |
| 190 | case 'j': lm = AsIntMax; ++I; break; |
| 191 | case 'z': lm = AsSizeT; ++I; break; |
| 192 | case 't': lm = AsPtrDiff; ++I; break; |
| 193 | case 'L': lm = AsLongDouble; ++I; break; |
Daniel Dunbar | 01aefc6 | 2010-01-30 15:49:20 +0000 | [diff] [blame] | 194 | case 'q': lm = AsLongLong; ++I; break; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 195 | } |
| 196 | FS.setLengthModifier(lm); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 198 | if (I == E) { |
| 199 | // No more characters left? |
Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 200 | H.HandleIncompleteFormatSpecifier(Start, E - Start); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 201 | return true; |
| 202 | } |
Ted Kremenek | df17f9d | 2010-02-09 00:04:09 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 204 | if (*I == '\0') { |
Ted Kremenek | df17f9d | 2010-02-09 00:04:09 +0000 | [diff] [blame] | 205 | // Detect spurious null characters, which are likely errors. |
| 206 | H.HandleNullChar(I); |
| 207 | return true; |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 208 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 210 | // Finally, look for the conversion specifier. |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 211 | const char *conversionPosition = I++; |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 212 | ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 213 | switch (*conversionPosition) { |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 214 | default: |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 215 | break; |
Ted Kremenek | c7cbb9b | 2010-01-28 00:55:28 +0000 | [diff] [blame] | 216 | // C99: 7.19.6.1 (section 8). |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 217 | case '%': k = ConversionSpecifier::PercentArg; break; |
| 218 | case 'A': k = ConversionSpecifier::AArg; break; |
| 219 | case 'E': k = ConversionSpecifier::EArg; break; |
| 220 | case 'F': k = ConversionSpecifier::FArg; break; |
| 221 | case 'G': k = ConversionSpecifier::GArg; break; |
| 222 | case 'X': k = ConversionSpecifier::XArg; break; |
| 223 | case 'a': k = ConversionSpecifier::aArg; break; |
| 224 | case 'c': k = ConversionSpecifier::IntAsCharArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 225 | case 'd': k = ConversionSpecifier::dArg; break; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 226 | case 'e': k = ConversionSpecifier::eArg; break; |
| 227 | case 'f': k = ConversionSpecifier::fArg; break; |
| 228 | case 'g': k = ConversionSpecifier::gArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 229 | case 'i': k = ConversionSpecifier::iArg; break; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 230 | case 'n': k = ConversionSpecifier::OutIntPtrArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 231 | case 'o': k = ConversionSpecifier::oArg; break; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 232 | case 'p': k = ConversionSpecifier::VoidPtrArg; break; |
| 233 | case 's': k = ConversionSpecifier::CStrArg; break; |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 234 | case 'u': k = ConversionSpecifier::uArg; break; |
| 235 | case 'x': k = ConversionSpecifier::xArg; break; |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 236 | // Mac OS X (unicode) specific |
| 237 | case 'C': k = ConversionSpecifier::CArg; break; |
| 238 | case 'S': k = ConversionSpecifier::UnicodeStrArg; break; |
Ted Kremenek | c7cbb9b | 2010-01-28 00:55:28 +0000 | [diff] [blame] | 239 | // Objective-C. |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 240 | case '@': k = ConversionSpecifier::ObjCObjArg; break; |
Ted Kremenek | df17f9d | 2010-02-09 00:04:09 +0000 | [diff] [blame] | 241 | // Glibc specific. |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 242 | case 'm': k = ConversionSpecifier::PrintErrno; break; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 243 | } |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 244 | FS.setConversionSpecifier(ConversionSpecifier(conversionPosition, k)); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 245 | |
| 246 | if (k == ConversionSpecifier::InvalidSpecifier) { |
| 247 | H.HandleInvalidConversionSpecifier(FS, Beg, I - Beg); |
| 248 | return false; // Keep processing format specifiers. |
| 249 | } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 250 | return FormatSpecifierResult(Start, FS); |
| 251 | } |
| 252 | |
Ted Kremenek | 74d56a1 | 2010-02-04 20:46:58 +0000 | [diff] [blame] | 253 | bool clang::analyze_printf::ParseFormatString(FormatStringHandler &H, |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 254 | const char *I, const char *E) { |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 255 | // Keep looking for a format specifier until we have exhausted the string. |
| 256 | while (I != E) { |
| 257 | const FormatSpecifierResult &FSR = ParseFormatSpecifier(H, I, E); |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 258 | // Did a fail-stop error of any kind occur when parsing the specifier? |
| 259 | // If so, don't do any more processing. |
| 260 | if (FSR.shouldStop()) |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 261 | return true;; |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 262 | // Did we exhaust the string or encounter an error that |
| 263 | // we can recover from? |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 264 | if (!FSR.hasValue()) |
Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 265 | continue; |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 266 | // We have a format specifier. Pass it to the callback. |
Ted Kremenek | a8d8fec | 2010-01-28 02:46:17 +0000 | [diff] [blame] | 267 | if (!H.HandleFormatSpecifier(FSR.getValue(), FSR.getStart(), |
| 268 | I - FSR.getStart())) |
Ted Kremenek | 4dcb18f | 2010-01-29 20:29:53 +0000 | [diff] [blame] | 269 | return true; |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 270 | } |
| 271 | assert(I == E && "Format string not exhausted"); |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 272 | return false; |
| 273 | } |
Ted Kremenek | 8f0a1c7 | 2010-01-27 23:43:25 +0000 | [diff] [blame] | 274 | |
| 275 | FormatStringHandler::~FormatStringHandler() {} |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 276 | |
| 277 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 278 | // Methods on ArgTypeResult. |
| 279 | //===----------------------------------------------------------------------===// |
| 280 | |
| 281 | bool ArgTypeResult::matchesType(ASTContext &C, QualType argTy) const { |
| 282 | assert(isValid()); |
| 283 | |
| 284 | if (K == UnknownTy) |
| 285 | return true; |
| 286 | |
| 287 | if (K == SpecificTy) { |
| 288 | argTy = C.getCanonicalType(argTy).getUnqualifiedType(); |
| 289 | |
| 290 | if (T == argTy) |
| 291 | return true; |
| 292 | |
| 293 | if (const BuiltinType *BT = argTy->getAs<BuiltinType>()) |
| 294 | switch (BT->getKind()) { |
| 295 | default: |
| 296 | break; |
| 297 | case BuiltinType::Char_S: |
| 298 | case BuiltinType::SChar: |
| 299 | return T == C.UnsignedCharTy; |
| 300 | case BuiltinType::Char_U: |
| 301 | case BuiltinType::UChar: |
| 302 | return T == C.SignedCharTy; |
| 303 | case BuiltinType::Short: |
| 304 | return T == C.UnsignedShortTy; |
| 305 | case BuiltinType::UShort: |
| 306 | return T == C.ShortTy; |
| 307 | case BuiltinType::Int: |
| 308 | return T == C.UnsignedIntTy; |
| 309 | case BuiltinType::UInt: |
| 310 | return T == C.IntTy; |
| 311 | case BuiltinType::Long: |
| 312 | return T == C.UnsignedLongTy; |
| 313 | case BuiltinType::ULong: |
| 314 | return T == C.LongTy; |
| 315 | case BuiltinType::LongLong: |
| 316 | return T == C.UnsignedLongLongTy; |
| 317 | case BuiltinType::ULongLong: |
| 318 | return T == C.LongLongTy; |
| 319 | } |
| 320 | |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | if (K == CStrTy) { |
| 325 | const PointerType *PT = argTy->getAs<PointerType>(); |
| 326 | if (!PT) |
| 327 | return false; |
| 328 | |
| 329 | QualType pointeeTy = PT->getPointeeType(); |
| 330 | |
| 331 | if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>()) |
| 332 | switch (BT->getKind()) { |
| 333 | case BuiltinType::Void: |
| 334 | case BuiltinType::Char_U: |
| 335 | case BuiltinType::UChar: |
| 336 | case BuiltinType::Char_S: |
| 337 | case BuiltinType::SChar: |
| 338 | return true; |
| 339 | default: |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | if (K == WCStrTy) { |
| 347 | const PointerType *PT = argTy->getAs<PointerType>(); |
| 348 | if (!PT) |
| 349 | return false; |
| 350 | |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 351 | QualType pointeeTy = |
| 352 | C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType(); |
| 353 | |
| 354 | return pointeeTy == C.getWCharType(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const { |
| 361 | assert(isValid()); |
| 362 | if (K == SpecificTy) |
| 363 | return T; |
| 364 | if (K == CStrTy) |
| 365 | return C.getPointerType(C.CharTy); |
| 366 | if (K == WCStrTy) |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 367 | return C.getPointerType(C.getWCharType()); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 368 | if (K == ObjCPointerTy) |
| 369 | return C.ObjCBuiltinIdTy; |
| 370 | |
| 371 | return QualType(); |
| 372 | } |
| 373 | |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | // Methods on OptionalAmount. |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | ArgTypeResult OptionalAmount::getArgType(ASTContext &Ctx) const { |
| 379 | return Ctx.IntTy; |
| 380 | } |
| 381 | |
| 382 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 383 | // Methods on FormatSpecifier. |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
| 386 | ArgTypeResult FormatSpecifier::getArgType(ASTContext &Ctx) const { |
| 387 | if (!CS.consumesDataArgument()) |
| 388 | return ArgTypeResult::Invalid(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 390 | if (CS.isIntArg()) |
| 391 | switch (LM) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 392 | case AsLongDouble: |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 393 | return ArgTypeResult::Invalid(); |
| 394 | case None: return Ctx.IntTy; |
| 395 | case AsChar: return Ctx.SignedCharTy; |
| 396 | case AsShort: return Ctx.ShortTy; |
| 397 | case AsLong: return Ctx.LongTy; |
| 398 | case AsLongLong: return Ctx.LongLongTy; |
| 399 | case AsIntMax: |
| 400 | // FIXME: Return unknown for now. |
| 401 | return ArgTypeResult(); |
| 402 | case AsSizeT: return Ctx.getSizeType(); |
| 403 | case AsPtrDiff: return Ctx.getPointerDiffType(); |
| 404 | } |
| 405 | |
| 406 | if (CS.isUIntArg()) |
| 407 | switch (LM) { |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 408 | case AsLongDouble: |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 409 | return ArgTypeResult::Invalid(); |
| 410 | case None: return Ctx.UnsignedIntTy; |
| 411 | case AsChar: return Ctx.UnsignedCharTy; |
| 412 | case AsShort: return Ctx.UnsignedShortTy; |
| 413 | case AsLong: return Ctx.UnsignedLongTy; |
| 414 | case AsLongLong: return Ctx.UnsignedLongLongTy; |
| 415 | case AsIntMax: |
| 416 | // FIXME: Return unknown for now. |
| 417 | return ArgTypeResult(); |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 418 | case AsSizeT: |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 419 | // FIXME: How to get the corresponding unsigned |
| 420 | // version of size_t? |
| 421 | return ArgTypeResult(); |
| 422 | case AsPtrDiff: |
| 423 | // FIXME: How to get the corresponding unsigned |
| 424 | // version of ptrdiff_t? |
| 425 | return ArgTypeResult(); |
| 426 | } |
Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | f911eba | 2010-02-01 23:23:50 +0000 | [diff] [blame] | 428 | if (CS.isDoubleArg()) { |
| 429 | if (LM == AsLongDouble) |
| 430 | return Ctx.LongDoubleTy; |
Ted Kremenek | c9a89fe | 2010-01-30 01:02:18 +0000 | [diff] [blame] | 431 | return Ctx.DoubleTy; |
Ted Kremenek | f911eba | 2010-02-01 23:23:50 +0000 | [diff] [blame] | 432 | } |
Ted Kremenek | 87260c7 | 2010-02-24 00:05:54 +0000 | [diff] [blame] | 433 | |
| 434 | switch (CS.getKind()) { |
| 435 | case ConversionSpecifier::CStrArg: |
| 436 | return ArgTypeResult(LM == AsWideChar ? ArgTypeResult::WCStrTy : ArgTypeResult::CStrTy); |
| 437 | case ConversionSpecifier::UnicodeStrArg: |
| 438 | // FIXME: This appears to be Mac OS X specific. |
| 439 | return ArgTypeResult::WCStrTy; |
| 440 | case ConversionSpecifier::CArg: |
| 441 | return Ctx.WCharTy; |
| 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 446 | // FIXME: Handle other cases. |
Ted Kremenek | 40888ad | 2010-01-29 23:00:35 +0000 | [diff] [blame] | 447 | return ArgTypeResult(); |
Ted Kremenek | 33567d2 | 2010-01-29 22:59:32 +0000 | [diff] [blame] | 448 | } |
| 449 | |