[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Sema / format-strings-scanf.c
blobeb5b8ec36bf7a41942c9b05a4523011f11083458
1 // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wformat-nonliteral %s
3 // Test that -Wformat=0 works:
4 // RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 %s
6 #include <stdarg.h>
7 typedef __SIZE_TYPE__ size_t;
8 #define __SSIZE_TYPE__ \
9 __typeof__(_Generic((__SIZE_TYPE__)0, \
10 unsigned long long int : (long long int)0, \
11 unsigned long int : (long int)0, \
12 unsigned int : (int)0, \
13 unsigned short : (short)0, \
14 unsigned char : (signed char)0))
15 typedef __SSIZE_TYPE__ ssize_t;
17 typedef __PTRDIFF_TYPE__ ptrdiff_t;
18 #define __UNSIGNED_PTRDIFF_TYPE__ \
19 __typeof__(_Generic((__PTRDIFF_TYPE__)0, \
20 long long int : (unsigned long long int)0, \
21 long int : (unsigned long int)0, \
22 int : (unsigned int)0, \
23 short : (unsigned short)0, \
24 signed char : (unsigned char)0))
26 typedef struct _FILE FILE;
27 typedef __WCHAR_TYPE__ wchar_t;
29 int fscanf(FILE * restrict, const char * restrict, ...) ;
30 int scanf(const char * restrict, ...) ;
31 int sscanf(const char * restrict, const char * restrict, ...) ;
32 int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
34 int vscanf(const char * restrict, va_list);
35 int vfscanf(FILE * restrict, const char * restrict, va_list);
36 int vsscanf(const char * restrict, const char * restrict, va_list);
38 void test(const char *s, int *i) {
39 scanf(s, i); // expected-warning{{format string is not a string literal}}
40 scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
41 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
42 scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
43 scanf("%B", i); // expected-warning{{invalid conversion specifier 'B'}}
45 unsigned short s_x;
46 scanf ("%" "hu" "\n", &s_x); // no-warning
47 scanf("%hb", &s_x);
48 scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
49 scanf("%%"); // no-warning
50 scanf("%%%1$d", i); // no-warning
51 scanf("%1$d%%", i); // no-warning
52 scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
53 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
54 scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
55 scanf("%*d%1$d", i); // no-warning
57 scanf("%s", (char*)0); // no-warning
58 scanf("%s", (volatile char*)0); // no-warning
59 scanf("%s", (signed char*)0); // no-warning
60 scanf("%s", (unsigned char*)0); // no-warning
61 scanf("%hhu", (signed char*)0); // no-warning
62 scanf("%hhb", (signed char*)0); // no-warning
65 void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
66 scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
67 scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
68 scanf("%ls", ws); // no-warning
69 scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
72 void missing_argument_with_length_modifier() {
73 char buf[30];
74 scanf("%s:%900s", buf); // expected-warning{{more '%' conversions than data arguments}}
77 // Test that the scanf call site is where the warning is attached. If the
78 // format string is somewhere else, point to it in a note.
79 void pr9751(void) {
80 int *i;
81 char str[100];
82 const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
83 scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
84 scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
85 const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
86 scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
87 scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
88 const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}}
89 scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}}
90 const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}}
91 scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}}
94 void test_variants(int *i, const char *s, ...) {
95 FILE *f = 0;
96 char buf[100];
98 fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
99 sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
100 my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
102 va_list ap;
103 va_start(ap, s);
105 vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
106 vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
107 vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
110 void test_scanlist(int *ip, char *sp, wchar_t *ls) {
111 scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
112 scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
113 scanf("%l[xyx]", ls); // no-warning
114 scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
116 // PR19559
117 scanf("%[]% ]", sp); // no-warning
118 scanf("%[^]% ]", sp); // no-warning
119 scanf("%[a^]% ]", sp); // expected-warning {{invalid conversion specifier ' '}}
122 void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
123 /* Make sure "%a" gets parsed as a conversion specifier for float,
124 * even when followed by an 's', 'S' or '[', which would cause it to be
125 * parsed as a length modifier in C90. */
126 scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
127 scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
128 scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
130 // Test that the 'm' length modifier is only allowed with s, S, c, C or [.
131 // TODO: Warn that 'm' is an extension.
132 scanf("%ms", sp); // No warning.
133 scanf("%mS", lsp); // No warning.
134 scanf("%mc", sp); // No warning.
135 scanf("%mC", lsp); // No warning.
136 scanf("%m[abc]", sp); // No warning.
137 scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
139 // Test argument type check for the 'm' length modifier.
140 scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
141 scanf("%mS", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
142 scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
143 scanf("%mC", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
144 scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
147 void test_quad(int *x, long long *llx) {
148 scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
149 scanf("%qd", llx); // no-warning
152 void test_writeback(int *x) {
153 scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}}
154 scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
156 scanf("%hhn", (signed char*)0); // no-warning
157 scanf("%hhn", (char*)0); // no-warning
158 scanf("%hhn", (unsigned char*)0); // no-warning
159 scanf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
161 scanf("%hn", (short*)0); // no-warning
162 scanf("%hn", (unsigned short*)0); // no-warning
163 scanf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
165 scanf("%n", (int*)0); // no-warning
166 scanf("%n", (unsigned int*)0); // no-warning
167 scanf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
169 scanf("%ln", (long*)0); // no-warning
170 scanf("%ln", (unsigned long*)0); // no-warning
171 scanf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
173 scanf("%lln", (long long*)0); // no-warning
174 scanf("%lln", (unsigned long long*)0); // no-warning
175 scanf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
177 scanf("%qn", (long long*)0); // no-warning
178 scanf("%qn", (unsigned long long*)0); // no-warning
179 scanf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
183 void test_qualifiers(const int *cip, volatile int* vip,
184 const char *ccp, volatile char* vcp,
185 const volatile int *cvip) {
186 scanf("%d", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
187 scanf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
188 scanf("%s", ccp); // expected-warning{{format specifies type 'char *' but the argument has type 'const char *'}}
189 scanf("%d", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
191 scanf("%d", vip); // No warning.
192 scanf("%n", vip); // No warning.
193 scanf("%c", vcp); // No warning.
195 typedef int* ip_t;
196 typedef const int* cip_t;
197 scanf("%d", (ip_t)0); // No warning.
198 scanf("%d", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
201 void test_size_types(void) {
202 size_t s = 0;
203 scanf("%zu", &s); // No warning.
204 scanf("%zb", &s);
206 double d1 = 0.;
207 scanf("%zu", &d1); // expected-warning-re{{format specifies type 'size_t *' (aka '{{.+}}') but the argument has type 'double *'}}
209 ssize_t ss = 0;
210 scanf("%zd", &s); // No warning.
212 double d2 = 0.;
213 scanf("%zd", &d2); // expected-warning-re{{format specifies type 'ssize_t *' (aka '{{.+}}') but the argument has type 'double *'}}
215 ssize_t sn = 0;
216 scanf("%zn", &sn); // No warning.
218 double d3 = 0.;
219 scanf("%zn", &d3); // expected-warning-re{{format specifies type 'ssize_t *' (aka '{{.+}}') but the argument has type 'double *'}}
222 void test_ptrdiff_t_types(void) {
223 __UNSIGNED_PTRDIFF_TYPE__ p1 = 0;
224 scanf("%tu", &p1); // No warning.
225 scanf("%tb", &p1);
227 double d1 = 0.;
228 scanf("%tu", &d1); // expected-warning-re{{format specifies type 'unsigned ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}}
230 ptrdiff_t p2 = 0;
231 scanf("%td", &p2); // No warning.
233 double d2 = 0.;
234 scanf("%td", &d2); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}}
236 ptrdiff_t p3 = 0;
237 scanf("%tn", &p3); // No warning.
239 double d3 = 0.;
240 scanf("%tn", &d3); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}}
243 void check_conditional_literal(char *s, int *i) {
244 scanf(0 ? "%s" : "%d", i); // no warning
245 scanf(1 ? "%s" : "%d", i); // expected-warning{{format specifies type 'char *'}}
246 scanf(0 ? "%d %d" : "%d", i); // no warning
247 scanf(1 ? "%d %d" : "%d", i); // expected-warning{{more '%' conversions than data arguments}}
248 scanf(0 ? "%d %d" : "%d", i, s); // expected-warning{{data argument not used}}
249 scanf(1 ? "%d %s" : "%d", i, s); // no warning
250 scanf(i ? "%d %s" : "%d", i, s); // no warning
251 scanf(i ? "%d" : "%d", i, s); // expected-warning{{data argument not used}}
252 scanf(i ? "%s" : "%d", s); // expected-warning{{format specifies type 'int *'}}
255 void test_promotion(void) {
256 // No promotions for *scanf pointers clarified in N2562
257 // https://github.com/llvm/llvm-project/issues/57102
258 // N2562: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2562.pdf
259 int i;
260 signed char sc;
261 unsigned char uc;
262 short ss;
263 unsigned short us;
265 // pointers could not be "promoted"
266 scanf("%hhd", &i); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
267 scanf("%hd", &i); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
268 scanf("%d", &i); // no-warning
269 // char & uchar
270 scanf("%hhd", &sc); // no-warning
271 scanf("%hhd", &uc); // no-warning
272 scanf("%hd", &sc); // expected-warning{{format specifies type 'short *' but the argument has type 'signed char *'}}
273 scanf("%hd", &uc); // expected-warning{{format specifies type 'short *' but the argument has type 'unsigned char *'}}
274 scanf("%d", &sc); // expected-warning{{format specifies type 'int *' but the argument has type 'signed char *'}}
275 scanf("%d", &uc); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned char *'}}
276 // short & ushort
277 scanf("%hhd", &ss); // expected-warning{{format specifies type 'char *' but the argument has type 'short *'}}
278 scanf("%hhd", &us); // expected-warning{{format specifies type 'char *' but the argument has type 'unsigned short *'}}
279 scanf("%hd", &ss); // no-warning
280 scanf("%hd", &us); // no-warning
281 scanf("%d", &ss); // expected-warning{{format specifies type 'int *' but the argument has type 'short *'}}
282 scanf("%d", &us); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned short *'}}
284 // long types
285 scanf("%ld", &i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
286 scanf("%lld", &i); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
287 scanf("%ld", &sc); // expected-warning{{format specifies type 'long *' but the argument has type 'signed char *'}}
288 scanf("%lld", &sc); // expected-warning{{format specifies type 'long long *' but the argument has type 'signed char *'}}
289 scanf("%ld", &uc); // expected-warning{{format specifies type 'long *' but the argument has type 'unsigned char *'}}
290 scanf("%lld", &uc); // expected-warning{{format specifies type 'long long *' but the argument has type 'unsigned char *'}}
291 scanf("%llx", &i); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'int *'}}
293 // ill-formed floats
294 scanf("%hf", // expected-warning{{length modifier 'h' results in undefined behavior or no effect with 'f' conversion specifier}}
295 &sc);
297 // pointers in scanf
298 scanf("%s", i); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
300 // FIXME: does this match what the C committee allows or should it be pedantically warned on?
301 char c;
302 void *vp;
303 scanf("%hhd", &c); // Pedantic warning?
304 scanf("%hhd", vp); // expected-warning{{format specifies type 'char *' but the argument has type 'void *'}}