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