blob: c1285b2e13d41896bdaac7e41c5cb5ac5ae0f3c2 [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)));
34
Ted Kremenekf7066ac2010-01-30 00:56:00 +000035int printf(const char * restrict, ...) ;
36
Ted Kremenekdf220832008-06-16 18:01:05 +000037//===----------------------------------------------------------------------===//
38// Test cases.
39//===----------------------------------------------------------------------===//
40
41void check_nslog(unsigned k) {
42 NSLog(@"%d%%", k); // no-warning
Ted Kremenekf88c8e02010-01-29 20:55:36 +000043 NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}}
Ted Kremenekdf220832008-06-16 18:01:05 +000044}
Daniel Dunbar085e8f72008-09-26 03:32:58 +000045
46// Check type validation
47extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}}
48extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000049
50// <rdar://problem/7068334> - Catch use of long long with int arguments.
51void rdar_7068334() {
52 long long test = 500;
Ted Kremenekce506ae2012-01-20 21:52:58 +000053 printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
54 NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000055}
Ted Kremeneke3fc5472010-02-27 08:34:51 +000056
57// <rdar://problem/7697748>
58void rdar_7697748() {
59 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
60}
Ted Kremenek13927a42010-06-16 21:23:04 +000061
62@protocol Foo;
63
64void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
65 printf("%p", x); // no-warning
66 printf("%p", y); // no-warning
67}
68
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +000069// <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored
70extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
71extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
72
73void check_mylog() {
74 MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
75 // FIXME: find a way to test CFString too, but I don't know how to create constant CFString.
76}
77
78// PR 10275 - format function attribute isn't checked in Objective-C methods
79@interface Foo
80+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
81+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
82@end
83
84void check_method() {
85 [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
86 [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
87}
Ted Kremeneke6ca97f2012-01-25 00:04:09 +000088
89// Warn about using BOOL with %@
90void rdar10743758(id x) {
91 NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
92}
93
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +000094NSString *test_literal_propagation(void) {
95 const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}}
96 printf(s1); // expected-warning {{more '%' conversions than data arguments}}
97 const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}}
98 const char * const s2 = s5;
99 printf(s2); // expected-warning {{more '%' conversions than data arguments}}
100
101 const char * const s3 = (const char *)0;
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +0000102 printf(s3); // no-warning (NULL is a valid format string)
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +0000103
104 NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}}
105 NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}}
106 NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}}
107 NSString * const ns2 = ns5;
108 NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}}
109 NSString * ns3 = ns1;
110 NSLog(ns3); // expected-warning {{format string is not a string literal}}}
111}
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000112
113// Do not emit warnings when using NSLocalizedString
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +0000114#include "format-strings-system.h"
115
116// Test it inhibits diag only for macros in system headers
117#define MyNSLocalizedString(key) GetLocalizedString(key)
118#define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000119
120void check_NSLocalizedString() {
121 [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +0000122 [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}}
123}
124
125void check_NSAssert() {
126 NSAssert(@"Hello %@", @"World"); // no-warning
127 MyNSAssert(@"Hello %@", @"World"); // expected-warning {{data argument not used by format string}}
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +0000128}
Nico Weber339b9072012-01-31 01:43:25 +0000129
Nico Weber1206f732012-02-04 01:50:30 +0000130typedef __WCHAR_TYPE__ wchar_t;
Nico Weber339b9072012-01-31 01:43:25 +0000131
132// Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at
133// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
134
135void test_percent_S() {
136 const unsigned short data[] = { 'a', 'b', 0 };
137 const unsigned short* ptr = data;
138 NSLog(@"%S", ptr); // no-warning
139
140 const wchar_t* wchar_ptr = L"ab";
141 NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unsigned short *' but the argument has type 'const wchar_t *'}}
142}
143
144void test_percent_ls() {
145 const unsigned short data[] = { 'a', 'b', 0 };
146 const unsigned short* ptr = data;
147 NSLog(@"%ls", ptr); // no-warning
148
149 const wchar_t* wchar_ptr = L"ab";
150 NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unsigned short *' but the argument has type 'const wchar_t *'}}
151}
152
153void test_percent_C() {
154 const unsigned short data = 'a';
155 NSLog(@"%C", data); // no-warning
156
157 const wchar_t wchar_data = L'a';
158 NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'wchar_t'}}
159}
Ted Kremenekb4a3ef72012-02-06 21:45:29 +0000160
161// Test that %@ works with toll-free bridging (<rdar://problem/10814120>).
162void test_toll_free_bridging(CFStringRef x) {
163 NSLog(@"%@", x); // no-warning
164}
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +0000165
166@interface Bar
167+ (void)log:(NSString *)fmt, ...;
168+ (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2)));
169@end
170
171@implementation Bar
172
173+ (void)log:(NSString *)fmt, ... {
174 va_list ap;
175 va_start(ap,fmt);
176 NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}}
177 va_end(ap);
178}
179
180+ (void)log2:(NSString *)fmt, ... {
181 va_list ap;
182 va_start(ap,fmt);
183 NSLogv(fmt, ap); // no-warning
184 va_end(ap);
185}
186
187@end
Ted Kremenekafcd1952012-03-15 21:22:27 +0000188
189
190// Test that it is okay to use %p with the address of a block.
191void rdar11049844_aux();
192int rdar11049844() {
193 typedef void (^MyBlock)(void);
194 MyBlock x = ^void() { rdar11049844_aux(); };
195 printf("%p", x); // no-warning
196}
197