xref: /aosp_15_r20/external/clang/test/Sema/format-strings-ms.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 -Wformat-non-iso -DNON_ISO_WARNING %s
3*67e74705SXin Li 
4*67e74705SXin Li int printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
5*67e74705SXin Li int scanf(const char * restrict, ...) ;
6*67e74705SXin Li typedef unsigned short wchar_t;
7*67e74705SXin Li 
8*67e74705SXin Li #ifdef NON_ISO_WARNING
9*67e74705SXin Li 
10*67e74705SXin Li // Split off this test to reduce the warning noise in the rest of the file.
non_iso_warning_test(__int32 i32,__int64 i64,wchar_t c,void * p)11*67e74705SXin Li void non_iso_warning_test(__int32 i32, __int64 i64, wchar_t c, void *p) {
12*67e74705SXin Li   printf("%Id", i32); // expected-warning{{'I' length modifier is not supported by ISO C}}
13*67e74705SXin Li   printf("%I32d", i32); // expected-warning{{'I32' length modifier is not supported by ISO C}}
14*67e74705SXin Li   printf("%I64d", i64); // expected-warning{{'I64' length modifier is not supported by ISO C}}
15*67e74705SXin Li   printf("%wc", c); // expected-warning{{'w' length modifier is not supported by ISO C}}
16*67e74705SXin Li   printf("%Z", p); // expected-warning{{'Z' conversion specifier is not supported by ISO C}}
17*67e74705SXin Li }
18*67e74705SXin Li 
19*67e74705SXin Li #else
20*67e74705SXin Li 
signed_test()21*67e74705SXin Li void signed_test() {
22*67e74705SXin Li   short val = 30;
23*67e74705SXin Li   printf("val = %I64d\n", val); // expected-warning{{format specifies type '__int64' (aka 'long long') but the argument has type 'short'}}
24*67e74705SXin Li   long long bigval = 30;
25*67e74705SXin Li   printf("val = %I32d\n", bigval); // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
26*67e74705SXin Li   printf("val = %Id\n", bigval); // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
27*67e74705SXin Li }
28*67e74705SXin Li 
unsigned_test()29*67e74705SXin Li void unsigned_test() {
30*67e74705SXin Li   unsigned short val = 30;
31*67e74705SXin Li   printf("val = %I64u\n", val); // expected-warning{{format specifies type 'unsigned __int64' (aka 'unsigned long long') but the argument has type 'unsigned short'}}
32*67e74705SXin Li   unsigned long long bigval = 30;
33*67e74705SXin Li   printf("val = %I32u\n", bigval); // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
34*67e74705SXin Li   printf("val = %Iu\n", bigval); // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
35*67e74705SXin Li }
36*67e74705SXin Li 
w_test(wchar_t c,wchar_t * s)37*67e74705SXin Li void w_test(wchar_t c, wchar_t *s) {
38*67e74705SXin Li   printf("%wc", c);
39*67e74705SXin Li   printf("%wC", c);
40*67e74705SXin Li   printf("%C", c);
41*67e74705SXin Li   printf("%ws", s);
42*67e74705SXin Li   printf("%wS", s);
43*67e74705SXin Li   printf("%S", s);
44*67e74705SXin Li   scanf("%wc", &c);
45*67e74705SXin Li   scanf("%wC", &c);
46*67e74705SXin Li   scanf("%C", &c);
47*67e74705SXin Li   scanf("%ws", s);
48*67e74705SXin Li   scanf("%wS", s);
49*67e74705SXin Li   scanf("%S", s);
50*67e74705SXin Li 
51*67e74705SXin Li   double bad;
52*67e74705SXin Li   printf("%wc", bad); // expected-warning{{format specifies type 'wint_t' (aka 'int') but the argument has type 'double'}}
53*67e74705SXin Li   printf("%wC", bad); // expected-warning{{format specifies type 'wchar_t' (aka 'unsigned short') but the argument has type 'double'}}
54*67e74705SXin Li   printf("%C", bad); // expected-warning{{format specifies type 'wchar_t' (aka 'unsigned short') but the argument has type 'double'}}
55*67e74705SXin Li   printf("%ws", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
56*67e74705SXin Li   printf("%wS", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
57*67e74705SXin Li   printf("%S", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
58*67e74705SXin Li   scanf("%wc", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
59*67e74705SXin Li   scanf("%wC", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
60*67e74705SXin Li   scanf("%C", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
61*67e74705SXin Li   scanf("%ws", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
62*67e74705SXin Li   scanf("%wS", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
63*67e74705SXin Li   scanf("%S", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
64*67e74705SXin Li 
65*67e74705SXin Li }
66*67e74705SXin Li 
h_test(char c,char * s)67*67e74705SXin Li void h_test(char c, char* s) {
68*67e74705SXin Li   double bad;
69*67e74705SXin Li   printf("%hc", bad); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
70*67e74705SXin Li   printf("%hC", bad); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
71*67e74705SXin Li   printf("%hs", bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double'}}
72*67e74705SXin Li   printf("%hS", bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double'}}
73*67e74705SXin Li   scanf("%hc", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
74*67e74705SXin Li   scanf("%hC", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
75*67e74705SXin Li   scanf("%hs", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
76*67e74705SXin Li   scanf("%hS", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
77*67e74705SXin Li }
78*67e74705SXin Li 
z_test(void * p)79*67e74705SXin Li void z_test(void *p) {
80*67e74705SXin Li   printf("%Z", p);
81*67e74705SXin Li   printf("%hZ", p);
82*67e74705SXin Li   printf("%lZ", p);
83*67e74705SXin Li   printf("%wZ", p);
84*67e74705SXin Li   printf("%hhZ", p); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 'Z' conversion specifier}}
85*67e74705SXin Li   scanf("%Z", p); // expected-warning{{invalid conversion specifier 'Z'}}
86*67e74705SXin Li }
87*67e74705SXin Li 
88*67e74705SXin Li #endif
89