Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | //RUN: %clang_cc1 -fsyntax-only -verify %s |
Ted Kremenek | aa8f976 | 2008-03-07 18:43:49 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdarg.h> |
| 4 | |
| 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-error {{format attribute requires variadic function}} |
| 9 | void e(char *str, int c, ...) __attribute__((format(printf, 2,3))); // expected-error {{format argument not a string type}} |
| 10 | |
| 11 | typedef const char* xpto; |
| 12 | void f(xpto c, va_list list) __attribute__((format(printf, 1, 0))); // no-error |
| 13 | void g(xpto c) __attribute__((format(printf, 1, 0))); // no-error |
| 14 | |
| 15 | void y(char *str) __attribute__((format(strftime, 1,0))); // no-error |
| 16 | void z(char *str, int c, ...) __attribute__((format(strftime, 1,2))); // expected-error {{strftime format attribute requires 3rd parameter to be 0}} |
Nuno Lopes | 8c1a9a8 | 2008-03-25 23:01:48 +0000 | [diff] [blame] | 17 | |
| 18 | int (*f_ptr)(char*,...) __attribute__((format(printf, 1,2))); // no-error |
| 19 | int (*f2_ptr)(double,...) __attribute__((format(printf, 1, 2))); // expected-error {{format argument not a string type}} |
Nuno Lopes | 59b6d5a | 2008-04-18 22:43:39 +0000 | [diff] [blame] | 20 | |
| 21 | struct _mystruct { |
| 22 | int (*printf)(const char *format, ...) __attribute__((__format__(printf, 1, 2))); // no-error |
| 23 | int (*printf2)(double format, ...) __attribute__((__format__(printf, 1, 2))); // expected-error {{format argument not a string type}} |
| 24 | }; |
Ted Kremenek | 72786e0 | 2008-05-09 17:36:24 +0000 | [diff] [blame] | 25 | |
| 26 | typedef int (*f3_ptr)(char*,...) __attribute__((format(printf,1,0))); // no-error |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 27 | |
| 28 | // <rdar://problem/6623513> |
| 29 | int rdar6623513(void *, const char*, const char*, ...) |
| 30 | __attribute__ ((format (printf, 3, 0))); |
| 31 | |
| 32 | int rdar6623513_aux(int len, const char* s) { |
| 33 | rdar6623513(0, "hello", "%.*s", len, s); |
| 34 | } |
Chris Lattner | a87d477 | 2010-03-22 21:05:15 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | |
| 38 | // same as format(printf(...))... |
| 39 | void a2(const char *a, ...) __attribute__((format(printf0, 1,2))); // no-error |
| 40 | void b2(const char *a, ...) __attribute__((format(printf0, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}} |
| 41 | void c2(const char *a, ...) __attribute__((format(printf0, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}} |
| 42 | void d2(const char *a, int c) __attribute__((format(printf0, 1,2))); // expected-error {{format attribute requires variadic function}} |
| 43 | void e2(char *str, int c, ...) __attribute__((format(printf0, 2,3))); // expected-error {{format argument not a string type}} |
| 44 | |
| 45 | // FreeBSD usage |
| 46 | #define __printf0like(fmt,va) __attribute__((__format__(__printf0__,fmt,va))) |
| 47 | void null(int i, const char *a, ...) __printf0like(2,0); // no-error |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 48 | void null(int i, const char *a, ...) { // expected-note{{passing argument to parameter 'a' here}} |
Chris Lattner | a87d477 | 2010-03-22 21:05:15 +0000 | [diff] [blame] | 49 | if (a) |
| 50 | (void)0/* vprintf(...) would go here */; |
| 51 | } |
| 52 | |
| 53 | void callnull(void){ |
| 54 | null(0, 0); // no error |
| 55 | null(0, (char*)0); // no error |
| 56 | null(0, (void*)0); // no error |
| 57 | null(0, (int*)0); // expected-warning {{incompatible pointer types}} |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | // PR4470 |
| 63 | int xx_vprintf(const char *, va_list); |
| 64 | |
| 65 | const char *foo(const char *format) __attribute__((format_arg(1))); |
| 66 | |
| 67 | void __attribute__((format(printf, 1, 0))) |
| 68 | foo2(const char *fmt, va_list va) { |
| 69 | xx_vprintf(foo(fmt), va); |
| 70 | } |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 71 | |
| 72 | // PR6542 |
| 73 | extern void gcc_format (const char *, ...) |
Duncan Sands | bc52595 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 74 | __attribute__ ((__format__(__gcc_diag__, 1, 2))); |
| 75 | extern void gcc_cformat (const char *, ...) |
| 76 | __attribute__ ((__format__(__gcc_cdiag__, 1, 2))); |
| 77 | extern void gcc_cxxformat (const char *, ...) |
| 78 | __attribute__ ((__format__(__gcc_cxxdiag__, 1, 2))); |
| 79 | extern void gcc_tformat (const char *, ...) |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 80 | __attribute__ ((__format__(__gcc_tdiag__, 1, 2))); |