xref: /llvm-project/clang/test/SemaCXX/format-strings-scanf.cpp (revision 442f67c8702a792a135d61765909b732827d6bf2)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks -std=c++11 %s
2 
3 __attribute__((format(scanf, 1, 2)))
4 int scanf(const char *, ...);
5 
6 template<typename... Args>
7 __attribute__((format(scanf, 1, 2)))
scan(const char * fmt,Args &&...args)8 int scan(const char *fmt, Args &&...args) { // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
9     return scanf(fmt, args...);
10 }
11 
12 union bag {
13     bool b;
14     unsigned char uc;
15     signed char sc;
16     unsigned short us;
17     signed short ss;
18     unsigned int ui;
19     signed int si;
20     unsigned long ul;
21     signed long sl;
22     unsigned long long ull;
23     signed long long sll;
24     __fp16 f16;
25     float ff;
26     double fd;
27     long double fl;
28 };
29 
test(void)30 void test(void) {
31     bag b;
32     // expected-warning@+2 {{format specifies type 'char *' but the argument has type 'bool *'}}
33     // expected-warning@+1 {{format specifies type 'unsigned char *' but the argument has type 'bool *'}}
34     scan("%hhi %hhu %hhi %hhu", &b.sc, &b.uc, &b.b, &b.b);
35     scan("%hi %hu", &b.ss, &b.us);
36     scan("%i %u", &b.si, &b.ui);
37     scan("%li %lu", &b.sl, &b.ul);
38     scan("%lli %llu", &b.sll, &b.ull);
39     scan("%f", &b.ff);
40     scan("%lf", &b.fd);
41     scan("%Lf", &b.fl);
42 
43     // expected-warning@+4{{format specifies type 'short *' but the argument has type 'signed char *'}}
44     // expected-warning@+3{{format specifies type 'unsigned short *' but the argument has type 'unsigned char *'}}
45     // expected-warning@+2{{format specifies type 'short *' but the argument has type 'bool *'}}
46     // expected-warning@+1{{format specifies type 'unsigned short *' but the argument has type 'bool *'}}
47     scan("%hi %hu %hi %hu", &b.sc, &b.uc, &b.b, &b.b);
48 
49     // expected-warning@+3{{format specifies type 'long *' but the argument has type 'short *'}}
50     // expected-warning@+2{{format specifies type 'char *' but the argument has type 'short *'}}
51     // expected-warning@+1{{format specifies type 'int *' but the argument has type 'short *'}}
52     scan("%hhi %i %li", &b.ss, &b.ss, &b.ss);
53 
54     // expected-warning@+3{{format specifies type 'float *' but the argument has type '__fp16 *'}}
55     // expected-warning@+2{{format specifies type 'float *' but the argument has type 'double *'}}
56     // expected-warning@+1{{format specifies type 'float *' but the argument has type 'long double *'}}
57     scan("%f %f %f", &b.f16, &b.fd, &b.fl);
58 
59     // expected-warning@+3{{format specifies type 'double *' but the argument has type '__fp16 *'}}
60     // expected-warning@+2{{format specifies type 'double *' but the argument has type 'float *'}}
61     // expected-warning@+1{{format specifies type 'double *' but the argument has type 'long double *'}}
62     scan("%lf %lf %lf", &b.f16, &b.ff, &b.fl);
63 
64     // expected-warning@+3{{format specifies type 'long double *' but the argument has type '__fp16 *'}}
65     // expected-warning@+2{{format specifies type 'long double *' but the argument has type 'float *'}}
66     // expected-warning@+1{{format specifies type 'long double *' but the argument has type 'double *'}}
67     scan("%Lf %Lf %Lf", &b.f16, &b.ff, &b.fd);
68 }
69