blob: 0d5b62598d11bcd97e2725b4e38a3184b5d38a0d [file] [log] [blame]
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s
Hans Wennborg5294c792011-12-28 13:10:50 +00002
3extern "C" {
4extern int scanf(const char *restrict, ...);
5extern int printf(const char *restrict, ...);
6}
7
8void f(char **sp, float *fp) {
9 // TODO: Warn that the 'a' length modifier is an extension.
10 scanf("%as", sp);
11
12 // TODO: Warn that the 'a' conversion specifier is a C++11 feature.
13 printf("%a", 1.0);
14 scanf("%afoobar", fp);
15}
Hans Wennborg7da1f462012-01-31 14:59:59 +000016
17void g() {
18 printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}}
19}
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +000020
21// Test that we properly handle format_idx on C++ members.
22class Foo {
23public:
24 const char *gettext(const char *fmt) __attribute__((format_arg(2)));
25
26 int scanf(const char *restrict, ...) __attribute__((format(scanf, 2, 3)));
27 int printf(const char *restrict, ...) __attribute__((format(printf, 2, 3)));
28
29 static const char *gettext_static(const char *fmt) __attribute__((format_arg(1)));
30 static int printf_static(const char *restrict, ...) __attribute__((format(printf, 1, 2)));
31};
32
33void h(int *i) {
34 Foo foo;
35 foo.scanf("%d"); // expected-warning{{more '%' conversions than data arguments}}
36 foo.printf("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
37 Foo::printf_static("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
38
39 printf(foo.gettext("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
40 printf(Foo::gettext_static("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
41}
Ted Kremeneke3d8e732012-02-10 19:13:51 +000042
43// Test handling __null for format string literal checking.
44extern "C" {
45 int test_null_format(const char *format, ...) __attribute__((__format__ (__printf__, 1, 2)));
46}
47
48void rdar8269537(const char *f)
49{
David Blaikiea73cdcb2012-02-10 21:07:25 +000050 test_null_format(false); // expected-warning {{null from a constant boolean}}
51 test_null_format(0); // no-warning
Ted Kremeneke3d8e732012-02-10 19:13:51 +000052 test_null_format(__null); // no-warning
53 test_null_format(f); // expected-warning {{not a string literal}}
54}