1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -Wformat-nonliteral -fsyntax-only -fblocks -verify -Wno-objc-root-class %s
3 //===----------------------------------------------------------------------===//
4 // The following code is reduced using delta-debugging from
5 // Foundation.h (Mac OS X).
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 //===----------------------------------------------------------------------===//
14 typedef signed char BOOL;
15 typedef unsigned int NSUInteger;
16 typedef long NSInteger;
17 @class NSString, Protocol;
18 extern void NSLog(NSString *format, ...);
19 extern void NSLogv(NSString *format, va_list args);
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>
30 +(instancetype)stringWithFormat:(NSString *)fmt, ...
31 __attribute__((format(__NSString__, 1, 2)));
33 @interface NSSimpleCString : NSString {} @end
34 @interface NSConstantString : NSSimpleCString @end
35 extern void *_NSConstantStringClassReference;
37 @interface NSAttributedString : NSObject
38 +(instancetype)stringWithFormat:(NSAttributedString *)fmt, ...
39 __attribute__((format(__NSString__, 1, 2)));
42 typedef const struct __CFString * CFStringRef;
43 extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2)));
44 #define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
46 // This function is used instead of the builtin if -fno-constant-cfstrings.
47 // The definition on Mac OS X is NOT annotated with format_arg as of 10.8,
48 // but clang will implicitly add the attribute if it's not written.
49 extern CFStringRef __CFStringMakeConstantString(const char *);
51 int printf(const char * restrict, ...) ;
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 void check_nslog(unsigned k) {
58 NSLog(@"%d%%", k); // no-warning
59 NSLog(@"%s%lv%d", "unix", 10, 20); // expected-warning {{invalid conversion specifier 'v'}} expected-warning {{data argument not used by format string}}
62 // Check type validation
63 extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not a string type}}
64 extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a string type}}
66 // Check interoperability of strings
67 extern void NSLog3(const char *, ...) __attribute__((format(__NSString__, 1, 2)));
68 extern void CFStringCreateWithFormat3(CFStringRef, ...) __attribute__((format(__NSString__, 1, 2)));
69 extern void printf2(NSString *format, ...) __attribute__((format(printf, 1, 2)));
71 extern NSString *CStringToNSString(const char *) __attribute__((format_arg(1)));
73 void NSLog3(const char *fmt, ...) {
74 NSString *const nsFmt = CStringToNSString(fmt);
81 // Catch use of long long with int arguments.
82 void rdar_7068334(void) {
84 printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
85 NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
86 CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
89 void rdar_7697748(void) {
90 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
95 void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
96 printf("%p", x); // no-warning
97 printf("%p", y); // no-warning
100 // PR 10274 - CFString and NSString formats are ignored
101 extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
102 extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
104 void check_mylog(void) {
105 MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
106 MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}}
109 // PR 10275 - format function attribute isn't checked in Objective-C methods
111 + (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
112 + (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
115 void check_method(void) {
116 [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
117 [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
120 // Warn about using BOOL with %@
121 void rdar10743758(id x) {
122 NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
125 NSString *test_literal_propagation(void) {
126 const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}}
127 printf(s1); // expected-warning {{more '%' conversions than data arguments}}
128 const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}}
129 const char * const s2 = s5;
130 printf(s2); // expected-warning {{more '%' conversions than data arguments}}
132 const char * const s3 = (const char *)0;
133 printf(s3); // no-warning (NULL is a valid format string)
135 NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}}
136 NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}}
137 NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}}
138 NSString * const ns2 = ns5;
139 NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
140 NSString * ns3 = ns1;
141 NSLog(ns3); // expected-warning {{format string is not a string literal}}}
142 // expected-note@-1{{treat the string as an argument to avoid this}}
144 NSString * const ns6 = @"split" " string " @"%s"; // expected-note {{format string is defined here}}
145 NSLog(ns6); // expected-warning {{more '%' conversions than data arguments}}
148 // Do not emit warnings when using NSLocalizedString
149 #include "format-strings-system.h"
151 // Test it inhibits diag only for macros in system headers
152 #define MyNSLocalizedString(key) GetLocalizedString(key)
153 #define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)
155 void check_NSLocalizedString(void) {
156 [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
157 [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}}
160 void check_NSAssert(void) {
161 NSAssert(@"Hello %@", @"World"); // no-warning
162 MyNSAssert(@"Hello %@", @"World"); // expected-warning {{data argument not used by format string}}
165 typedef __WCHAR_TYPE__ wchar_t;
167 // Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at
168 // http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
170 void test_percent_S(void) {
171 const unsigned short data[] = { 'a', 'b', 0 };
172 const unsigned short* ptr = data;
173 NSLog(@"%S", ptr); // no-warning
175 const wchar_t* wchar_ptr = L"ab";
176 NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}}
179 void test_percent_ls(void) {
180 const unsigned short data[] = { 'a', 'b', 0 };
181 const unsigned short* ptr = data;
182 NSLog(@"%ls", ptr); // no-warning
184 const wchar_t* wchar_ptr = L"ab";
185 NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}}
188 void test_percent_C(void) {
189 const unsigned short data = 'a';
190 NSLog(@"%C", data); // no-warning
192 const wchar_t wchar_data = L'a';
193 NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}}
196 // Test that %@ works with toll-free bridging
197 void test_toll_free_bridging(CFStringRef x, id y) {
198 NSLog(@"%@", x); // no-warning
199 CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning
201 NSLog(@"%@", y); // no-warning
202 CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning
206 + (void)log:(NSString *)fmt, ...;
207 + (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2)));
212 + (void)log:(NSString *)fmt, ... {
215 NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}}
219 + (void)log2:(NSString *)fmt, ... {
222 NSLogv(fmt, ap); // no-warning
229 // Test that it is okay to use %p with the address of a block.
230 void rdar11049844_aux(void);
231 int rdar11049844(void) {
232 typedef void (^MyBlock)(void);
233 MyBlock x = ^void(void) { rdar11049844_aux(); };
234 printf("%p", x); // no-warning
237 void test_nonBuiltinCFStrings(void) {
238 CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}
242 // Don't crash on an invalid argument expression.
243 @interface NSDictionary : NSObject
244 - (id)objectForKeyedSubscript:(id)key;
247 void testInvalidFormatArgument(NSDictionary *dict) {
248 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}}
249 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}}
250 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}}
252 [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}}
253 [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}}
254 [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}}
258 void testByValueObjectInFormat(Foo *obj) {
259 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'}}
260 printf("%!", *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} expected-warning {{invalid conversion specifier}}
261 printf(0, *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}}
263 [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'}}
266 void testTypeOf(NSInteger dW, NSInteger dH) {
267 NSLog(@"dW %d dH %d",({ __typeof__(dW) __a = (dW); __a < 0 ? -__a : __a; }),({ __typeof__(dH) __a = (dH); __a < 0 ? -__a : __a; })); // expected-warning 2 {{values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead}}
270 void testUnicode(void) {
271 NSLog(@"%C", 0x2022); // no-warning
272 NSLog(@"%C", 0x202200); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}}
275 // Test Objective-C modifier flags.
276 void testObjCModifierFlags(void) {
277 NSLog(@"%[]@", @"Foo"); // expected-warning {{missing object format flag}}
278 NSLog(@"%[", @"Foo"); // expected-warning {{incomplete format specifier}}
279 NSLog(@"%[tt", @"Foo"); // expected-warning {{incomplete format specifier}}
280 NSLog(@"%[tt]@", @"Foo"); // no-warning
281 NSLog(@"%[tt]@ %s", @"Foo", "hello"); // no-warning
282 NSLog(@"%s %[tt]@", "hello", @"Foo"); // no-warning
283 NSLog(@"%[blark]@", @"Foo"); // expected-warning {{'blark' is not a valid object format flag}}
284 NSLog(@"%2$[tt]@ %1$[tt]@", @"Foo", @"Bar"); // no-warning
285 NSLog(@"%2$[tt]@ %1$[tt]s", @"Foo", @"Bar"); // expected-warning {{object format flags cannot be used with 's' conversion specifier}}
288 @interface RD23622446_Tester: NSObject
290 + (void)stringWithFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
294 @implementation RD23622446_Tester
296 __attribute__ ((format_arg(1)))
297 const char *rd23622446(const char *format) {
301 + (void)stringWithFormat:(const char *)format, ... {
305 - (const char *)test:(const char *)format __attribute__ ((format_arg(1))) {
309 - (NSString *)str:(NSString *)format __attribute__ ((format_arg(1))) {
314 [RD23622446_Tester stringWithFormat:rd23622446("%u"), 1, 2]; // expected-warning {{data argument not used by format string}}
315 [RD23622446_Tester stringWithFormat:[self test: "%u"], 1, 2]; // expected-warning {{data argument not used by format string}}
316 [RD23622446_Tester stringWithFormat:[self test: "%s %s"], "name"]; // expected-warning {{more '%' conversions than data arguments}}
317 NSLog([self str: @"%@ %@"], @"name"); // expected-warning {{more '%' conversions than data arguments}}
318 [RD23622446_Tester stringWithFormat:rd23622446("%d"), 1]; // ok
319 [RD23622446_Tester stringWithFormat:[self test: "%d %d"], 1, 2]; // ok
320 NSLog([self str: @"%@"], @"string"); // ok
325 @interface NSBundle : NSObject
326 - (NSString *)localizedStringForKey:(NSString *)key
327 value:(nullable NSString *)value
328 table:(nullable NSString *)tableName
329 __attribute__((format_arg(1)));
331 - (NSString *)someRandomMethod:(NSString *)key
332 value:(nullable NSString *)value
333 table:(nullable NSString *)tableName
334 __attribute__((format_arg(1)));
336 - (NSAttributedString *)someMethod2:(NSString *)key
337 __attribute__((format_arg(1)));
340 void useLocalizedStringForKey(NSBundle *bndl) {
341 [NSString stringWithFormat:
342 [bndl localizedStringForKey:@"%d" // expected-warning{{more '%' conversions than data arguments}}
345 // No warning, @"flerp" doesn't have a format specifier.
346 [NSString stringWithFormat: [bndl localizedStringForKey:@"flerp" value:0 table:0], 43, @"flarp"];
348 [NSString stringWithFormat:
349 [bndl localizedStringForKey:@"%f"
351 table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
353 [NSString stringWithFormat:
354 [bndl someRandomMethod:@"%f"
356 table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
358 [NSString stringWithFormat:
359 [bndl someRandomMethod:@"flerp"
361 table:0], 42]; // expected-warning{{data argument not used by format string}}
363 [NSAttributedString stringWithFormat:
364 [bndl someMethod2: @"test"], 5]; // expected-warning{{data argument not used by format string}}
365 [NSAttributedString stringWithFormat:
366 [bndl someMethod2: @"%f"], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}