blob: 74fc7a7810d97bf7ade526efdc97017551042a74 [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
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +00003#include <stdarg.h>
4
Hans Wennborg5294c792011-12-28 13:10:50 +00005extern "C" {
6extern int scanf(const char *restrict, ...);
7extern int printf(const char *restrict, ...);
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +00008extern int vprintf(const char *restrict, va_list);
Hans Wennborg5294c792011-12-28 13:10:50 +00009}
10
11void f(char **sp, float *fp) {
12 // TODO: Warn that the 'a' length modifier is an extension.
13 scanf("%as", sp);
14
15 // TODO: Warn that the 'a' conversion specifier is a C++11 feature.
16 printf("%a", 1.0);
17 scanf("%afoobar", fp);
18}
Hans Wennborg7da1f462012-01-31 14:59:59 +000019
20void g() {
21 printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}}
22}
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +000023
24// Test that we properly handle format_idx on C++ members.
25class Foo {
26public:
27 const char *gettext(const char *fmt) __attribute__((format_arg(2)));
28
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +000029 int scanf(const char *, ...) __attribute__((format(scanf, 2, 3)));
30 int printf(const char *, ...) __attribute__((format(printf, 2, 3)));
31 int printf2(const char *, ...);
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +000032
33 static const char *gettext_static(const char *fmt) __attribute__((format_arg(1)));
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +000034 static int printf_static(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +000035};
36
37void h(int *i) {
38 Foo foo;
39 foo.scanf("%d"); // expected-warning{{more '%' conversions than data arguments}}
40 foo.printf("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
41 Foo::printf_static("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
42
43 printf(foo.gettext("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
44 printf(Foo::gettext_static("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
45}
Ted Kremeneke3d8e732012-02-10 19:13:51 +000046
47// Test handling __null for format string literal checking.
48extern "C" {
49 int test_null_format(const char *format, ...) __attribute__((__format__ (__printf__, 1, 2)));
50}
51
52void rdar8269537(const char *f)
53{
David Blaikiea73cdcb2012-02-10 21:07:25 +000054 test_null_format(false); // expected-warning {{null from a constant boolean}}
55 test_null_format(0); // no-warning
Ted Kremeneke3d8e732012-02-10 19:13:51 +000056 test_null_format(__null); // no-warning
57 test_null_format(f); // expected-warning {{not a string literal}}
58}
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +000059
60int Foo::printf(const char *fmt, ...) {
61 va_list ap;
62 va_start(ap,fmt);
63 const char * const format = fmt;
64 vprintf(format, ap); // no-warning
65
66 const char *format2 = fmt;
67 vprintf(format2, ap); // expected-warning{{format string is not a string literal}}
68
69 return 0;
70}
71
72int Foo::printf2(const char *fmt, ...) {
73 va_list ap;
74 va_start(ap,fmt);
75 vprintf(fmt, ap); // expected-warning{{format string is not a string literal}}
76
77 return 0;
78}