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 37typedef const struct __CFString * CFStringRef; 38extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2))); 39#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr "")) 40 41// This function is used instead of the builtin if -fno-constant-cfstrings. 42// The definition on Mac OS X is NOT annotated with format_arg as of 10.8, 43// but clang will implicitly add the attribute if it's not written. 44extern CFStringRef __CFStringMakeConstantString(const char *); 45 46int printf(const char * restrict, ...) ; 47 48//===----------------------------------------------------------------------===// 49// Test cases. 50//===----------------------------------------------------------------------===// 51 52void check_nslog(unsigned k) { 53 NSLog(@"%d%%", k); // no-warning 54 NSLog(@"%s%lb%d", "unix", 10, 20); // expected-warning {{invalid conversion specifier 'b'}} expected-warning {{data argument not used by format string}} 55} 56 57// Check type validation 58extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}} 59extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}} 60 61// <rdar://problem/7068334> - Catch use of long long with int arguments. 62void rdar_7068334() { 63 long long test = 500; 64 printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} 65 NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} 66 CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} 67} 68 69// <rdar://problem/7697748> 70void rdar_7697748() { 71 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}} 72} 73 74@protocol Foo; 75 76void test_p_conversion_with_objc_pointer(id x, id<Foo> y) { 77 printf("%p", x); // no-warning 78 printf("%p", y); // no-warning 79} 80 81// <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored 82extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 83extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2))); 84 85void check_mylog() { 86 MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}} 87 MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}} 88} 89 90// PR 10275 - format function attribute isn't checked in Objective-C methods 91@interface Foo 92+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2))); 93+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2))); 94@end 95 96void check_method() { 97 [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}} 98 [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}} 99} 100 101// Warn about using BOOL with %@ 102void rdar10743758(id x) { 103 NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}} 104} 105 106NSString *test_literal_propagation(void) { 107 const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}} 108 printf(s1); // expected-warning {{more '%' conversions than data arguments}} 109 const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}} 110 const char * const s2 = s5; 111 printf(s2); // expected-warning {{more '%' conversions than data arguments}} 112 113 const char * const s3 = (const char *)0; 114 printf(s3); // no-warning (NULL is a valid format string) 115 116 NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}} 117 NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}} 118 NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}} 119 NSString * const ns2 = ns5; 120 NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}} 121 NSString * ns3 = ns1; 122 NSLog(ns3); // expected-warning {{format string is not a string literal}}} 123 // expected-note@-1{{treat the string as an argument to avoid this}} 124 125 NSString * const ns6 = @"split" " string " @"%s"; // expected-note {{format string is defined here}} 126 NSLog(ns6); // expected-warning {{more '%' conversions than data arguments}} 127} 128 129// Do not emit warnings when using NSLocalizedString 130#include "format-strings-system.h" 131 132// Test it inhibits diag only for macros in system headers 133#define MyNSLocalizedString(key) GetLocalizedString(key) 134#define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0) 135 136void check_NSLocalizedString() { 137 [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning 138 [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}} 139} 140 141void check_NSAssert() { 142 NSAssert(@"Hello %@", @"World"); // no-warning 143 MyNSAssert(@"Hello %@", @"World"); // expected-warning {{data argument not used by format string}} 144} 145 146typedef __WCHAR_TYPE__ wchar_t; 147 148// Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at 149// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265 150 151void test_percent_S() { 152 const unsigned short data[] = { 'a', 'b', 0 }; 153 const unsigned short* ptr = data; 154 NSLog(@"%S", ptr); // no-warning 155 156 const wchar_t* wchar_ptr = L"ab"; 157 NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}} 158} 159 160void test_percent_ls() { 161 const unsigned short data[] = { 'a', 'b', 0 }; 162 const unsigned short* ptr = data; 163 NSLog(@"%ls", ptr); // no-warning 164 165 const wchar_t* wchar_ptr = L"ab"; 166 NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unichar *' (aka 'const unsigned short *') but the argument has type 'const wchar_t *'}} 167} 168 169void test_percent_C() { 170 const unsigned short data = 'a'; 171 NSLog(@"%C", data); // no-warning 172 173 const wchar_t wchar_data = L'a'; 174 NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} 175} 176 177// Test that %@ works with toll-free bridging (<rdar://problem/10814120>). 178void test_toll_free_bridging(CFStringRef x, id y) { 179 NSLog(@"%@", x); // no-warning 180 CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning 181 182 NSLog(@"%@", y); // no-warning 183 CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning 184} 185 186@interface Bar 187+ (void)log:(NSString *)fmt, ...; 188+ (void)log2:(NSString *)fmt, ... __attribute__((format(NSString, 1, 2))); 189@end 190 191@implementation Bar 192 193+ (void)log:(NSString *)fmt, ... { 194 va_list ap; 195 va_start(ap,fmt); 196 NSLogv(fmt, ap); // expected-warning{{format string is not a string literal}} 197 va_end(ap); 198} 199 200+ (void)log2:(NSString *)fmt, ... { 201 va_list ap; 202 va_start(ap,fmt); 203 NSLogv(fmt, ap); // no-warning 204 va_end(ap); 205} 206 207@end 208 209 210// Test that it is okay to use %p with the address of a block. 211void rdar11049844_aux(); 212int rdar11049844() { 213 typedef void (^MyBlock)(void); 214 MyBlock x = ^void() { rdar11049844_aux(); }; 215 printf("%p", x); // no-warning 216} 217 218void test_nonBuiltinCFStrings() { 219 CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}} 220} 221 222 223// Don't crash on an invalid argument expression. 224// <rdar://problem/11890818> 225@interface NSDictionary : NSObject 226- (id)objectForKeyedSubscript:(id)key; 227@end 228 229void testInvalidFormatArgument(NSDictionary *dict) { 230 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}} 231 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}} 232 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}} 233 234 [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}} 235 [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}} 236 [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}} 237} 238 239 240// <rdar://problem/11825593> 241void testByValueObjectInFormat(Foo *obj) { 242 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'}} 243 printf("%!", *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} expected-warning {{invalid conversion specifier}} 244 printf(0, *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} 245 246 [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'}} 247} 248 249// <rdar://problem/13557053> 250void testTypeOf(NSInteger dW, NSInteger dH) { 251 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'}} 252} 253 254void testUnicode() { 255 NSLog(@"%C", 0x2022); // no-warning 256 NSLog(@"%C", 0x202200); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}} 257} 258 259// Test Objective-C modifier flags. 260void testObjCModifierFlags() { 261 NSLog(@"%[]@", @"Foo"); // expected-warning {{missing object format flag}} 262 NSLog(@"%[", @"Foo"); // expected-warning {{incomplete format specifier}} 263 NSLog(@"%[tt", @"Foo"); // expected-warning {{incomplete format specifier}} 264 NSLog(@"%[tt]@", @"Foo"); // no-warning 265 NSLog(@"%[tt]@ %s", @"Foo", "hello"); // no-warning 266 NSLog(@"%s %[tt]@", "hello", @"Foo"); // no-warning 267 NSLog(@"%[blark]@", @"Foo"); // expected-warning {{'blark' is not a valid object format flag}} 268 NSLog(@"%2$[tt]@ %1$[tt]@", @"Foo", @"Bar"); // no-warning 269 NSLog(@"%2$[tt]@ %1$[tt]s", @"Foo", @"Bar"); // expected-warning {{object format flags cannot be used with 's' conversion specifier}} 270} 271 272// rdar://23622446 273@interface RD23622446_Tester: NSObject 274 275+ (void)stringWithFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2))); 276 277@end 278 279@implementation RD23622446_Tester 280 281__attribute__ ((format_arg(1))) 282const char *rd23622446(const char *format) { 283 return format; 284} 285 286+ (void)stringWithFormat:(const char *)format, ... { 287 return; 288} 289 290- (const char *)test:(const char *)format __attribute__ ((format_arg(1))) { 291 return format; 292} 293 294- (NSString *)str:(NSString *)format __attribute__ ((format_arg(1))) { 295 return format; 296} 297 298- (void)foo { 299 [RD23622446_Tester stringWithFormat:rd23622446("%u"), 1, 2]; // expected-warning {{data argument not used by format string}} 300 [RD23622446_Tester stringWithFormat:[self test: "%u"], 1, 2]; // expected-warning {{data argument not used by format string}} 301 [RD23622446_Tester stringWithFormat:[self test: "%s %s"], "name"]; // expected-warning {{more '%' conversions than data arguments}} 302 NSLog([self str: @"%@ %@"], @"name"); // expected-warning {{more '%' conversions than data arguments}} 303 [RD23622446_Tester stringWithFormat:rd23622446("%d"), 1]; // ok 304 [RD23622446_Tester stringWithFormat:[self test: "%d %d"], 1, 2]; // ok 305 NSLog([self str: @"%@"], @"string"); // ok 306} 307 308@end 309 310@interface NSBundle : NSObject 311- (NSString *)localizedStringForKey:(NSString *)key 312 value:(nullable NSString *)value 313 table:(nullable NSString *)tableName 314 __attribute__((format_arg(1))); 315 316- (NSString *)someRandomMethod:(NSString *)key 317 value:(nullable NSString *)value 318 table:(nullable NSString *)tableName 319 __attribute__((format_arg(1))); 320@end 321 322void useLocalizedStringForKey(NSBundle *bndl) { 323 [NSString stringWithFormat: 324 [bndl localizedStringForKey:@"%d" // expected-warning{{more '%' conversions than data arguments}} 325 value:0 326 table:0]]; 327 // No warning, @"flerp" doesn't have a format specifier. 328 [NSString stringWithFormat: [bndl localizedStringForKey:@"flerp" value:0 table:0], 43, @"flarp"]; 329 330 [NSString stringWithFormat: 331 [bndl localizedStringForKey:@"%f" 332 value:0 333 table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}} 334 335 [NSString stringWithFormat: 336 [bndl someRandomMethod:@"%f" 337 value:0 338 table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}} 339 340 [NSString stringWithFormat: 341 [bndl someRandomMethod:@"flerp" 342 value:0 343 table:0], 42]; // expected-warning{{data argument not used by format string}} 344} 345