1*67e74705SXin Li //RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li
3*67e74705SXin Li #include <stdarg.h>
4*67e74705SXin Li
5*67e74705SXin Li void a(const char *a, ...) __attribute__((format(printf, 1,2))); // no-error
6*67e74705SXin Li void b(const char *a, ...) __attribute__((format(printf, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
7*67e74705SXin Li void c(const char *a, ...) __attribute__((format(printf, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
8*67e74705SXin Li void d(const char *a, int c) __attribute__((format(printf, 1,2))); // expected-error {{format attribute requires variadic function}}
9*67e74705SXin Li void e(char *str, int c, ...) __attribute__((format(printf, 2,3))); // expected-error {{format argument not a string type}}
10*67e74705SXin Li
11*67e74705SXin Li typedef const char* xpto;
12*67e74705SXin Li void f(xpto c, va_list list) __attribute__((format(printf, 1, 0))); // no-error
13*67e74705SXin Li void g(xpto c) __attribute__((format(printf, 1, 0))); // no-error
14*67e74705SXin Li
15*67e74705SXin Li void y(char *str) __attribute__((format(strftime, 1,0))); // no-error
16*67e74705SXin Li void z(char *str, int c, ...) __attribute__((format(strftime, 1,2))); // expected-error {{strftime format attribute requires 3rd parameter to be 0}}
17*67e74705SXin Li
18*67e74705SXin Li int (*f_ptr)(char*,...) __attribute__((format(printf, 1,2))); // no-error
19*67e74705SXin Li int (*f2_ptr)(double,...) __attribute__((format(printf, 1, 2))); // expected-error {{format argument not a string type}}
20*67e74705SXin Li
21*67e74705SXin Li struct _mystruct {
22*67e74705SXin Li int (*printf)(const char *format, ...) __attribute__((__format__(printf, 1, 2))); // no-error
23*67e74705SXin Li int (*printf2)(double format, ...) __attribute__((__format__(printf, 1, 2))); // expected-error {{format argument not a string type}}
24*67e74705SXin Li };
25*67e74705SXin Li
26*67e74705SXin Li typedef int (*f3_ptr)(char*,...) __attribute__((format(printf,1,0))); // no-error
27*67e74705SXin Li
28*67e74705SXin Li // <rdar://problem/6623513>
29*67e74705SXin Li int rdar6623513(void *, const char*, const char*, ...)
30*67e74705SXin Li __attribute__ ((format (printf, 3, 0)));
31*67e74705SXin Li
rdar6623513_aux(int len,const char * s)32*67e74705SXin Li int rdar6623513_aux(int len, const char* s) {
33*67e74705SXin Li rdar6623513(0, "hello", "%.*s", len, s);
34*67e74705SXin Li }
35*67e74705SXin Li
36*67e74705SXin Li
37*67e74705SXin Li
38*67e74705SXin Li // same as format(printf(...))...
39*67e74705SXin Li void a2(const char *a, ...) __attribute__((format(printf0, 1,2))); // no-error
40*67e74705SXin Li void b2(const char *a, ...) __attribute__((format(printf0, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
41*67e74705SXin Li void c2(const char *a, ...) __attribute__((format(printf0, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
42*67e74705SXin Li void d2(const char *a, int c) __attribute__((format(printf0, 1,2))); // expected-error {{format attribute requires variadic function}}
43*67e74705SXin Li void e2(char *str, int c, ...) __attribute__((format(printf0, 2,3))); // expected-error {{format argument not a string type}}
44*67e74705SXin Li
45*67e74705SXin Li // FreeBSD usage
46*67e74705SXin Li #define __printf0like(fmt,va) __attribute__((__format__(__printf0__,fmt,va)))
47*67e74705SXin Li void null(int i, const char *a, ...) __printf0like(2,0); // no-error
null(int i,const char * a,...)48*67e74705SXin Li void null(int i, const char *a, ...) { // expected-note{{passing argument to parameter 'a' here}}
49*67e74705SXin Li if (a)
50*67e74705SXin Li (void)0/* vprintf(...) would go here */;
51*67e74705SXin Li }
52*67e74705SXin Li
callnull(void)53*67e74705SXin Li void callnull(void){
54*67e74705SXin Li null(0, 0); // no error
55*67e74705SXin Li null(0, (char*)0); // no error
56*67e74705SXin Li null(0, (void*)0); // no error
57*67e74705SXin Li null(0, (int*)0); // expected-warning {{incompatible pointer types}}
58*67e74705SXin Li }
59*67e74705SXin Li
60*67e74705SXin Li // FreeBSD kernel extensions
61*67e74705SXin Li void a3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1,2))); // no-error
62*67e74705SXin Li void b3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
63*67e74705SXin Li void c3(const char *a, ...) __attribute__((format(freebsd_kprintf, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
64*67e74705SXin Li void d3(const char *a, int c) __attribute__((format(freebsd_kprintf, 1,2))); // expected-error {{format attribute requires variadic function}}
65*67e74705SXin Li void e3(char *str, int c, ...) __attribute__((format(freebsd_kprintf, 2,3))); // expected-error {{format argument not a string type}}
66*67e74705SXin Li
67*67e74705SXin Li
68*67e74705SXin Li
69*67e74705SXin Li // PR4470
70*67e74705SXin Li int xx_vprintf(const char *, va_list);
71*67e74705SXin Li
72*67e74705SXin Li const char *foo(const char *format) __attribute__((format_arg(1)));
73*67e74705SXin Li
74*67e74705SXin Li void __attribute__((format(printf, 1, 0)))
foo2(const char * fmt,va_list va)75*67e74705SXin Li foo2(const char *fmt, va_list va) {
76*67e74705SXin Li xx_vprintf(foo(fmt), va);
77*67e74705SXin Li }
78*67e74705SXin Li
79*67e74705SXin Li // PR6542
80*67e74705SXin Li extern void gcc_format (const char *, ...)
81*67e74705SXin Li __attribute__ ((__format__(__gcc_diag__, 1, 2)));
82*67e74705SXin Li extern void gcc_cformat (const char *, ...)
83*67e74705SXin Li __attribute__ ((__format__(__gcc_cdiag__, 1, 2)));
84*67e74705SXin Li extern void gcc_cxxformat (const char *, ...)
85*67e74705SXin Li __attribute__ ((__format__(__gcc_cxxdiag__, 1, 2)));
86*67e74705SXin Li extern void gcc_tformat (const char *, ...)
87*67e74705SXin Li __attribute__ ((__format__(__gcc_tdiag__, 1, 2)));
88*67e74705SXin Li
89*67e74705SXin Li const char *foo3(const char *format) __attribute__((format_arg("foo"))); // expected-error{{'format_arg' attribute requires parameter 1 to be an integer constant}}
90