[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / format-strings-signedness.c
blobd5a8140d9ef8a084ea6c8dd7b58f578bea474178
1 // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness %s
2 // RUN: %clang_cc1 -triple=x86_64-pc-win32 -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness %s
4 // Verify that -Wformat-signedness alone (without -Wformat) trigger the
5 // warnings. Note in gcc this will not trigger the signedness warnings as
6 // -Wformat is default off in gcc.
7 // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -verify -Wformat-signedness %s
8 // RUN: %clang_cc1 -triple=x86_64-pc-win32 -std=c11 -fsyntax-only -verify -Wformat-signedness %s
10 // Verify that -Wformat-signedness warnings are not reported with only -Wformat
11 // (gcc compat).
12 // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat -verify=okay %s
14 // Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).
15 // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify=okay %s
16 // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify=okay %s
17 // okay-no-diagnostics
19 int printf(const char *restrict format, ...);
20 int scanf(const char * restrict, ...);
22 void test_printf_bool(_Bool x)
24 printf("%d", x); // no-warning
25 printf("%u", x); // no-warning
26 printf("%x", x); // no-warning
29 void test_printf_char(char x)
31 printf("%c", x); // no-warning
34 void test_printf_unsigned_char(unsigned char x)
36 printf("%c", x); // no-warning
39 void test_printf_int(int x)
41 printf("%d", x); // no-warning
42 printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int'}}
43 printf("%x", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int'}}
46 void test_printf_unsigned(unsigned x)
48 printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned int'}}
49 printf("%u", x); // no-warning
50 printf("%x", x); // no-warning
53 void test_printf_long(long x)
55 printf("%ld", x); // no-warning
56 printf("%lu", x); // expected-warning{{format specifies type 'unsigned long' but the argument has type 'long'}}
57 printf("%lx", x); // expected-warning{{format specifies type 'unsigned long' but the argument has type 'long'}}
60 void test_printf_unsigned_long(unsigned long x)
62 printf("%ld", x); // expected-warning{{format specifies type 'long' but the argument has type 'unsigned long'}}
63 printf("%lu", x); // no-warning
64 printf("%lx", x); // no-warning
67 void test_printf_long_long(long long x)
69 printf("%lld", x); // no-warning
70 printf("%llu", x); // expected-warning{{format specifies type 'unsigned long long' but the argument has type 'long long'}}
71 printf("%llx", x); // expected-warning{{format specifies type 'unsigned long long' but the argument has type 'long long'}}
74 void test_printf_unsigned_long_long(unsigned long long x)
76 printf("%lld", x); // expected-warning{{format specifies type 'long long' but the argument has type 'unsigned long long'}}
77 printf("%llu", x); // no-warning
78 printf("%llx", x); // no-warning
81 enum enum_int {
82 minus_1 = -1
85 void test_printf_enum_int(enum enum_int x)
87 printf("%d", x); // no-warning
88 printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has underlying type 'int'}}
89 printf("%x", x); // expected-warning{{format specifies type 'unsigned int' but the argument has underlying type 'int'}}
92 #ifndef _WIN32 // Disabled due to enums have different underlying type on _WIN32
93 enum enum_unsigned {
94 zero = 0
97 void test_printf_enum_unsigned(enum enum_unsigned x)
99 printf("%d", x); // expected-warning{{format specifies type 'int' but the argument has underlying type 'unsigned int'}}
100 printf("%u", x); // no-warning
101 printf("%x", x); // no-warning
104 enum enum_long {
105 minus_one = -1,
106 int_val = __INT_MAX__, // INT_MAX
107 unsigned_val = (unsigned)(-__INT_MAX__ -1) // (unsigned)INT_MIN
110 void test_printf_enum_long(enum enum_long x)
112 printf("%ld", x); // no-warning
113 printf("%lu", x); // expected-warning{{format specifies type 'unsigned long' but the argument has underlying type 'long'}}
114 printf("%lx", x); // expected-warning{{format specifies type 'unsigned long' but the argument has underlying type 'long'}}
117 enum enum_unsigned_long {
118 uint_max_plus = (unsigned long)(__INT_MAX__ *2U +1U)+1, // (unsigned long)UINT_MAX+1
121 void test_printf_enum_unsigned_long(enum enum_unsigned_long x)
123 printf("%ld", x); // expected-warning{{format specifies type 'long' but the argument has underlying type 'unsigned long'}}
124 printf("%lu", x); // no-warning
125 printf("%lx", x); // no-warning
127 #endif
129 void test_scanf_char(char *y) {
130 scanf("%c", y); // no-warning
133 void test_scanf_unsigned_char(unsigned char *y) {
134 scanf("%c", y); // no-warning
137 void test_scanf_int(int *x) {
138 scanf("%d", x); // no-warning
139 scanf("%u", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'int *'}}
140 scanf("%x", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'int *'}}
143 void test_scanf_unsigned(unsigned *x) {
144 scanf("%d", x); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned int *'}}
145 scanf("%u", x); // no-warning
146 scanf("%x", x); // no-warning
149 void test_scanf_long(long *x) {
150 scanf("%ld", x); // no-warning
151 scanf("%lu", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'long *'}}
152 scanf("%lx", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'long *'}}
155 void test_scanf_unsigned_long(unsigned long *x) {
156 scanf("%ld", x); // expected-warning{{format specifies type 'long *' but the argument has type 'unsigned long *'}}
157 scanf("%lu", x); // no-warning
158 scanf("%lx", x); // no-warning
161 void test_scanf_longlong(long long *x) {
162 scanf("%lld", x); // no-warning
163 scanf("%llu", x); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'long long *'}}
164 scanf("%llx", x); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'long long *'}}
167 void test_scanf_unsigned_longlong(unsigned long long *x) {
168 scanf("%lld", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'unsigned long long *'}}
169 scanf("%llu", x); // no-warning
170 scanf("%llx", x); // no-warning
173 void test_scanf_enum_int(enum enum_int *x) {
174 scanf("%d", x); // no-warning
175 scanf("%u", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'enum enum_int *'}}
176 scanf("%x", x); // expected-warning{{format specifies type 'unsigned int *' but the argument has type 'enum enum_int *'}}
179 #ifndef _WIN32 // Disabled due to enums have different underlying type on _WIN32
180 void test_scanf_enum_unsigned(enum enum_unsigned *x) {
181 scanf("%d", x); // expected-warning{{format specifies type 'int *' but the argument has type 'enum enum_unsigned *'}}
182 scanf("%u", x); // no-warning
183 scanf("%x", x); // no-warning
186 void test_scanf_enum_long(enum enum_long *x) {
187 scanf("%ld", x); // no-warning
188 scanf("%lu", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'enum enum_long *'}}
189 scanf("%lx", x); // expected-warning{{format specifies type 'unsigned long *' but the argument has type 'enum enum_long *'}}
192 void test_scanf_enum_unsigned_long(enum enum_unsigned_long *x) {
193 scanf("%ld", x); // expected-warning{{format specifies type 'long *' but the argument has type 'enum enum_unsigned_long *'}}
194 scanf("%lu", x); // no-warning
195 scanf("%lx", x); // no-warning
197 #endif
199 // Verify that we get no warnings from <inttypes.h>
201 typedef short int int16_t;
202 typedef unsigned short int uint16_t;
204 void test_printf_priX16(int16_t x) {
205 printf("PRId16: %" "d" /*PRId16*/ "\n", x); // no-warning
206 printf("PRIi16: %" "i" /*PRIi16*/ "\n", x); // no-warning
209 void test_printf_unsigned_priX16(uint16_t x) {
210 printf("PRIo16: %" "o" /*PRIo16*/ "\n", x); // no-warning
211 printf("PRIu16: %" "u" /*PRIu16*/ "\n", x); // no-warning
212 printf("PRIx16: %" "x" /*PRIx16*/ "\n", x); // no-warning
213 printf("PRIX16: %" "X" /*PRIX16*/ "\n", x); // no-warning
216 // Verify that we can suppress a -Wformat-signedness warning by ignoring
217 // -Wformat (gcc compat).
218 void test_suppress(int x)
220 #pragma GCC diagnostic ignored "-Wformat"
221 printf("%u", x);