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