blob: 8490130224332b07db88e6cdd417266f49d6450f [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;
Ted Kremenek02be9682013-04-10 06:26:26 +000016typedef long NSInteger;
Ted Kremenekdf220832008-06-16 18:01:05 +000017@class NSString, Protocol;
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +000018extern void NSLog(NSString *format, ...);
19extern void NSLogv(NSString *format, va_list args);
Ted Kremenekdf220832008-06-16 18:01:05 +000020typedef 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> - (NSUInteger)length; @end
29@interface NSSimpleCString : NSString {} @end
30@interface NSConstantString : NSSimpleCString @end
31extern void *_NSConstantStringClassReference;
32
Daniel Dunbar085e8f72008-09-26 03:32:58 +000033typedef const struct __CFString * CFStringRef;
34extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2)));
Jordan Rose50687312012-06-04 23:52:23 +000035#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
36
37// This function is used instead of the builtin if -fno-constant-cfstrings.
Jordan Rose8a64f882012-08-08 21:17:31 +000038// The definition on Mac OS X is NOT annotated with format_arg as of 10.8,
39// but clang will implicitly add the attribute if it's not written.
40extern CFStringRef __CFStringMakeConstantString(const char *);
Daniel Dunbar085e8f72008-09-26 03:32:58 +000041
Ted Kremenekf7066ac2010-01-30 00:56:00 +000042int printf(const char * restrict, ...) ;
43
Ted Kremenekdf220832008-06-16 18:01:05 +000044//===----------------------------------------------------------------------===//
45// Test cases.
46//===----------------------------------------------------------------------===//
47
48void check_nslog(unsigned k) {
49 NSLog(@"%d%%", k); // no-warning
Ted Kremenekf88c8e02010-01-29 20:55:36 +000050 NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}}
Ted Kremenekdf220832008-06-16 18:01:05 +000051}
Daniel Dunbar085e8f72008-09-26 03:32:58 +000052
53// Check type validation
54extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}}
55extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000056
57// <rdar://problem/7068334> - Catch use of long long with int arguments.
58void rdar_7068334() {
59 long long test = 500;
Ted Kremenekce506ae2012-01-20 21:52:58 +000060 printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
61 NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
Jordan Rose50687312012-06-04 23:52:23 +000062 CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000063}
Ted Kremeneke3fc5472010-02-27 08:34:51 +000064
65// <rdar://problem/7697748>
66void rdar_7697748() {
67 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
68}
Ted Kremenek13927a42010-06-16 21:23:04 +000069
70@protocol Foo;
71
72void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
73 printf("%p", x); // no-warning
74 printf("%p", y); // no-warning
75}
76
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +000077// <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored
78extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
79extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
80
81void check_mylog() {
82 MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
Jordan Rose50687312012-06-04 23:52:23 +000083 MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}}
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +000084}
85
86// PR 10275 - format function attribute isn't checked in Objective-C methods
87@interface Foo
88+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
89+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
90@end
91
92void check_method() {
93 [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
94 [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
95}
Ted Kremeneke6ca97f2012-01-25 00:04:09 +000096
97// Warn about using BOOL with %@
98void rdar10743758(id x) {
99 NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
100}
101
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +0000102NSString *test_literal_propagation(void) {
103 const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}}
104 printf(s1); // expected-warning {{more '%' conversions than data arguments}}
105 const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}}
106 const char * const s2 = s5;
107 printf(s2); // expected-warning {{more '%' conversions than data arguments}}
108
109 const char * const s3 = (const char *)0;
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +0000110 printf(s3); // no-warning (NULL is a valid format string)
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +0000111
112 NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}}
113 NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}}
114 NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}}
115 NSString * const ns2 = ns5;
116 NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
117 NSString * ns3 = ns1;
118 NSLog(ns3); // expected-warning {{format string is not a string literal}}}
119}
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000120
121// Do not emit warnings when using NSLocalizedString
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +0000122#include "format-strings-system.h"
123
124// Test it inhibits diag only for macros in system headers
125#define MyNSLocalizedString(key) GetLocalizedString(key)
126#define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000127
128void check_NSLocalizedString() {
129 [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +0000130 [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}}
131}
132
133void check_NSAssert() {
134 NSAssert(@"Hello %@", @"World"); // no-warning
135 MyNSAssert(@"Hello %@", @"World"); // expected-warning {{data argument not used by format string}}
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000136}
Nico Weber339b9072012-01-31 01:43:25 +0000137
Nico Weber1206f732012-02-04 01:50:30 +0000138typedef __WCHAR_TYPE__ wchar_t;
Nico Weber339b9072012-01-31 01:43:25 +0000139
140// Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at
141// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
142
143void test_percent_S() {
144 const unsigned short data[] = { 'a', 'b', 0 };
145 const unsigned short* ptr = data;
146 NSLog(@"%S", ptr); // no-warning
147
148 const wchar_t* wchar_ptr = L"ab";
Jordan Rose2cd34402012-12-05 18:44:49 +0000149 NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}}
Nico Weber339b9072012-01-31 01:43:25 +0000150}
151
152void test_percent_ls() {
153 const unsigned short data[] = { 'a', 'b', 0 };
154 const unsigned short* ptr = data;
155 NSLog(@"%ls", ptr); // no-warning
156
157 const wchar_t* wchar_ptr = L"ab";
Jordan Rose2cd34402012-12-05 18:44:49 +0000158 NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}}
Nico Weber339b9072012-01-31 01:43:25 +0000159}
160
161void test_percent_C() {
162 const unsigned short data = 'a';
163 NSLog(@"%C", data); // no-warning
164
165 const wchar_t wchar_data = L'a';
Jordan Rose2cd34402012-12-05 18:44:49 +0000166 NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}}
Nico Weber339b9072012-01-31 01:43:25 +0000167}
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000168
169// Test that %@ works with toll-free bridging (<rdar://problem/10814120>).
Jordan Rose50687312012-06-04 23:52:23 +0000170void test_toll_free_bridging(CFStringRef x, id y) {
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000171 NSLog(@"%@", x); // no-warning
Jordan Rose50687312012-06-04 23:52:23 +0000172 CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning
173
174 NSLog(@"%@", y); // no-warning
175 CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000176}
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +0000177
178@interface Bar
179+ (void)log:(NSString *)fmt, ...;
180+ (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2)));
181@end
182
183@implementation Bar
184
185+ (void)log:(NSString *)fmt, ... {
186 va_list ap;
187 va_start(ap,fmt);
188 NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}}
189 va_end(ap);
190}
191
192+ (void)log2:(NSString *)fmt, ... {
193 va_list ap;
194 va_start(ap,fmt);
195 NSLogv(fmt, ap); // no-warning
196 va_end(ap);
197}
198
199@end
Ted Kremenekafcd1952012-03-15 21:22:27 +0000200
201
202// Test that it is okay to use %p with the address of a block.
203void rdar11049844_aux();
204int rdar11049844() {
205 typedef void (^MyBlock)(void);
206 MyBlock x = ^void() { rdar11049844_aux(); };
207 printf("%p", x); // no-warning
208}
209
Jordan Rose50687312012-06-04 23:52:23 +0000210void test_nonBuiltinCFStrings() {
211 CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}}
212}
213
Jordan Rose48716662012-07-19 18:10:08 +0000214
215// Don't crash on an invalid argument expression.
216// <rdar://problem/11890818>
217@interface NSDictionary : NSObject
218- (id)objectForKeyedSubscript:(id)key;
219@end
220
221void testInvalidFormatArgument(NSDictionary *dict) {
Jordan Roseb13291a2012-07-19 18:10:18 +0000222 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}}
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 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}}
Jordan Rose48716662012-07-19 18:10:08 +0000225
Jordan Roseb13291a2012-07-19 18:10:18 +0000226 [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}}
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}}
228 [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}}
Jordan Rose48716662012-07-19 18:10:08 +0000229}
230
Jordan Roseddcfbc92012-07-19 18:10:23 +0000231
232// <rdar://problem/11825593>
233void testByValueObjectInFormat(Foo *obj) {
234 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'}}
235
236 [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'}}
237}
238
Ted Kremenek02be9682013-04-10 06:26:26 +0000239// <rdar://problem/13557053>
240void testTypeOf(NSInteger dW, NSInteger dH) {
241 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}}
242}
243