Argyrios Kyrtzidis | 23a4ddf | 2012-02-03 17:13:43 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -Wformat-nonliteral -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))); |
Ted Kremenek | e6ca97f | 2012-01-25 00:04:09 +0000 | [diff] [blame] | 16 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | df22083 | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 17 | typedef 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 |
| 24 | typedef float CGFloat; |
| 25 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; @end |
| 26 | @interface NSSimpleCString : NSString {} @end |
| 27 | @interface NSConstantString : NSSimpleCString @end |
| 28 | extern void *_NSConstantStringClassReference; |
| 29 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 30 | typedef const struct __CFString * CFStringRef; |
| 31 | extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2))); |
| 32 | |
Ted Kremenek | f7066ac | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 33 | int printf(const char * restrict, ...) ; |
| 34 | |
Ted Kremenek | df22083 | 2008-06-16 18:01:05 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // Test cases. |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | void check_nslog(unsigned k) { |
| 40 | NSLog(@"%d%%", k); // no-warning |
Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 41 | 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] | 42 | } |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 43 | |
| 44 | // Check type validation |
| 45 | extern void NSLog2(int format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-error {{format argument not an NSString}} |
| 46 | 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] | 47 | |
| 48 | // <rdar://problem/7068334> - Catch use of long long with int arguments. |
| 49 | void rdar_7068334() { |
| 50 | long long test = 500; |
Ted Kremenek | ce506ae | 2012-01-20 21:52:58 +0000 | [diff] [blame] | 51 | 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 Kremenek | f7066ac | 2010-01-30 00:56:00 +0000 | [diff] [blame] | 53 | } |
Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 54 | |
| 55 | // <rdar://problem/7697748> |
| 56 | void rdar_7697748() { |
| 57 | NSLog(@"%@!"); // expected-warning{{more '%' conversions than data arguments}} |
| 58 | } |
Ted Kremenek | 13927a4 | 2010-06-16 21:23:04 +0000 | [diff] [blame] | 59 | |
| 60 | @protocol Foo; |
| 61 | |
| 62 | void 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 Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 67 | // <rdar://problem/10696348>, PR 10274 - CFString and NSString formats are ignored |
| 68 | extern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 69 | extern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2))); |
| 70 | |
| 71 | void 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 | |
| 82 | void check_method() { |
| 83 | [Foo fooWithFormat:@"%@"]; // expected-warning {{more '%' conversions than data arguments}} |
| 84 | [Foo fooWithCStringFormat:"%@"]; // expected-warning {{invalid conversion specifier '@'}} |
| 85 | } |
Ted Kremenek | e6ca97f | 2012-01-25 00:04:09 +0000 | [diff] [blame] | 86 | |
| 87 | // Warn about using BOOL with %@ |
| 88 | void 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 | |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 92 | NSString *test_literal_propagation(void) { |
| 93 | const char * const s1 = "constant string %s"; // expected-note {{format string is defined here}} |
| 94 | printf(s1); // expected-warning {{more '%' conversions than data arguments}} |
| 95 | const char * const s5 = "constant string %s"; // expected-note {{format string is defined here}} |
| 96 | const char * const s2 = s5; |
| 97 | printf(s2); // expected-warning {{more '%' conversions than data arguments}} |
| 98 | |
| 99 | const char * const s3 = (const char *)0; |
Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 100 | printf(s3); // no-warning (NULL is a valid format string) |
Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 101 | |
| 102 | NSString * const ns1 = @"constant string %s"; // expected-note {{format string is defined here}} |
| 103 | NSLog(ns1); // expected-warning {{more '%' conversions than data arguments}} |
| 104 | NSString * const ns5 = @"constant string %s"; // expected-note {{format string is defined here}} |
| 105 | NSString * const ns2 = ns5; |
| 106 | NSLog(ns2); // expected-warning {{more '%' conversions than data arguments}} |
| 107 | NSString * ns3 = ns1; |
| 108 | NSLog(ns3); // expected-warning {{format string is not a string literal}}} |
| 109 | } |
Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 110 | |
| 111 | // Do not emit warnings when using NSLocalizedString |
| 112 | extern NSString *GetLocalizedString(NSString *str); |
| 113 | #define NSLocalizedString(key) GetLocalizedString(key) |
| 114 | |
| 115 | void check_NSLocalizedString() { |
| 116 | [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning |
| 117 | } |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 118 | |
Nico Weber | 1206f73 | 2012-02-04 01:50:30 +0000 | [diff] [blame] | 119 | typedef __WCHAR_TYPE__ wchar_t; |
Nico Weber | 339b907 | 2012-01-31 01:43:25 +0000 | [diff] [blame] | 120 | |
| 121 | // Test that %S, %C, %ls check for 16 bit types in ObjC strings, as described at |
| 122 | // http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265 |
| 123 | |
| 124 | void test_percent_S() { |
| 125 | const unsigned short data[] = { 'a', 'b', 0 }; |
| 126 | const unsigned short* ptr = data; |
| 127 | NSLog(@"%S", ptr); // no-warning |
| 128 | |
| 129 | const wchar_t* wchar_ptr = L"ab"; |
| 130 | NSLog(@"%S", wchar_ptr); // expected-warning{{format specifies type 'const unsigned short *' but the argument has type 'const wchar_t *'}} |
| 131 | } |
| 132 | |
| 133 | void test_percent_ls() { |
| 134 | const unsigned short data[] = { 'a', 'b', 0 }; |
| 135 | const unsigned short* ptr = data; |
| 136 | NSLog(@"%ls", ptr); // no-warning |
| 137 | |
| 138 | const wchar_t* wchar_ptr = L"ab"; |
| 139 | NSLog(@"%ls", wchar_ptr); // expected-warning{{format specifies type 'const unsigned short *' but the argument has type 'const wchar_t *'}} |
| 140 | } |
| 141 | |
| 142 | void test_percent_C() { |
| 143 | const unsigned short data = 'a'; |
| 144 | NSLog(@"%C", data); // no-warning |
| 145 | |
| 146 | const wchar_t wchar_data = L'a'; |
| 147 | NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'wchar_t'}} |
| 148 | } |