Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 1 | // RUN: %clang_cc1 -xc %s -verify -DBOOL=_Bool |
| 2 | // RUN: %clang_cc1 -xc++ %s -verify -DBOOL=bool |
| 3 | // RUN: %clang_cc1 -xobjective-c %s -verify -DBOOL=_Bool |
| 4 | // RUN: %clang_cc1 -xc %s -verify -DBOOL=_Bool -Wformat-type-confusion -DTYPE_CONF |
| 5 | // RUN: %clang_cc1 -xc++ %s -verify -DBOOL=bool -Wformat-type-confusion -DTYPE_CONF |
| 6 | |
| 7 | __attribute__((format(__printf__, 1, 2))) |
| 8 | int p(const char *fmt, ...); |
| 9 | |
| 10 | BOOL b; |
| 11 | |
| 12 | #ifdef __OBJC__ |
| 13 | @interface NSString |
| 14 | +(NSString *)stringWithFormat:(NSString *)fmt, ... |
| 15 | __attribute__((format(__NSString__, 1, 2))); |
| 16 | @end |
| 17 | |
| 18 | #define YES __objc_yes |
| 19 | #define NO __objc_no |
| 20 | #endif |
| 21 | |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 22 | int main(void) { |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 23 | p("%d", b); |
| 24 | p("%hd", b); |
| 25 | #ifdef TYPE_CONF |
| 26 | // expected-warning@-2 {{format specifies type 'short' but the argument has type}} |
| 27 | #endif |
| 28 | p("%hhd", b); |
| 29 | p("%u", b); |
| 30 | p("%hu", b); |
| 31 | #ifdef TYPE_CONF |
| 32 | // expected-warning@-2 {{format specifies type 'unsigned short' but the argument has type}} |
| 33 | #endif |
| 34 | p("%hhu", b); |
| 35 | p("%c", b); // expected-warning {{using '%c' format specifier, but argument has boolean value}} |
| 36 | p("%lc", b); // expected-warning {{using '%lc' format specifier, but argument has boolean value}} |
| 37 | p("%c", 1 == 1); // expected-warning {{using '%c' format specifier, but argument has boolean value}} |
| 38 | p("%f", b); // expected-warning{{format specifies type 'double' but the argument has type}} |
| 39 | p("%ld", b); // expected-warning{{format specifies type 'long' but the argument has type}} |
| 40 | p("%lld", b); // expected-warning{{format specifies type 'long long' but the argument has type}} |
| 41 | |
| 42 | #ifdef __OBJC__ |
| 43 | [NSString stringWithFormat: @"%c", 0]; // probably fine? |
| 44 | [NSString stringWithFormat: @"%c", NO]; // expected-warning {{using '%c' format specifier, but argument has boolean value}} |
| 45 | #endif |
| 46 | } |