blob: a2fe841ced28fe5cd3a8ef47ce0f26f5816c96f2 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %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
12typedef signed char BOOL;
13typedef unsigned int NSUInteger;
14@class NSString, Protocol;
15extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremeneke6ca97f2012-01-25 00:04:09 +000016extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenekdf220832008-06-16 18:01:05 +000017typedef struct _NSZone NSZone;
18@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
19@protocol NSObject - (BOOL)isEqual:(id)object; @end
20@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
21@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
22@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
23@interface NSObject <NSObject> {} @end
24typedef float CGFloat;
25@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; @end
26@interface NSSimpleCString : NSString {} @end
27@interface NSConstantString : NSSimpleCString @end
28extern void *_NSConstantStringClassReference;
29
Daniel Dunbar085e8f72008-09-26 03:32:58 +000030typedef const struct __CFString * CFStringRef;
31extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2)));
32
Ted Kremenekf7066ac2010-01-30 00:56:00 +000033int printf(const char * restrict, ...) ;
34
Ted Kremenekdf220832008-06-16 18:01:05 +000035//===----------------------------------------------------------------------===//
36// Test cases.
37//===----------------------------------------------------------------------===//
38
39void check_nslog(unsigned k) {
40 NSLog(@"%d%%", k); // no-warning
Ted Kremenekf88c8e02010-01-29 20:55:36 +000041 NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}}
Ted Kremenekdf220832008-06-16 18:01:05 +000042}
Daniel Dunbar085e8f72008-09-26 03:32:58 +000043
44// Check type validation
45extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}}
46extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000047
48// <rdar://problem/7068334> - Catch use of long long with int arguments.
49void rdar_7068334() {
50 long long test = 500;
Ted Kremenekce506ae2012-01-20 21:52:58 +000051 printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
52 NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}}
Ted Kremenekf7066ac2010-01-30 00:56:00 +000053}
Ted Kremeneke3fc5472010-02-27 08:34:51 +000054
55// <rdar://problem/7697748>
56void rdar_7697748() {
57 NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}}
58}
Ted Kremenek13927a42010-06-16 21:23:04 +000059
60@protocol Foo;
61
62void test_p_conversion_with_objc_pointer(id x, id<Foo> y) {
63 printf("%p", x); // no-warning
64 printf("%p", y); // no-warning
65}
66
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +000067// <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored
68extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
69extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2)));
70
71void check_mylog() {
72 MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}}
73 // FIXME: find a way to test CFString too, but I don't know how to create constant CFString.
74}
75
76// PR 10275 - format function attribute isn't checked in Objective-C methods
77@interface Foo
78+ (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2)));
79+ (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2)));
80@end
81
82void check_method() {
83 [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}}
84 [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}}
85}
Ted Kremeneke6ca97f2012-01-25 00:04:09 +000086
87// Warn about using BOOL with %@
88void rdar10743758(id x) {
89 NSLog(@"%@ %@", x, (BOOL) 1); // expected-warning {{format specifies type 'id' but the argument has type 'BOOL' (aka 'signed char')}}
90}
91