[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Sema / attr-format.c
blobd891828a77a4c6e79fb5bd676dfe92fd758bbfaf
1 //RUN: %clang_cc1 -fsyntax-only -verify %s
3 #include <stdarg.h>
5 void a(const char *a, ...) __attribute__((format(printf, 1, 2))); // no-error
6 void b(const char *a, ...) __attribute__((format(printf, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
7 void c(const char *a, ...) __attribute__((format(printf, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
8 void d(const char *a, int c) __attribute__((format(printf, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
9 void e(char *str, int c, ...) __attribute__((format(printf, 2, 3))); // expected-error {{format argument not a string type}}
10 void f(int a, const char *b, ...) __attribute__((format(printf, 2, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
11 void g(int a, const char *b, ...) __attribute__((format(printf, 2, 2))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
12 void h(int a, const char *b, ...) __attribute__((format(printf, 2, 3))); // no-error
13 void i(const char *a, int b, ...) __attribute__((format(printf, 1, 2))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
15 typedef const char *xpto;
16 void j(xpto c, va_list list) __attribute__((format(printf, 1, 0))); // no-error
17 void k(xpto c) __attribute__((format(printf, 1, 0))); // no-error
19 void y(char *str) __attribute__((format(strftime, 1, 0))); // no-error
20 void z(char *str, int c, ...) __attribute__((format(strftime, 1, 2))); // expected-error {{strftime format attribute requires 3rd parameter to be 0}}
22 int (*f_ptr)(char*,...) __attribute__((format(printf, 1,2))); // no-error
23 int (*f2_ptr)(double,...) __attribute__((format(printf, 1, 2))); // expected-error {{format argument not a string type}}
25 struct _mystruct {
26 int (*printf)(const char *format, ...) __attribute__((__format__(printf, 1, 2))); // no-error
27 int (*printf2)(double format, ...) __attribute__((__format__(printf, 1, 2))); // expected-error {{format argument not a string type}}
30 typedef int (*f3_ptr)(char*,...) __attribute__((format(printf,1,0))); // no-error
32 // <rdar://problem/6623513>
33 int rdar6623513(void *, const char*, const char*, ...)
34 __attribute__ ((format (printf, 3, 0)));
36 int rdar6623513_aux(int len, const char* s) {
37 rdar6623513(0, "hello", "%.*s", len, s);
40 // same as format(printf(...))...
41 void a2(const char *a, ...) __attribute__((format(printf0, 1, 2))); // no-error
42 void b2(const char *a, ...) __attribute__((format(printf0, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
43 void c2(const char *a, ...) __attribute__((format(printf0, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
44 void d2(const char *a, int c) __attribute__((format(printf0, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
45 void e2(char *str, int c, ...) __attribute__((format(printf0, 2, 3))); // expected-error {{format argument not a string type}}
47 // FreeBSD usage
48 #define __printf0like(fmt, va) __attribute__((__format__(__printf0__, fmt, va)))
49 void null(int i, const char *a, ...) __printf0like(2, 0); // no-error
50 void null(int i, const char *a, ...) { // expected-note{{passing argument to parameter 'a' here}}
51 if (a)
52 (void)0/* vprintf(...) would go here */;
55 void callnull(void){
56 null(0, 0); // no error
57 null(0, (char*)0); // no error
58 null(0, (void*)0); // no error
59 null(0, (int*)0); // expected-warning {{incompatible pointer types}}
62 // FreeBSD kernel extensions
63 void a3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1, 2))); // no-error
64 void b3(const char *a, ...) __attribute__((format(freebsd_kprintf, 1, 1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
65 void c3(const char *a, ...) __attribute__((format(freebsd_kprintf, 0, 2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
66 void d3(const char *a, int c) __attribute__((format(freebsd_kprintf, 1, 2))); // expected-warning {{GCC requires a function with the 'format' attribute to be variadic}}
67 void e3(char *str, int c, ...) __attribute__((format(freebsd_kprintf, 2, 3))); // expected-error {{format argument not a string type}}
69 // PR4470
70 int xx_vprintf(const char *, va_list);
72 const char *foo(const char *format) __attribute__((format_arg(1)));
74 void __attribute__((format(printf, 1, 0)))
75 foo2(const char *fmt, va_list va) {
76 xx_vprintf(foo(fmt), va);
79 // PR6542
80 extern void gcc_format(const char *, ...)
81 __attribute__((__format__(__gcc_diag__, 1, 2)));
82 extern void gcc_cformat(const char *, ...)
83 __attribute__((__format__(__gcc_cdiag__, 1, 2)));
84 extern void gcc_cxxformat(const char *, ...)
85 __attribute__((__format__(__gcc_cxxdiag__, 1, 2)));
86 extern void gcc_tformat(const char *, ...)
87 __attribute__((__format__(__gcc_tdiag__, 1, 2)));
89 const char *foo3(const char *format) __attribute__((format_arg("foo"))); // expected-error{{'format_arg' attribute requires parameter 1 to be an integer constant}}
91 void call_nonvariadic(void) {
92 d3("%i", 123);
93 d3("%d", 123);
94 d3("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
97 __attribute__((format(printf, 1, 2))) void forward_fixed(const char *fmt, int i) { // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
98 forward_fixed(fmt, i);
99 a(fmt, i);
102 __attribute__((format(printf, 1, 2))) void forward_fixed_2(const char *fmt, int i, int j) { // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
103 forward_fixed_2(fmt, i, j);
104 a(fmt, i);