1*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness %s
2*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-win32 -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness %s
3*ea92b1f9SKarl-Johan Karlsson
4*ea92b1f9SKarl-Johan Karlsson // Verify that -Wformat-signedness alone (without -Wformat) trigger the
5*ea92b1f9SKarl-Johan Karlsson // warnings. Note in gcc this will not trigger the signedness warnings as
6*ea92b1f9SKarl-Johan Karlsson // -Wformat is default off in gcc.
7*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -verify -Wformat-signedness %s
8*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-win32 -std=c11 -fsyntax-only -verify -Wformat-signedness %s
9*ea92b1f9SKarl-Johan Karlsson
10*ea92b1f9SKarl-Johan Karlsson // Verify that -Wformat-signedness warnings are not reported with only -Wformat
11*ea92b1f9SKarl-Johan Karlsson // (gcc compat).
12*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat -verify=okay %s
13*ea92b1f9SKarl-Johan Karlsson
14*ea92b1f9SKarl-Johan Karlsson // Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).
15*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify=okay %s
16*ea92b1f9SKarl-Johan Karlsson // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify=okay %s
17*ea92b1f9SKarl-Johan Karlsson // okay-no-diagnostics
18*ea92b1f9SKarl-Johan Karlsson
19*ea92b1f9SKarl-Johan Karlsson int printf(const char *restrict format, ...);
20*ea92b1f9SKarl-Johan Karlsson int scanf(const char * restrict, ...);
21*ea92b1f9SKarl-Johan Karlsson
test_printf_bool(_Bool x)22*ea92b1f9SKarl-Johan Karlsson void test_printf_bool(_Bool x)
23*ea92b1f9SKarl-Johan Karlsson {
24*ea92b1f9SKarl-Johan Karlsson printf("%d", x); // no-warning
25*ea92b1f9SKarl-Johan Karlsson printf("%u", x); // no-warning
26*ea92b1f9SKarl-Johan Karlsson printf("%x", x); // no-warning
27*ea92b1f9SKarl-Johan Karlsson }
28*ea92b1f9SKarl-Johan Karlsson
test_printf_char(char x)29*ea92b1f9SKarl-Johan Karlsson void test_printf_char(char x)
30*ea92b1f9SKarl-Johan Karlsson {
31*ea92b1f9SKarl-Johan Karlsson printf("%c", x); // no-warning
32*ea92b1f9SKarl-Johan Karlsson }
33*ea92b1f9SKarl-Johan Karlsson
test_printf_unsigned_char(unsigned char x)34*ea92b1f9SKarl-Johan Karlsson void test_printf_unsigned_char(unsigned char x)
35*ea92b1f9SKarl-Johan Karlsson {
36*ea92b1f9SKarl-Johan Karlsson printf("%c", x); // no-warning
37*ea92b1f9SKarl-Johan Karlsson }
38*ea92b1f9SKarl-Johan Karlsson
test_printf_int(int x)39*ea92b1f9SKarl-Johan Karlsson void test_printf_int(int x)
40*ea92b1f9SKarl-Johan Karlsson {
41*ea92b1f9SKarl-Johan Karlsson printf("%d", x); // no-warning
42*ea92b1f9SKarl-Johan Karlsson printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int'}}
43*ea92b1f9SKarl-Johan Karlsson printf("%x", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int'}}
44*ea92b1f9SKarl-Johan Karlsson }
45*ea92b1f9SKarl-Johan Karlsson
test_printf_unsigned(unsigned x)46*ea92b1f9SKarl-Johan Karlsson void test_printf_unsigned(unsigned x)
47*ea92b1f9SKarl-Johan Karlsson {
48*ea92b1f9SKarl-Johan Karlsson printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned int'}}
49*ea92b1f9SKarl-Johan Karlsson printf("%u", x); // no-warning
50*ea92b1f9SKarl-Johan Karlsson printf("%x", x); // no-warning
51*ea92b1f9SKarl-Johan Karlsson }
52*ea92b1f9SKarl-Johan Karlsson
test_printf_long(long x)53*ea92b1f9SKarl-Johan Karlsson void test_printf_long(long x)
54*ea92b1f9SKarl-Johan Karlsson {
55*ea92b1f9SKarl-Johan Karlsson printf("%ld", x); // no-warning
56*ea92b1f9SKarl-Johan Karlsson printf("%lu", x); // expected-warning{{format specifies type 'unsigned long' but the argument has type 'long'}}
57*ea92b1f9SKarl-Johan Karlsson printf("%lx", x); // expected-warning{{format specifies type 'unsigned long' but the argument has type 'long'}}
58*ea92b1f9SKarl-Johan Karlsson }
59*ea92b1f9SKarl-Johan Karlsson
test_printf_unsigned_long(unsigned long x)60*ea92b1f9SKarl-Johan Karlsson void test_printf_unsigned_long(unsigned long x)
61*ea92b1f9SKarl-Johan Karlsson {
62*ea92b1f9SKarl-Johan Karlsson printf("%ld", x); // expected-warning{{format specifies type 'long' but the argument has type 'unsigned long'}}
63*ea92b1f9SKarl-Johan Karlsson printf("%lu", x); // no-warning
64*ea92b1f9SKarl-Johan Karlsson printf("%lx", x); // no-warning
65*ea92b1f9SKarl-Johan Karlsson }
66*ea92b1f9SKarl-Johan Karlsson
test_printf_long_long(long long x)67*ea92b1f9SKarl-Johan Karlsson void test_printf_long_long(long long x)
68*ea92b1f9SKarl-Johan Karlsson {
69*ea92b1f9SKarl-Johan Karlsson printf("%lld", x); // no-warning
70*ea92b1f9SKarl-Johan Karlsson printf("%llu", x); // expected-warning{{format specifies type 'unsigned long long' but the argument has type 'long long'}}
71*ea92b1f9SKarl-Johan Karlsson printf("%llx", x); // expected-warning{{format specifies type 'unsigned long long' but the argument has type 'long long'}}
72*ea92b1f9SKarl-Johan Karlsson }
73*ea92b1f9SKarl-Johan Karlsson
test_printf_unsigned_long_long(unsigned long long x)74*ea92b1f9SKarl-Johan Karlsson void test_printf_unsigned_long_long(unsigned long long x)
75*ea92b1f9SKarl-Johan Karlsson {
76*ea92b1f9SKarl-Johan Karlsson printf("%lld", x); // expected-warning{{format specifies type 'long long' but the argument has type 'unsigned long long'}}
77*ea92b1f9SKarl-Johan Karlsson printf("%llu", x); // no-warning
78*ea92b1f9SKarl-Johan Karlsson printf("%llx", x); // no-warning
79*ea92b1f9SKarl-Johan Karlsson }
80*ea92b1f9SKarl-Johan Karlsson
81*ea92b1f9SKarl-Johan Karlsson enum enum_int {
82*ea92b1f9SKarl-Johan Karlsson minus_1 = -1
83*ea92b1f9SKarl-Johan Karlsson };
84*ea92b1f9SKarl-Johan Karlsson
test_printf_enum_int(enum enum_int x)85*ea92b1f9SKarl-Johan Karlsson void test_printf_enum_int(enum enum_int x)
86*ea92b1f9SKarl-Johan Karlsson {
87*ea92b1f9SKarl-Johan Karlsson printf("%d", x); // no-warning
88*ea92b1f9SKarl-Johan Karlsson printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has underlying type 'int'}}
89*ea92b1f9SKarl-Johan Karlsson printf("%x", x); // expected-warning{{format specifies type 'unsigned int' but the argument has underlying type 'int'}}
90*ea92b1f9SKarl-Johan Karlsson }
91*ea92b1f9SKarl-Johan Karlsson
92*ea92b1f9SKarl-Johan Karlsson #ifndef _WIN32 // Disabled due to enums have different underlying type on _WIN32
93*ea92b1f9SKarl-Johan Karlsson enum enum_unsigned {
94*ea92b1f9SKarl-Johan Karlsson zero = 0
95*ea92b1f9SKarl-Johan Karlsson };
96*ea92b1f9SKarl-Johan Karlsson
test_printf_enum_unsigned(enum enum_unsigned x)97*ea92b1f9SKarl-Johan Karlsson void test_printf_enum_unsigned(enum enum_unsigned x)
98*ea92b1f9SKarl-Johan Karlsson {
99*ea92b1f9SKarl-Johan Karlsson printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has underlying type 'unsigned int'}}
100*ea92b1f9SKarl-Johan Karlsson printf("%u", x); // no-warning
101*ea92b1f9SKarl-Johan Karlsson printf("%x", x); // no-warning
102*ea92b1f9SKarl-Johan Karlsson }
103*ea92b1f9SKarl-Johan Karlsson
104*ea92b1f9SKarl-Johan Karlsson enum enum_long {
105*ea92b1f9SKarl-Johan Karlsson minus_one = -1,
106*ea92b1f9SKarl-Johan Karlsson int_val = __INT_MAX__, // INT_MAX
107*ea92b1f9SKarl-Johan Karlsson unsigned_val = (unsigned)(-__INT_MAX__ -1) // (unsigned)INT_MIN
108*ea92b1f9SKarl-Johan Karlsson };
109*ea92b1f9SKarl-Johan Karlsson
test_printf_enum_long(enum enum_long x)110*ea92b1f9SKarl-Johan Karlsson void test_printf_enum_long(enum enum_long x)
111*ea92b1f9SKarl-Johan Karlsson {
112*ea92b1f9SKarl-Johan Karlsson printf("%ld", x); // no-warning
113*ea92b1f9SKarl-Johan Karlsson printf("%lu", x); // expected-warning{{format specifies type 'unsigned long' but the argument has underlying type 'long'}}
114*ea92b1f9SKarl-Johan Karlsson printf("%lx", x); // expected-warning{{format specifies type 'unsigned long' but the argument has underlying type 'long'}}
115*ea92b1f9SKarl-Johan Karlsson }
116*ea92b1f9SKarl-Johan Karlsson
117*ea92b1f9SKarl-Johan Karlsson enum enum_unsigned_long {
118*ea92b1f9SKarl-Johan Karlsson uint_max_plus = (unsigned long)(__INT_MAX__ *2U +1U)+1, // (unsigned long)UINT_MAX+1
119*ea92b1f9SKarl-Johan Karlsson };
120*ea92b1f9SKarl-Johan Karlsson
test_printf_enum_unsigned_long(enum enum_unsigned_long x)121*ea92b1f9SKarl-Johan Karlsson void test_printf_enum_unsigned_long(enum enum_unsigned_long x)
122*ea92b1f9SKarl-Johan Karlsson {
123*ea92b1f9SKarl-Johan Karlsson printf("%ld", x); // expected-warning{{format specifies type 'long' but the argument has underlying type 'unsigned long'}}
124*ea92b1f9SKarl-Johan Karlsson printf("%lu", x); // no-warning
125*ea92b1f9SKarl-Johan Karlsson printf("%lx", x); // no-warning
126*ea92b1f9SKarl-Johan Karlsson }
127*ea92b1f9SKarl-Johan Karlsson #endif
128*ea92b1f9SKarl-Johan Karlsson
test_scanf_char(char * y)129*ea92b1f9SKarl-Johan Karlsson void test_scanf_char(char *y) {
130*ea92b1f9SKarl-Johan Karlsson scanf("%c", y); // no-warning
131*ea92b1f9SKarl-Johan Karlsson }
132*ea92b1f9SKarl-Johan Karlsson
test_scanf_unsigned_char(unsigned char * y)133*ea92b1f9SKarl-Johan Karlsson void test_scanf_unsigned_char(unsigned char *y) {
134*ea92b1f9SKarl-Johan Karlsson scanf("%c", y); // no-warning
135*ea92b1f9SKarl-Johan Karlsson }
136*ea92b1f9SKarl-Johan Karlsson
test_scanf_int(int * x)137*ea92b1f9SKarl-Johan Karlsson void test_scanf_int(int *x) {
138*ea92b1f9SKarl-Johan Karlsson scanf("%d", x); // no-warning
139*ea92b1f9SKarl-Johan Karlsson scanf("%u", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'int *'}}
140*ea92b1f9SKarl-Johan Karlsson scanf("%x", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'int *'}}
141*ea92b1f9SKarl-Johan Karlsson }
142*ea92b1f9SKarl-Johan Karlsson
test_scanf_unsigned(unsigned * x)143*ea92b1f9SKarl-Johan Karlsson void test_scanf_unsigned(unsigned *x) {
144*ea92b1f9SKarl-Johan Karlsson scanf("%d", x); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned int *'}}
145*ea92b1f9SKarl-Johan Karlsson scanf("%u", x); // no-warning
146*ea92b1f9SKarl-Johan Karlsson scanf("%x", x); // no-warning
147*ea92b1f9SKarl-Johan Karlsson }
148*ea92b1f9SKarl-Johan Karlsson
test_scanf_long(long * x)149*ea92b1f9SKarl-Johan Karlsson void test_scanf_long(long *x) {
150*ea92b1f9SKarl-Johan Karlsson scanf("%ld", x); // no-warning
151*ea92b1f9SKarl-Johan Karlsson scanf("%lu", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'long *'}}
152*ea92b1f9SKarl-Johan Karlsson scanf("%lx", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'long *'}}
153*ea92b1f9SKarl-Johan Karlsson }
154*ea92b1f9SKarl-Johan Karlsson
test_scanf_unsigned_long(unsigned long * x)155*ea92b1f9SKarl-Johan Karlsson void test_scanf_unsigned_long(unsigned long *x) {
156*ea92b1f9SKarl-Johan Karlsson scanf("%ld", x); // expected-warning{{format specifies type 'long *' but the argument has type 'unsigned long *'}}
157*ea92b1f9SKarl-Johan Karlsson scanf("%lu", x); // no-warning
158*ea92b1f9SKarl-Johan Karlsson scanf("%lx", x); // no-warning
159*ea92b1f9SKarl-Johan Karlsson }
160*ea92b1f9SKarl-Johan Karlsson
test_scanf_longlong(long long * x)161*ea92b1f9SKarl-Johan Karlsson void test_scanf_longlong(long long *x) {
162*ea92b1f9SKarl-Johan Karlsson scanf("%lld", x); // no-warning
163*ea92b1f9SKarl-Johan Karlsson scanf("%llu", x); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'long long *'}}
164*ea92b1f9SKarl-Johan Karlsson scanf("%llx", x); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'long long *'}}
165*ea92b1f9SKarl-Johan Karlsson }
166*ea92b1f9SKarl-Johan Karlsson
test_scanf_unsigned_longlong(unsigned long long * x)167*ea92b1f9SKarl-Johan Karlsson void test_scanf_unsigned_longlong(unsigned long long *x) {
168*ea92b1f9SKarl-Johan Karlsson scanf("%lld", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'unsigned long long *'}}
169*ea92b1f9SKarl-Johan Karlsson scanf("%llu", x); // no-warning
170*ea92b1f9SKarl-Johan Karlsson scanf("%llx", x); // no-warning
171*ea92b1f9SKarl-Johan Karlsson }
172*ea92b1f9SKarl-Johan Karlsson
test_scanf_enum_int(enum enum_int * x)173*ea92b1f9SKarl-Johan Karlsson void test_scanf_enum_int(enum enum_int *x) {
174*ea92b1f9SKarl-Johan Karlsson scanf("%d", x); // no-warning
175*ea92b1f9SKarl-Johan Karlsson scanf("%u", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'enum enum_int *'}}
176*ea92b1f9SKarl-Johan Karlsson scanf("%x", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'enum enum_int *'}}
177*ea92b1f9SKarl-Johan Karlsson }
178*ea92b1f9SKarl-Johan Karlsson
179*ea92b1f9SKarl-Johan Karlsson #ifndef _WIN32 // Disabled due to enums have different underlying type on _WIN32
test_scanf_enum_unsigned(enum enum_unsigned * x)180*ea92b1f9SKarl-Johan Karlsson void test_scanf_enum_unsigned(enum enum_unsigned *x) {
181*ea92b1f9SKarl-Johan Karlsson scanf("%d", x); // expected-warning{{format specifies type 'int *' but the argument has type 'enum enum_unsigned *'}}
182*ea92b1f9SKarl-Johan Karlsson scanf("%u", x); // no-warning
183*ea92b1f9SKarl-Johan Karlsson scanf("%x", x); // no-warning
184*ea92b1f9SKarl-Johan Karlsson }
185*ea92b1f9SKarl-Johan Karlsson
test_scanf_enum_long(enum enum_long * x)186*ea92b1f9SKarl-Johan Karlsson void test_scanf_enum_long(enum enum_long *x) {
187*ea92b1f9SKarl-Johan Karlsson scanf("%ld", x); // no-warning
188*ea92b1f9SKarl-Johan Karlsson scanf("%lu", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'enum enum_long *'}}
189*ea92b1f9SKarl-Johan Karlsson scanf("%lx", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'enum enum_long *'}}
190*ea92b1f9SKarl-Johan Karlsson }
191*ea92b1f9SKarl-Johan Karlsson
test_scanf_enum_unsigned_long(enum enum_unsigned_long * x)192*ea92b1f9SKarl-Johan Karlsson void test_scanf_enum_unsigned_long(enum enum_unsigned_long *x) {
193*ea92b1f9SKarl-Johan Karlsson scanf("%ld", x); // expected-warning{{format specifies type 'long *' but the argument has type 'enum enum_unsigned_long *'}}
194*ea92b1f9SKarl-Johan Karlsson scanf("%lu", x); // no-warning
195*ea92b1f9SKarl-Johan Karlsson scanf("%lx", x); // no-warning
196*ea92b1f9SKarl-Johan Karlsson }
197*ea92b1f9SKarl-Johan Karlsson #endif
198*ea92b1f9SKarl-Johan Karlsson
199*ea92b1f9SKarl-Johan Karlsson // Verify that we get no warnings from <inttypes.h>
200*ea92b1f9SKarl-Johan Karlsson
201*ea92b1f9SKarl-Johan Karlsson typedef short int int16_t;
202*ea92b1f9SKarl-Johan Karlsson typedef unsigned short int uint16_t;
203*ea92b1f9SKarl-Johan Karlsson
test_printf_priX16(int16_t x)204*ea92b1f9SKarl-Johan Karlsson void test_printf_priX16(int16_t x) {
205*ea92b1f9SKarl-Johan Karlsson printf("PRId16: %" "d" /*PRId16*/ "\n", x); // no-warning
206*ea92b1f9SKarl-Johan Karlsson printf("PRIi16: %" "i" /*PRIi16*/ "\n", x); // no-warning
207*ea92b1f9SKarl-Johan Karlsson }
208*ea92b1f9SKarl-Johan Karlsson
test_printf_unsigned_priX16(uint16_t x)209*ea92b1f9SKarl-Johan Karlsson void test_printf_unsigned_priX16(uint16_t x) {
210*ea92b1f9SKarl-Johan Karlsson printf("PRIo16: %" "o" /*PRIo16*/ "\n", x); // no-warning
211*ea92b1f9SKarl-Johan Karlsson printf("PRIu16: %" "u" /*PRIu16*/ "\n", x); // no-warning
212*ea92b1f9SKarl-Johan Karlsson printf("PRIx16: %" "x" /*PRIx16*/ "\n", x); // no-warning
213*ea92b1f9SKarl-Johan Karlsson printf("PRIX16: %" "X" /*PRIX16*/ "\n", x); // no-warning
214*ea92b1f9SKarl-Johan Karlsson }
215*ea92b1f9SKarl-Johan Karlsson
216*ea92b1f9SKarl-Johan Karlsson // Verify that we can suppress a -Wformat-signedness warning by ignoring
217*ea92b1f9SKarl-Johan Karlsson // -Wformat (gcc compat).
test_suppress(int x)218*ea92b1f9SKarl-Johan Karlsson void test_suppress(int x)
219*ea92b1f9SKarl-Johan Karlsson {
220*ea92b1f9SKarl-Johan Karlsson #pragma GCC diagnostic ignored "-Wformat"
221*ea92b1f9SKarl-Johan Karlsson printf("%u", x);
222*ea92b1f9SKarl-Johan Karlsson }
223