1 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -Wformat-pedantic -fblocks %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -Wformat-pedantic -fblocks -std=c++11 %s
8 extern int scanf(const char *restrict
, ...);
9 extern int printf(const char *restrict
, ...);
10 extern int vprintf(const char *restrict
, va_list);
13 void f(char **sp
, float *fp
) {
15 #if __cplusplus <= 199711L
16 // expected-warning@-2 {{'a' length modifier is not supported by ISO C}}
18 // expected-warning@-4 {{format specifies type 'float *' but the argument has type 'char **'}}
22 scanf("%afoobar", fp
);
26 printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}}
29 // Test that we properly handle format_idx on C++ members.
32 const char *gettext(const char *fmt
) __attribute__((format_arg(2)));
34 int scanf(const char *, ...) __attribute__((format(scanf
, 2, 3)));
35 int printf(const char *, ...) __attribute__((format(printf
, 2, 3)));
36 int printf2(const char *, ...);
38 static const char *gettext_static(const char *fmt
) __attribute__((format_arg(1)));
39 static int printf_static(const char *fmt
, ...) __attribute__((format(printf
, 1, 2)));
44 foo
.scanf("%d"); // expected-warning{{more '%' conversions than data arguments}}
45 foo
.printf("%d", i
); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
46 Foo::printf_static("%d", i
); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
48 printf(foo
.gettext("%d"), i
); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
49 printf(Foo::gettext_static("%d"), i
); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
52 // Test handling __null for format string literal checking.
54 int test_null_format(const char *format
, ...) __attribute__((__format__ (__printf__
, 1, 2)));
55 #if __cplusplus >= 201103L
56 // expected-note@-2 {{candidate function not viable: no known conversion from 'bool' to 'const char *' for 1st argument}}
60 void rdar8269537(const char *f
)
62 test_null_format(false);
63 #if __cplusplus <= 199711L
64 // expected-warning@-2 {{null from a constant boolean}}
66 // expected-error@-4 {{no matching function for call to 'test_null_format'}}
68 test_null_format(0); // no-warning
69 test_null_format(__null
); // no-warning
70 test_null_format(f
); // expected-warning {{not a string literal}}
71 // expected-note@-1{{treat the string as an argument to avoid this}}
74 int Foo::printf(const char *fmt
, ...) {
77 const char * const format
= fmt
;
78 vprintf(format
, ap
); // no-warning
80 const char *format2
= fmt
;
81 vprintf(format2
, ap
); // expected-warning{{format string is not a string literal}}
86 int Foo::printf2(const char *fmt
, ...) {
89 vprintf(fmt
, ap
); // expected-warning{{format string is not a string literal}}
97 void my_uninstantiated_print(const T
&arg
) {
98 printf("%d", arg
); // no-warning
102 void my_print(const T
&arg
) {
103 printf("%d", arg
); // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
106 void use_my_print() {
107 my_print("abc"); // expected-note {{requested here}}
112 class UninstantiatedPrinter
{
114 static void print(const T
&arg
) {
115 printf("%d", arg
); // no-warning
121 void format(const char *fmt
, ...) __attribute__((format(printf
,2,3)));
124 void print(const T
&arg
) {
125 format("%d", arg
); // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
129 void use_class(Printer
<const char *> &p
) {
130 p
.print("abc"); // expected-note {{requested here}}
134 extern void (^block_print
)(const char * format
, ...) __attribute__((format(printf
, 1, 2)));
137 void uninstantiated_call_block_print(const T
&arg
) {
138 block_print("%d", arg
); // no-warning
142 void call_block_print(const T
&arg
) {
143 block_print("%d", arg
); // expected-warning {{format specifies type 'int' but the argument has type 'const char *'}}
146 void use_block_print() {
147 call_block_print("abc"); // expected-note {{requested here}}
151 namespace implicit_this_tests
{
153 void func1(const char *, ...) __attribute__((__format__(printf
, 1, 2))); // expected-error {{format attribute cannot specify the implicit this argument as the format string}}
154 void (*func2
)(const char *, ...) __attribute__((__format__(printf
, 1, 2)));
155 static void (*func3
)(const char *, ...) __attribute__((__format__(printf
, 1, 2)));
156 static void func4(const char *, ...) __attribute__((__format__(printf
, 1, 2)));
161 t1
.func2("Hello %s"); // expected-warning {{more '%' conversions than data arguments}}
162 t::func3("Hello %s"); // expected-warning {{more '%' conversions than data arguments}}
163 t::func4("Hello %s"); // expected-warning {{more '%' conversions than data arguments}}
166 #if __cplusplus >= 201103L
167 namespace evaluated
{
169 constexpr const char *basic() {
171 "%s %d"; // expected-note {{format string is defined here}}
174 constexpr const char *correct_fmt() {
179 constexpr const char *string_linebreak() {
182 "%d %s"; // expected-note {{format string is defined here}}
185 /*non-constexpr*/ const char *not_literal() {
191 constexpr const char *inner_call() {
192 return "%d %s"; // expected-note {{format string is defined here}}
195 constexpr const char *wrap_constexpr() {
201 printf(basic(), 1, 2); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
202 printf(correct_fmt(), 1, 2);
203 printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
204 printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is not a string literal}}
205 printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
207 constexpr const char *fmt
{"%d%d"};
208 printf(fmt
, 1, 1); // no-warning
210 constexpr const char *fmt2
{"%d"}; // expected-note{{format string is defined here}}
211 printf(fmt2
, "oops"); // expected-warning{{format specifies type 'int' but the argument has type}}
217 namespace ScopedEnumerations
{
218 enum class Scoped1
{ One
};
219 enum class Scoped2
: unsigned short { Two
};
221 void f(Scoped1 S1
, Scoped2 S2
) {
222 printf("%hhd", S1
); // expected-warning {{format specifies type 'char' but the argument has type 'Scoped1'}}
223 printf("%hd", S1
); // expected-warning {{format specifies type 'short' but the argument has type 'Scoped1'}}
224 printf("%d", S1
); // expected-warning {{format specifies type 'int' but the argument has type 'Scoped1'}}
226 printf("%hhd", S2
); // expected-warning {{format specifies type 'char' but the argument has type 'Scoped2'}}
227 printf("%hd", S2
); // expected-warning {{format specifies type 'short' but the argument has type 'Scoped2'}}
228 printf("%d", S2
); // expected-warning {{format specifies type 'int' but the argument has type 'Scoped2'}}
230 scanf("%hhd", &S1
); // expected-warning {{format specifies type 'char *' but the argument has type 'Scoped1 *'}}
231 scanf("%hd", &S1
); // expected-warning {{format specifies type 'short *' but the argument has type 'Scoped1 *'}}
232 scanf("%d", &S1
); // expected-warning {{format specifies type 'int *' but the argument has type 'Scoped1 *'}}
234 scanf("%hhd", &S2
); // expected-warning {{format specifies type 'char *' but the argument has type 'Scoped2 *'}}
235 scanf("%hd", &S2
); // expected-warning {{format specifies type 'short *' but the argument has type 'Scoped2 *'}}
236 scanf("%d", &S2
); // expected-warning {{format specifies type 'int *' but the argument has type 'Scoped2 *'}}