Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s |
Hans Wennborg | 5294c79 | 2011-12-28 13:10:50 +0000 | [diff] [blame] | 2 | |
| 3 | extern "C" { |
| 4 | extern int scanf(const char *restrict, ...); |
| 5 | extern int printf(const char *restrict, ...); |
| 6 | } |
| 7 | |
| 8 | void 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 Wennborg | 7da1f46 | 2012-01-31 14:59:59 +0000 | [diff] [blame] | 16 | |
| 17 | void g() { |
| 18 | printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}} |
| 19 | } |
Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 20 | |
| 21 | // Test that we properly handle format_idx on C++ members. |
| 22 | class Foo { |
| 23 | public: |
| 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 | |
| 33 | void 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 Kremenek | e3d8e73 | 2012-02-10 19:13:51 +0000 | [diff] [blame] | 42 | |
| 43 | // Test handling __null for format string literal checking. |
| 44 | extern "C" { |
| 45 | int test_null_format(const char *format, ...) __attribute__((__format__ (__printf__, 1, 2))); |
| 46 | } |
| 47 | |
| 48 | void rdar8269537(const char *f) |
| 49 | { |
David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 50 | test_null_format(false); // expected-warning {{null from a constant boolean}} |
| 51 | test_null_format(0); // no-warning |
Ted Kremenek | e3d8e73 | 2012-02-10 19:13:51 +0000 | [diff] [blame] | 52 | test_null_format(__null); // no-warning |
| 53 | test_null_format(f); // expected-warning {{not a string literal}} |
| 54 | } |