blob: c6f26e53b8ee9a79f0404fa8d4d263e79e66fc48 [file] [log] [blame]
Patrick Beardb2f68202012-04-06 18:12:22 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin -Wformat-nonliteral -fsyntax-only -fblocks -verify -Wno-objc-root-class %s
Ted Kremenekdf220832008-06-16 18:01:05 +00002
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
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +000012#include <stdarg.h>
13
Ted Kremenekdf220832008-06-16 18:01:05 +000014typedef signed char BOOL;
15typedef unsigned int NSUInteger;
16@class NSString, Protocol;
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +000017extern void NSLog(NSString *format, ...);
18extern void NSLogv(NSString *format, va_list args);
Ted Kremenekdf220832008-06-16 18:01:05 +000019typedef struct _NSZone NSZone;
20@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
21@protocol NSObject - (BOOL)isEqual:(id)object; @end
22@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
23@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
24@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
25@interface NSObject <NSObject> {} @end
26typedef float CGFloat;
27@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; @end
28@interface NSSimpleCString : NSString {} @end
29@interface NSConstantString : NSSimpleCString @end
30extern void *_NSConstantStringClassReference;
31
Daniel Dunbar085e8f72008-09-26 03:32:58 +000032typedef const struct __CFString * CFStringRef;
33extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2)));
Jordan Rose50687312012-06-04 23:52:23 +000034#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
35
36// This function is used instead of the builtin if -fno-constant-cfstrings.
37// The definition on Mac OS X is NOT annotated with format_arg as of 10.7,
38// but if it were, we want the same checking behavior as with the builtin.
39extern CFStringRef __CFStringMakeConstantString(const char *) __attribute__((format_arg(1)));
Daniel Dunbar085e8f72008-09-26 03:32:58 +000040
Ted Kremenekf7066ac2010-01-30 00:56:00 +000041int printf(const char * restrict, ...) ;
42
Ted Kremenekdf220832008-06-16 18:01:05 +000043//===----------------------------------------------------------------------===//
44// Test cases.
45//===----------------------------------------------------------------------===//
46
47void check_nslog(unsigned k) {
48 NSLog(@"%d%%", k); // no-warning
Ted Kremenekf88c8e02010-01-29 20:55:36 +000049 NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}}
Ted Kremenekdf220832008-06-16 18:01:05 +000050}
Daniel Dunbar085e8f72008-09-26 03:32:58 +000051
52// Check type validation
53extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}}
54extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000055
56// <rdar://problem/7068334> - Catch use of long long with int arguments.
57void rdar_7068334() {
58 long long test = 500;
Ted Kremenekce506ae2012-01-20 21:52:58 +000059 printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
60 NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
Jordan Rose50687312012-06-04 23:52:23 +000061 CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000062}
Ted Kremeneke3fc5472010-02-27 08:34:51 +000063
64// <rdar://problem/7697748>
65void rdar_7697748() {
66 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
67}
Ted Kremenek13927a42010-06-16 21:23:04 +000068
69@protocol Foo;
70
71void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
72 printf("%p", x); // no-warning
73 printf("%p", y); // no-warning
74}
75
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +000076// <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored
77extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
78extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
79
80void check_mylog() {
81 MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
Jordan Rose50687312012-06-04 23:52:23 +000082 MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}}
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +000083}
84
85// PR 10275 - format function attribute isn't checked in Objective-C methods
86@interface Foo
87+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
88+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
89@end
90
91void check_method() {
92 [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
93 [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
94}
Ted Kremeneke6ca97f2012-01-25 00:04:09 +000095
96// Warn about using BOOL with %@
97void rdar10743758(id x) {
98 NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
99}
100
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +0000101NSString *test_literal_propagation(void) {
102 const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}}
103 printf(s1); // expected-warning {{more '%' conversions than data arguments}}
104 const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}}
105 const char * const s2 = s5;
106 printf(s2); // expected-warning {{more '%' conversions than data arguments}}
107
108 const char * const s3 = (const char *)0;
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +0000109 printf(s3); // no-warning (NULL is a valid format string)
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +0000110
111 NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}}
112 NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}}
113 NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}}
114 NSString * const ns2 = ns5;
115 NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
116 NSString * ns3 = ns1;
117 NSLog(ns3); // expected-warning {{format string is not a string literal}}}
118}
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000119
120// Do not emit warnings when using NSLocalizedString
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +0000121#include "format-strings-system.h"
122
123// Test it inhibits diag only for macros in system headers
124#define MyNSLocalizedString(key) GetLocalizedString(key)
125#define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000126
127void check_NSLocalizedString() {
128 [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +0000129 [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}}
130}
131
132void check_NSAssert() {
133 NSAssert(@"Hello %@", @"World"); // no-warning
134 MyNSAssert(@"Hello %@", @"World"); // expected-warning {{data argument not used by format string}}
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000135}
Nico Weber339b9072012-01-31 01:43:25 +0000136
Nico Weber1206f732012-02-04 01:50:30 +0000137typedef __WCHAR_TYPE__ wchar_t;
Nico Weber339b9072012-01-31 01:43:25 +0000138
139// Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at
140// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
141
142void test_percent_S() {
143 const unsigned short data[] = { 'a', 'b', 0 };
144 const unsigned short* ptr = data;
145 NSLog(@"%S", ptr); // no-warning
146
147 const wchar_t* wchar_ptr = L"ab";
148 NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unsigned short *' but the argument has type 'const wchar_t *'}}
149}
150
151void test_percent_ls() {
152 const unsigned short data[] = { 'a', 'b', 0 };
153 const unsigned short* ptr = data;
154 NSLog(@"%ls", ptr); // no-warning
155
156 const wchar_t* wchar_ptr = L"ab";
157 NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unsigned short *' but the argument has type 'const wchar_t *'}}
158}
159
160void test_percent_C() {
161 const unsigned short data = 'a';
162 NSLog(@"%C", data); // no-warning
163
164 const wchar_t wchar_data = L'a';
165 NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'wchar_t'}}
166}
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000167
168// Test that %@ works with toll-free bridging (<rdar://problem/10814120>).
Jordan Rose50687312012-06-04 23:52:23 +0000169void test_toll_free_bridging(CFStringRef x, id y) {
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000170 NSLog(@"%@", x); // no-warning
Jordan Rose50687312012-06-04 23:52:23 +0000171 CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning
172
173 NSLog(@"%@", y); // no-warning
174 CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000175}
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +0000176
177@interface Bar
178+ (void)log:(NSString *)fmt, ...;
179+ (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2)));
180@end
181
182@implementation Bar
183
184+ (void)log:(NSString *)fmt, ... {
185 va_list ap;
186 va_start(ap,fmt);
187 NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}}
188 va_end(ap);
189}
190
191+ (void)log2:(NSString *)fmt, ... {
192 va_list ap;
193 va_start(ap,fmt);
194 NSLogv(fmt, ap); // no-warning
195 va_end(ap);
196}
197
198@end
Ted Kremenekafcd1952012-03-15 21:22:27 +0000199
200
201// Test that it is okay to use %p with the address of a block.
202void rdar11049844_aux();
203int rdar11049844() {
204 typedef void (^MyBlock)(void);
205 MyBlock x = ^void() { rdar11049844_aux(); };
206 printf("%p", x); // no-warning
207}
208
Jordan Rose50687312012-06-04 23:52:23 +0000209void test_nonBuiltinCFStrings() {
210 CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}
211}
212
Jordan Rose48716662012-07-19 18:10:08 +0000213
214// Don't crash on an invalid argument expression.
215// <rdar://problem/11890818>
216@interface NSDictionary : NSObject
217- (id)objectForKeyedSubscript:(id)key;
218@end
219
220void testInvalidFormatArgument(NSDictionary *dict) {
221 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}}
222 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}}
223 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}}
224
225 [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}}
226 [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}}
227 [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}}
228}
229