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; 16@class NSString, Protocol; 17extern void NSLog(NSString *format, ...); 18extern void NSLogv(NSString *format, va_list args); 19typedef 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 32typedef const struct __CFString * CFStringRef; 33extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2))); 34#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.8, 38// but clang will implicitly add the attribute if it's not written. 39extern CFStringRef __CFStringMakeConstantString(const char *); 40 41int printf(const char * restrict, ...) ; 42 43//===----------------------------------------------------------------------===// 44// Test cases. 45//===----------------------------------------------------------------------===// 46 47void check_nslog(unsigned k) { 48 NSLog(@"%d%%", k); // no-warning 49 NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}} 50} 51 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}} 55 56// <rdar://problem/7068334> - Catch use of long long with int arguments. 57void rdar_7068334() { 58 long long test = 500; 59 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'}} 61 CFStringCreateWithFormat(CFSTR("%i"),test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} 62} 63 64// <rdar://problem/7697748> 65void rdar_7697748() { 66 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}} 67} 68 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 76// <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}} 82 MyCFStringCreateWithFormat(CFSTR("%@")); // expected-warning {{more '%' conversions than data arguments}} 83} 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} 95 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 101NSString *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; 109 printf(s3); // no-warning (NULL is a valid format string) 110 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} 119 120// Do not emit warnings when using NSLocalizedString 121#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) 126 127void check_NSLocalizedString() { 128 [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning 129 [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}} 135} 136 137typedef __WCHAR_TYPE__ wchar_t; 138 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 unichar *' (aka '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 unichar *' (aka '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 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} 166} 167 168// Test that %@ works with toll-free bridging (<rdar://problem/10814120>). 169void test_toll_free_bridging(CFStringRef x, id y) { 170 NSLog(@"%@", x); // no-warning 171 CFStringCreateWithFormat(CFSTR("%@"), x); // no-warning 172 173 NSLog(@"%@", y); // no-warning 174 CFStringCreateWithFormat(CFSTR("%@"), y); // no-warning 175} 176 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 199 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 209void test_nonBuiltinCFStrings() { 210 CFStringCreateWithFormat(__CFStringMakeConstantString("%@"), 1); // expected-warning{{format specifies type 'id' but the argument has type 'int'}} 211} 212 213 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 230 231// <rdar://problem/11825593> 232void testByValueObjectInFormat(Foo *obj) { 233 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'}} 234 235 [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'}} 236} 237 238