xref: /llvm-project/clang/test/SemaObjC/format-strings-objc.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
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%lv%d", "unix", 10, 20); // expected-warning {{invalid conversion specifier 'v'}} 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// 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
89void rdar_7697748(void) {
90  NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
91}
92
93@protocol Foo;
94
95void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
96  printf("%p", x); // no-warning
97  printf("%p", y); // no-warning
98}
99
100// PR 10274 - CFString and NSString formats are ignored
101extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
102extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
103
104void check_mylog(void) {
105  MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
106  MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}}
107}
108
109// PR 10275 - format function attribute isn't checked in Objective-C methods
110@interface Foo
111+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
112+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
113@end
114
115void check_method(void) {
116  [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
117  [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
118}
119
120// Warn about using BOOL with %@
121void rdar10743758(id x) {
122  NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
123}
124
125NSString *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}}
131
132  const char * const s3 = (const char *)0;
133  printf(s3); // no-warning (NULL is a valid format string)
134
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}}
143
144  NSString * const ns6 = @"split" " string " @"%s"; // expected-note {{format string is defined here}}
145  NSLog(ns6); // expected-warning {{more '%' conversions than data arguments}}
146}
147
148// Do not emit warnings when using NSLocalizedString
149#include "format-strings-system.h"
150
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)
154
155void 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}}}
158}
159
160void check_NSAssert(void) {
161  NSAssert(@"Hello %@", @"World"); // no-warning
162  MyNSAssert(@"Hello %@", @"World"); // expected-warning  {{data argument not used by format string}}
163}
164
165typedef __WCHAR_TYPE__ wchar_t;
166
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
169
170void test_percent_S(void) {
171  const unsigned short data[] = { 'a', 'b', 0 };
172  const unsigned short* ptr = data;
173  NSLog(@"%S", ptr);  // no-warning
174
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 *'}}
177}
178
179void test_percent_ls(void) {
180  const unsigned short data[] = { 'a', 'b', 0 };
181  const unsigned short* ptr = data;
182  NSLog(@"%ls", ptr);  // no-warning
183
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 *'}}
186}
187
188void test_percent_C(void) {
189  const unsigned short data = 'a';
190  NSLog(@"%C", data);  // no-warning
191
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'}}
194}
195
196// Test that %@ works with toll-free bridging
197void test_toll_free_bridging(CFStringRef x, id y) {
198  NSLog(@"%@", x); // no-warning
199  CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning
200
201  NSLog(@"%@", y); // no-warning
202  CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning
203}
204
205@interface Bar
206+ (void)log:(NSString *)fmt, ...;
207+ (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2)));
208@end
209
210@implementation Bar
211
212+ (void)log:(NSString *)fmt, ... {
213  va_list ap;
214  va_start(ap,fmt);
215  NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}}
216  va_end(ap);
217}
218
219+ (void)log2:(NSString *)fmt, ... {
220  va_list ap;
221  va_start(ap,fmt);
222  NSLogv(fmt, ap); // no-warning
223  va_end(ap);
224}
225
226@end
227
228
229// Test that it is okay to use %p with the address of a block.
230void rdar11049844_aux(void);
231int rdar11049844(void) {
232  typedef void (^MyBlock)(void);
233  MyBlock x = ^void(void) { rdar11049844_aux(); };
234  printf("%p", x);  // no-warning
235}
236
237void test_nonBuiltinCFStrings(void) {
238  CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}
239}
240
241
242// Don't crash on an invalid argument expression.
243@interface NSDictionary : NSObject
244- (id)objectForKeyedSubscript:(id)key;
245@end
246
247void 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}}
251
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}}
255}
256
257
258void 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}}
262
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'}}
264}
265
266void 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}}
268}
269
270void 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'}}
273}
274
275// Test Objective-C modifier flags.
276void 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}}
286}
287
288@interface RD23622446_Tester: NSObject
289
290+ (void)stringWithFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
291
292@end
293
294@implementation RD23622446_Tester
295
296__attribute__ ((format_arg(1)))
297const char *rd23622446(const char *format) {
298  return format;
299}
300
301+ (void)stringWithFormat:(const char *)format, ... {
302  return;
303}
304
305- (const char *)test:(const char *)format __attribute__ ((format_arg(1))) {
306  return format;
307}
308
309- (NSString *)str:(NSString *)format __attribute__ ((format_arg(1))) {
310  return format;
311}
312
313- (void)foo {
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
321}
322
323@end
324
325@interface NSBundle : NSObject
326- (NSString *)localizedStringForKey:(NSString *)key
327                              value:(nullable NSString *)value
328                              table:(nullable NSString *)tableName
329     __attribute__((format_arg(1)));
330
331- (NSString *)someRandomMethod:(NSString *)key
332                         value:(nullable NSString *)value
333                         table:(nullable NSString *)tableName
334    __attribute__((format_arg(1)));
335
336- (NSAttributedString *)someMethod2:(NSString *)key
337    __attribute__((format_arg(1)));
338@end
339
340void useLocalizedStringForKey(NSBundle *bndl) {
341  [NSString stringWithFormat:
342              [bndl localizedStringForKey:@"%d" // expected-warning{{more '%' conversions than data arguments}}
343                                      value:0
344                                      table:0]];
345  // No warning, @"flerp" doesn't have a format specifier.
346  [NSString stringWithFormat: [bndl localizedStringForKey:@"flerp" value:0 table:0], 43, @"flarp"];
347
348  [NSString stringWithFormat:
349              [bndl localizedStringForKey:@"%f"
350                                    value:0
351                                    table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
352
353  [NSString stringWithFormat:
354              [bndl someRandomMethod:@"%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:@"flerp"
360                               value:0
361                               table:0], 42]; // expected-warning{{data argument not used by format string}}
362
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'}}
367}
368