Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -Wformat-nonliteral -fsyntax-only -fblocks -verify -Wno-objc-root-class %s |
Ted Kremenek | 87c760a | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 2 | |
| 3 | //===----------------------------------------------------------------------===// |
| 4 | // The following code is reduced using delta-debugging from |
| 5 | // Foundation.h (Mac OS X). |
| 6 | // |
| 7 | // It includes the basic definitions for the test cases below. |
| 8 | // Not including Foundation.h directly makes this test case both svelt and |
| 9 | // portable to non-Mac platforms. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 12 | #include <stdarg.h> |
| 13 | |
Ted Kremenek | 87c760a | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 14 | typedef signed char BOOL; |
| 15 | typedef unsigned int NSUInteger; |
Ted Kremenek | 3365e52 | 2013-04-10 06:26:26 +0000 | [diff] [blame] | 16 | typedef long NSInteger; |
Ted Kremenek | 87c760a | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 17 | @class NSString, Protocol; |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 18 | extern void NSLog(NSString *format, ...); |
| 19 | extern void NSLogv(NSString *format, va_list args); |
Ted Kremenek | 87c760a | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 20 | typedef struct _NSZone NSZone; |
| 21 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 22 | @protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 23 | @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end |
| 24 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
| 25 | @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end |
| 26 | @interface NSObject <NSObject> {} @end |
| 27 | typedef float CGFloat; |
| 28 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; @end |
| 29 | @interface NSSimpleCString : NSString {} @end |
| 30 | @interface NSConstantString : NSSimpleCString @end |
| 31 | extern void *_NSConstantStringClassReference; |
| 32 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 33 | typedef const struct __CFString * CFStringRef; |
| 34 | extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2))); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 35 | #define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr "")) |
| 36 | |
| 37 | // This function is used instead of the builtin if -fno-constant-cfstrings. |
Jordan Rose | 742c607 | 2012-08-08 21:17:31 +0000 | [diff] [blame] | 38 | // The definition on Mac OS X is NOT annotated with format_arg as of 10.8, |
| 39 | // but clang will implicitly add the attribute if it's not written. |
| 40 | extern CFStringRef __CFStringMakeConstantString(const char *); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | b0fe9b9 | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 42 | int printf(const char * restrict, ...) ; |
| 43 | |
Ted Kremenek | 87c760a | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // Test cases. |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | void check_nslog(unsigned k) { |
| 49 | NSLog(@"%d%%", k); // no-warning |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 50 | NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}} |
Ted Kremenek | 87c760a | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 51 | } |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 52 | |
| 53 | // Check type validation |
| 54 | extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}} |
| 55 | extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}} |
Ted Kremenek | b0fe9b9 | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 56 | |
| 57 | // <rdar://problem/7068334> - Catch use of long long with int arguments. |
| 58 | void rdar_7068334() { |
| 59 | long long test = 500; |
Ted Kremenek | e7b9d43 | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 60 | printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} |
| 61 | NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 62 | CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} |
Ted Kremenek | b0fe9b9 | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 63 | } |
Ted Kremenek | 09597b4 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 64 | |
| 65 | // <rdar://problem/7697748> |
| 66 | void rdar_7697748() { |
| 67 | NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}} |
| 68 | } |
Ted Kremenek | 64c235e | 2010-06-16 21:23:04 +0000 | [diff] [blame] | 69 | |
| 70 | @protocol Foo; |
| 71 | |
| 72 | void test_p_conversion_with_objc_pointer(id x, id<Foo> y) { |
| 73 | printf("%p", x); // no-warning |
| 74 | printf("%p", y); // no-warning |
| 75 | } |
| 76 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 77 | // <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored |
| 78 | extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 79 | extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2))); |
| 80 | |
| 81 | void check_mylog() { |
| 82 | MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}} |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 83 | MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}} |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // PR 10275 - format function attribute isn't checked in Objective-C methods |
| 87 | @interface Foo |
| 88 | + (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2))); |
| 89 | + (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2))); |
| 90 | @end |
| 91 | |
| 92 | void check_method() { |
| 93 | [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}} |
| 94 | [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}} |
| 95 | } |
Ted Kremenek | 9b1f3d4 | 2012-01-25 00:04:09 +0000 | [diff] [blame] | 96 | |
| 97 | // Warn about using BOOL with %@ |
| 98 | void rdar10743758(id x) { |
| 99 | NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}} |
| 100 | } |
| 101 | |
Jean-Daniel Dupas | d5f7ef4 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 102 | NSString *test_literal_propagation(void) { |
| 103 | const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}} |
| 104 | printf(s1); // expected-warning {{more '%' conversions than data arguments}} |
| 105 | const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}} |
| 106 | const char * const s2 = s5; |
| 107 | printf(s2); // expected-warning {{more '%' conversions than data arguments}} |
| 108 | |
| 109 | const char * const s3 = (const char *)0; |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 110 | printf(s3); // no-warning (NULL is a valid format string) |
Jean-Daniel Dupas | d5f7ef4 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 111 | |
| 112 | NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}} |
| 113 | NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}} |
| 114 | NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}} |
| 115 | NSString * const ns2 = ns5; |
| 116 | NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}} |
| 117 | NSString * ns3 = ns1; |
| 118 | NSLog(ns3); // expected-warning {{format string is not a string literal}}} |
Benjamin Kramer | cdac761 | 2014-02-25 12:26:20 +0000 | [diff] [blame] | 119 | |
| 120 | NSString * const ns6 = @"split" " string " @"%s"; // expected-note {{format string is defined here}} |
| 121 | NSLog(ns6); // expected-warning {{more '%' conversions than data arguments}} |
Jean-Daniel Dupas | d5f7ef4 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 122 | } |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 123 | |
| 124 | // Do not emit warnings when using NSLocalizedString |
Jean-Daniel Dupas | 2b7da83 | 2012-05-04 21:08:08 +0000 | [diff] [blame] | 125 | #include "format-strings-system.h" |
| 126 | |
| 127 | // Test it inhibits diag only for macros in system headers |
| 128 | #define MyNSLocalizedString(key) GetLocalizedString(key) |
| 129 | #define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0) |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 130 | |
| 131 | void check_NSLocalizedString() { |
| 132 | [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning |
Jean-Daniel Dupas | 2b7da83 | 2012-05-04 21:08:08 +0000 | [diff] [blame] | 133 | [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}} |
| 134 | } |
| 135 | |
| 136 | void check_NSAssert() { |
| 137 | NSAssert(@"Hello %@", @"World"); // no-warning |
| 138 | MyNSAssert(@"Hello %@", @"World"); // expected-warning {{data argument not used by format string}} |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 139 | } |
Nico Weber | 496cdc2 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 140 | |
Nico Weber | d1928cb | 2012-02-04 01:50:30 +0000 | [diff] [blame] | 141 | typedef __WCHAR_TYPE__ wchar_t; |
Nico Weber | 496cdc2 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 142 | |
| 143 | // Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at |
| 144 | // http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265 |
| 145 | |
| 146 | void test_percent_S() { |
| 147 | const unsigned short data[] = { 'a', 'b', 0 }; |
| 148 | const unsigned short* ptr = data; |
| 149 | NSLog(@"%S", ptr); // no-warning |
| 150 | |
| 151 | const wchar_t* wchar_ptr = L"ab"; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 152 | NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}} |
Nico Weber | 496cdc2 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void test_percent_ls() { |
| 156 | const unsigned short data[] = { 'a', 'b', 0 }; |
| 157 | const unsigned short* ptr = data; |
| 158 | NSLog(@"%ls", ptr); // no-warning |
| 159 | |
| 160 | const wchar_t* wchar_ptr = L"ab"; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 161 | NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}} |
Nico Weber | 496cdc2 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void test_percent_C() { |
| 165 | const unsigned short data = 'a'; |
| 166 | NSLog(@"%C", data); // no-warning |
| 167 | |
| 168 | const wchar_t wchar_data = L'a'; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 169 | NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} |
Nico Weber | 496cdc2 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | d22b98a | 2012-02-06 21:45:29 +0000 | [diff] [blame] | 171 | |
| 172 | // Test that %@ works with toll-free bridging (<rdar://problem/10814120>). |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 173 | void test_toll_free_bridging(CFStringRef x, id y) { |
Ted Kremenek | d22b98a | 2012-02-06 21:45:29 +0000 | [diff] [blame] | 174 | NSLog(@"%@", x); // no-warning |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 175 | CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning |
| 176 | |
| 177 | NSLog(@"%@", y); // no-warning |
| 178 | CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning |
Ted Kremenek | d22b98a | 2012-02-06 21:45:29 +0000 | [diff] [blame] | 179 | } |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 180 | |
| 181 | @interface Bar |
| 182 | + (void)log:(NSString *)fmt, ...; |
| 183 | + (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2))); |
| 184 | @end |
| 185 | |
| 186 | @implementation Bar |
| 187 | |
| 188 | + (void)log:(NSString *)fmt, ... { |
| 189 | va_list ap; |
| 190 | va_start(ap,fmt); |
| 191 | NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}} |
| 192 | va_end(ap); |
| 193 | } |
| 194 | |
| 195 | + (void)log2:(NSString *)fmt, ... { |
| 196 | va_list ap; |
| 197 | va_start(ap,fmt); |
| 198 | NSLogv(fmt, ap); // no-warning |
| 199 | va_end(ap); |
| 200 | } |
| 201 | |
| 202 | @end |
Ted Kremenek | c08c475 | 2012-03-15 21:22:27 +0000 | [diff] [blame] | 203 | |
| 204 | |
| 205 | // Test that it is okay to use %p with the address of a block. |
| 206 | void rdar11049844_aux(); |
| 207 | int rdar11049844() { |
| 208 | typedef void (^MyBlock)(void); |
| 209 | MyBlock x = ^void() { rdar11049844_aux(); }; |
| 210 | printf("%p", x); // no-warning |
| 211 | } |
| 212 | |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 213 | void test_nonBuiltinCFStrings() { |
| 214 | CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}} |
| 215 | } |
| 216 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 217 | |
| 218 | // Don't crash on an invalid argument expression. |
| 219 | // <rdar://problem/11890818> |
| 220 | @interface NSDictionary : NSObject |
| 221 | - (id)objectForKeyedSubscript:(id)key; |
| 222 | @end |
| 223 | |
| 224 | void testInvalidFormatArgument(NSDictionary *dict) { |
Jordan Rose | 96c49686 | 2012-07-19 18:10:18 +0000 | [diff] [blame] | 225 | NSLog(@"no specifiers", dict[CFSTR("abc")]); // expected-error{{indexing expression is invalid because subscript type 'CFStringRef' (aka 'const struct __CFString *') is not an integral or Objective-C pointer type}} |
| 226 | NSLog(@"%@", dict[CFSTR("abc")]); // expected-error{{indexing expression is invalid because subscript type 'CFStringRef' (aka 'const struct __CFString *') is not an integral or Objective-C pointer type}} |
| 227 | NSLog(@"%@ %@", dict[CFSTR("abc")]); // expected-error{{indexing expression is invalid because subscript type 'CFStringRef' (aka 'const struct __CFString *') is not an integral or Objective-C pointer type}} |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 228 | |
Jordan Rose | 96c49686 | 2012-07-19 18:10:18 +0000 | [diff] [blame] | 229 | [Foo fooWithFormat:@"no specifiers", dict[CFSTR("abc")]]; // expected-error{{indexing expression is invalid because subscript type 'CFStringRef' (aka 'const struct __CFString *') is not an integral or Objective-C pointer type}} |
| 230 | [Foo fooWithFormat:@"%@", dict[CFSTR("abc")]]; // expected-error{{indexing expression is invalid because subscript type 'CFStringRef' (aka 'const struct __CFString *') is not an integral or Objective-C pointer type}} |
| 231 | [Foo fooWithFormat:@"%@ %@", dict[CFSTR("abc")]]; // expected-error{{indexing expression is invalid because subscript type 'CFStringRef' (aka 'const struct __CFString *') is not an integral or Objective-C pointer type}} expected-warning{{more '%' conversions than data arguments}} |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 234 | |
| 235 | // <rdar://problem/11825593> |
| 236 | void testByValueObjectInFormat(Foo *obj) { |
| 237 | printf("%d %d %d", 1L, *obj, 1L); // expected-error {{cannot pass object with interface type 'Foo' by value to variadic function; expected type from format string was 'int'}} expected-warning 2 {{format specifies type 'int' but the argument has type 'long'}} |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 238 | printf("%!", *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} expected-warning {{invalid conversion specifier}} |
| 239 | printf(0, *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 240 | |
| 241 | [Bar log2:@"%d", *obj]; // expected-error {{cannot pass object with interface type 'Foo' by value to variadic method; expected type from format string was 'int'}} |
| 242 | } |
| 243 | |
Ted Kremenek | 3365e52 | 2013-04-10 06:26:26 +0000 | [diff] [blame] | 244 | // <rdar://problem/13557053> |
| 245 | void testTypeOf(NSInteger dW, NSInteger dH) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 246 | NSLog(@"dW %d dH %d",({ __typeof__(dW) __a = (dW); __a < 0 ? -__a : __a; }),({ __typeof__(dH) __a = (dH); __a < 0 ? -__a : __a; })); // expected-warning 2 {{format specifies type 'int' but the argument has type 'long'}} |
Ted Kremenek | 3365e52 | 2013-04-10 06:26:26 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Ted Kremenek | da2f405 | 2013-10-15 05:25:17 +0000 | [diff] [blame] | 249 | void testUnicode() { |
| 250 | NSLog(@"%C", 0x2022); // no-warning |
| 251 | NSLog(@"%C", 0x202200); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}} |
| 252 | } |
| 253 | |
Ted Kremenek | 2b41771 | 2015-07-02 05:39:16 +0000 | [diff] [blame] | 254 | // Test Objective-C modifier flags. |
| 255 | void testObjCModifierFlags() { |
| 256 | NSLog(@"%[]@", @"Foo"); // expected-warning {{missing object format flag}} |
| 257 | NSLog(@"%[", @"Foo"); // expected-warning {{incomplete format specifier}} |
| 258 | NSLog(@"%[tt", @"Foo"); // expected-warning {{incomplete format specifier}} |
| 259 | NSLog(@"%[tt]@", @"Foo"); // no-warning |
| 260 | NSLog(@"%[tt]@ %s", @"Foo", "hello"); // no-warning |
| 261 | NSLog(@"%s %[tt]@", "hello", @"Foo"); // no-warning |
| 262 | NSLog(@"%[blark]@", @"Foo"); // expected-warning {{'blark' is not a valid object format flag}} |
| 263 | NSLog(@"%2$[tt]@ %1$[tt]@", @"Foo", @"Bar"); // no-warning |
| 264 | NSLog(@"%2$[tt]@ %1$[tt]s", @"Foo", @"Bar"); // expected-warning {{object format flags cannot be used with 's' conversion specifier}} |
| 265 | } |
| 266 | |