Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Ted Kremenek | df22083 | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 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 | typedef signed char BOOL; |
| 13 | typedef unsigned int NSUInteger; |
| 14 | @class NSString, Protocol; |
| 15 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 16 | typedef struct _NSZone NSZone; |
| 17 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 18 | @protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 19 | @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end |
| 20 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
| 21 | @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end |
| 22 | @interface NSObject <NSObject> {} @end |
| 23 | typedef float CGFloat; |
| 24 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; @end |
| 25 | @interface NSSimpleCString : NSString {} @end |
| 26 | @interface NSConstantString : NSSimpleCString @end |
| 27 | extern void *_NSConstantStringClassReference; |
| 28 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 29 | typedef const struct __CFString * CFStringRef; |
| 30 | extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2))); |
| 31 | |
Ted Kremenek | f7066ac | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 32 | int printf(const char * restrict, ...) ; |
| 33 | |
Ted Kremenek | df22083 | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | // Test cases. |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | void check_nslog(unsigned k) { |
| 39 | NSLog(@"%d%%", k); // no-warning |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 40 | NSLog(@"%s%lb%d", "unix", 10,20); // expected-warning {{invalid conversion specifier 'b'}} |
Ted Kremenek | df22083 | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 41 | } |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 42 | |
| 43 | // Check type validation |
| 44 | extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}} |
| 45 | extern void CFStringCreateWithFormat2(int *format, ...) __attribute__((format(CFString, 1, 2))); // expected-error {{format argument not a CFString}} |
Ted Kremenek | f7066ac | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 46 | |
| 47 | // <rdar://problem/7068334> - Catch use of long long with int arguments. |
| 48 | void rdar_7068334() { |
| 49 | long long test = 500; |
Ted Kremenek | ce506ae | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 50 | printf("%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} |
| 51 | NSLog(@"%i ",test); // expected-warning{{format specifies type 'int' but the argument has type 'long long'}} |
Ted Kremenek | f7066ac | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 52 | } |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 53 | |
| 54 | // <rdar://problem/7697748> |
| 55 | void rdar_7697748() { |
| 56 | NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}} |
| 57 | } |
Ted Kremenek | 13927a4 | 2010-06-16 21:23:04 +0000 | [diff] [blame] | 58 | |
| 59 | @protocol Foo; |
| 60 | |
| 61 | void test_p_conversion_with_objc_pointer(id x, id<Foo> y) { |
| 62 | printf("%p", x); // no-warning |
| 63 | printf("%p", y); // no-warning |
| 64 | } |
| 65 | |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 66 | // <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored |
| 67 | extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 68 | extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2))); |
| 69 | |
| 70 | void check_mylog() { |
| 71 | MyNSLog(@"%@"); // expected-warning {{more '%' conversions than data arguments}} |
| 72 | // FIXME: find a way to test CFString too, but I don't know how to create constant CFString. |
| 73 | } |
| 74 | |
| 75 | // PR 10275 - format function attribute isn't checked in Objective-C methods |
| 76 | @interface Foo |
| 77 | + (id)fooWithFormat:(NSString *)fmt, ... __attribute__((format(__NSString__, 1, 2))); |
| 78 | + (id)fooWithCStringFormat:(const char *)format, ... __attribute__((format(__printf__, 1, 2))); |
| 79 | @end |
| 80 | |
| 81 | void check_method() { |
| 82 | [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}} |
| 83 | [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}} |
| 84 | } |