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