1// RUN: %clang_cc1 -triple x86_64-apple-darwin -Wformat-nonliteral -fsyntax-only -fblocks -verify -Wno-objc-root-class %s
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
12#include <stdarg.h>
13
14typedef signed char BOOL;
15typedef unsigned int NSUInteger;
16typedef long NSInteger;
17@class NSString, Protocol;
18extern void NSLog(NSString *format, ...);
19extern void NSLogv(NSString *format, va_list args);
20typedef 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
27typedef float CGFloat;
28@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
29- (NSUInteger)length;
30+(instancetype)stringWithFormat:(NSString *)fmt, ...
31    __attribute__((format(__NSString__, 1, 2)));
32@end
33@interface NSSimpleCString : NSString {} @end
34@interface NSConstantString : NSSimpleCString @end
35extern void *_NSConstantStringClassReference;
36
37@interface NSAttributedString : NSObject
38+(instancetype)stringWithFormat:(NSAttributedString *)fmt, ...
39    __attribute__((format(__NSString__, 1, 2)));
40@end
41
42typedef const struct __CFString * CFStringRef;
43extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2)));
44#define CFSTR(cStr)  ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
45
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.
49extern CFStringRef __CFStringMakeConstantString(const char *);
50
51int printf(const char * restrict, ...) ;
52
53//===----------------------------------------------------------------------===//
54// Test cases.
55//===----------------------------------------------------------------------===//
56
57void check_nslog(unsigned k) {
58  NSLog(@"%d%%", k); // no-warning
59  NSLog(@"%s%lb%d", "unix", 10, 20); // expected-warning {{invalid conversion specifier 'b'}} expected-warning {{data argument not used by format string}}
60}
61
62// Check type validation
63extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}}
64extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}}
65
66// <rdar://problem/7068334> - Catch use of long long with int arguments.
67void rdar_7068334() {
68  long long test = 500;
69  printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
70  NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
71  CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
72}
73
74// <rdar://problem/7697748>
75void rdar_7697748() {
76  NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
77}
78
79@protocol Foo;
80
81void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
82  printf("%p", x); // no-warning
83  printf("%p", y); // no-warning
84}
85
86// <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored
87extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
88extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
89
90void check_mylog() {
91  MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
92  MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}}
93}
94
95// PR 10275 - format function attribute isn't checked in Objective-C methods
96@interface Foo
97+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
98+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
99@end
100
101void check_method() {
102  [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
103  [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
104}
105
106// Warn about using BOOL with %@
107void rdar10743758(id x) {
108  NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
109}
110
111NSString *test_literal_propagation(void) {
112  const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}}
113  printf(s1); // expected-warning {{more '%' conversions than data arguments}}
114  const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}}
115  const char * const s2 = s5;
116  printf(s2); // expected-warning {{more '%' conversions than data arguments}}
117
118  const char * const s3 = (const char *)0;
119  printf(s3); // no-warning (NULL is a valid format string)
120
121  NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}}
122  NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}}
123  NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}}
124  NSString * const ns2 = ns5;
125  NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
126  NSString * ns3 = ns1;
127  NSLog(ns3); // expected-warning {{format string is not a string literal}}}
128  // expected-note@-1{{treat the string as an argument to avoid this}}
129
130  NSString * const ns6 = @"split" " string " @"%s"; // expected-note {{format string is defined here}}
131  NSLog(ns6); // expected-warning {{more '%' conversions than data arguments}}
132}
133
134// Do not emit warnings when using NSLocalizedString
135#include "format-strings-system.h"
136
137// Test it inhibits diag only for macros in system headers
138#define MyNSLocalizedString(key) GetLocalizedString(key)
139#define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)
140
141void check_NSLocalizedString() {
142  [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
143  [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}}
144}
145
146void check_NSAssert() {
147  NSAssert(@"Hello %@", @"World"); // no-warning
148  MyNSAssert(@"Hello %@", @"World"); // expected-warning  {{data argument not used by format string}}
149}
150
151typedef __WCHAR_TYPE__ wchar_t;
152
153// Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at
154// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
155
156void test_percent_S() {
157  const unsigned short data[] = { 'a', 'b', 0 };
158  const unsigned short* ptr = data;
159  NSLog(@"%S", ptr);  // no-warning
160
161  const wchar_t* wchar_ptr = L"ab";
162  NSLog(@"%S", wchar_ptr);  // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}}
163}
164
165void test_percent_ls() {
166  const unsigned short data[] = { 'a', 'b', 0 };
167  const unsigned short* ptr = data;
168  NSLog(@"%ls", ptr);  // no-warning
169
170  const wchar_t* wchar_ptr = L"ab";
171  NSLog(@"%ls", wchar_ptr);  // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}}
172}
173
174void test_percent_C() {
175  const unsigned short data = 'a';
176  NSLog(@"%C", data);  // no-warning
177
178  const wchar_t wchar_data = L'a';
179  NSLog(@"%C", wchar_data);  // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}}
180}
181
182// Test that %@ works with toll-free bridging (<rdar://problem/10814120>).
183void test_toll_free_bridging(CFStringRef x, id y) {
184  NSLog(@"%@", x); // no-warning
185  CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning
186
187  NSLog(@"%@", y); // no-warning
188  CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning
189}
190
191@interface Bar
192+ (void)log:(NSString *)fmt, ...;
193+ (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2)));
194@end
195
196@implementation Bar
197
198+ (void)log:(NSString *)fmt, ... {
199  va_list ap;
200  va_start(ap,fmt);
201  NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}}
202  va_end(ap);
203}
204
205+ (void)log2:(NSString *)fmt, ... {
206  va_list ap;
207  va_start(ap,fmt);
208  NSLogv(fmt, ap); // no-warning
209  va_end(ap);
210}
211
212@end
213
214
215// Test that it is okay to use %p with the address of a block.
216void rdar11049844_aux();
217int rdar11049844() {
218  typedef void (^MyBlock)(void);
219  MyBlock x = ^void() { rdar11049844_aux(); };
220  printf("%p", x);  // no-warning
221}
222
223void test_nonBuiltinCFStrings() {
224  CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}
225}
226
227
228// Don't crash on an invalid argument expression.
229// <rdar://problem/11890818>
230@interface NSDictionary : NSObject
231- (id)objectForKeyedSubscript:(id)key;
232@end
233
234void testInvalidFormatArgument(NSDictionary *dict) {
235  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}}
236  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}}
237  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}}
238
239  [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}}
240  [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}}
241  [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}}
242}
243
244
245// <rdar://problem/11825593>
246void testByValueObjectInFormat(Foo *obj) {
247  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'}}
248  printf("%!", *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} expected-warning {{invalid conversion specifier}}
249  printf(0, *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}}
250
251  [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'}}
252}
253
254// <rdar://problem/13557053>
255void testTypeOf(NSInteger dW, NSInteger dH) {
256  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'}}
257}
258
259void testUnicode() {
260  NSLog(@"%C", 0x2022); // no-warning
261  NSLog(@"%C", 0x202200); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}}
262}
263
264// Test Objective-C modifier flags.
265void testObjCModifierFlags() {
266  NSLog(@"%[]@", @"Foo"); // expected-warning {{missing object format flag}}
267  NSLog(@"%[", @"Foo"); // expected-warning {{incomplete format specifier}}
268  NSLog(@"%[tt", @"Foo");  // expected-warning {{incomplete format specifier}}
269  NSLog(@"%[tt]@", @"Foo"); // no-warning
270  NSLog(@"%[tt]@ %s", @"Foo", "hello"); // no-warning
271  NSLog(@"%s %[tt]@", "hello", @"Foo"); // no-warning
272  NSLog(@"%[blark]@", @"Foo"); // expected-warning {{'blark' is not a valid object format flag}}
273  NSLog(@"%2$[tt]@ %1$[tt]@", @"Foo", @"Bar"); // no-warning
274  NSLog(@"%2$[tt]@ %1$[tt]s", @"Foo", @"Bar"); // expected-warning {{object format flags cannot be used with 's' conversion specifier}}
275}
276
277// rdar://23622446
278@interface RD23622446_Tester: NSObject
279
280+ (void)stringWithFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
281
282@end
283
284@implementation RD23622446_Tester
285
286__attribute__ ((format_arg(1)))
287const char *rd23622446(const char *format) {
288  return format;
289}
290
291+ (void)stringWithFormat:(const char *)format, ... {
292  return;
293}
294
295- (const char *)test:(const char *)format __attribute__ ((format_arg(1))) {
296  return format;
297}
298
299- (NSString *)str:(NSString *)format __attribute__ ((format_arg(1))) {
300  return format;
301}
302
303- (void)foo {
304  [RD23622446_Tester stringWithFormat:rd23622446("%u"), 1, 2]; // expected-warning {{data argument not used by format string}}
305  [RD23622446_Tester stringWithFormat:[self test: "%u"], 1, 2]; // expected-warning {{data argument not used by format string}}
306  [RD23622446_Tester stringWithFormat:[self test: "%s %s"], "name"]; // expected-warning {{more '%' conversions than data arguments}}
307  NSLog([self str: @"%@ %@"], @"name"); // expected-warning {{more '%' conversions than data arguments}}
308  [RD23622446_Tester stringWithFormat:rd23622446("%d"), 1]; // ok
309  [RD23622446_Tester stringWithFormat:[self test: "%d %d"], 1, 2]; // ok
310  NSLog([self str: @"%@"], @"string"); // ok
311}
312
313@end
314
315@interface NSBundle : NSObject
316- (NSString *)localizedStringForKey:(NSString *)key
317                              value:(nullable NSString *)value
318                              table:(nullable NSString *)tableName
319     __attribute__((format_arg(1)));
320
321- (NSString *)someRandomMethod:(NSString *)key
322                         value:(nullable NSString *)value
323                         table:(nullable NSString *)tableName
324    __attribute__((format_arg(1)));
325
326- (NSAttributedString *)someMethod2:(NSString *)key
327    __attribute__((format_arg(1)));
328@end
329
330void useLocalizedStringForKey(NSBundle *bndl) {
331  [NSString stringWithFormat:
332              [bndl localizedStringForKey:@"%d" // expected-warning{{more '%' conversions than data arguments}}
333                                      value:0
334                                      table:0]];
335  // No warning, @"flerp" doesn't have a format specifier.
336  [NSString stringWithFormat: [bndl localizedStringForKey:@"flerp" value:0 table:0], 43, @"flarp"];
337
338  [NSString stringWithFormat:
339              [bndl localizedStringForKey:@"%f"
340                                    value:0
341                                    table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
342
343  [NSString stringWithFormat:
344              [bndl someRandomMethod:@"%f"
345                               value:0
346                               table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
347
348  [NSString stringWithFormat:
349              [bndl someRandomMethod:@"flerp"
350                               value:0
351                               table:0], 42]; // expected-warning{{data argument not used by format string}}
352
353  [NSAttributedString stringWithFormat:
354              [bndl someMethod2: @"test"], 5]; // expected-warning{{data argument not used by format string}}
355  [NSAttributedString stringWithFormat:
356              [bndl someMethod2: @"%f"], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
357}
358