1 // RUN: %clang_cc1 -fsyntax-only -Wformat-nonliteral -verify %s 2 #include <stdarg.h> 3 4 int printf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); 5 6 struct S { 7 static void f(const char *, ...) __attribute__((format(printf, 1, 2))); 8 static const char *f2(const char *) __attribute__((format_arg(1))); 9 10 // GCC has a hidden 'this' argument in member functions which is why 11 // the format argument is argument 2 here. 12 void g(const char*, ...) __attribute__((format(printf, 2, 3))); 13 const char* g2(const char*) __attribute__((format_arg(2))); 14 15 void h(const char*, ...) __attribute__((format(printf, 1, 4))); // \ 16 expected-error{{implicit this argument as the format string}} 17 void h2(const char*, ...) __attribute__((format(printf, 2, 1))); // \ 18 expected-error{{out of bounds}} 19 const char* h3(const char*) __attribute__((format_arg(1))); // \ 20 expected-error{{invalid for the implicit this argument}} 21 22 void operator() (const char*, ...) __attribute__((format(printf, 2, 3))); 23 }; 24 25 // PR5521 26 struct A { void a(const char*,...) __attribute((format(printf,2,3))); }; 27 void b(A x) { 28 x.a("%d", 3); 29 } 30 31 // PR8625: correctly interpret static member calls as not having an implicit 32 // 'this' argument. 33 namespace PR8625 { 34 struct S { 35 static void f(const char*, const char*, ...) 36 __attribute__((format(printf, 2, 3))); 37 }; 38 void test(S s, const char* str) { 39 s.f(str, "%s", str); 40 } 41 } 42 43 // Make sure we interpret member operator calls as having an implicit 44 // this argument. 45 void test_operator_call(S s, const char *str) { 46 s("%s", str); 47 } 48 49 template <typename... Args> 50 void format(const char *fmt, Args &&...args) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}} 51 __attribute__((format(printf, 1, 2))); 52 53 template <typename Arg> 54 Arg &expand(Arg &a) { return a; } 55 56 struct foo { 57 int big[10]; 58 foo(); 59 ~foo(); 60 61 template <typename... Args> 62 void format(const char *const fmt, Args &&...args) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}} 63 __attribute__((format(printf, 2, 3))) { 64 printf(fmt, expand(args)...); 65 } 66 }; 67 68 void format_invalid_nonpod(const char *fmt, struct foo f) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}} 69 __attribute__((format(printf, 1, 2))); 70 71 void do_format() { 72 int x = 123; 73 int &y = x; 74 const char *s = "world"; 75 format("bare string"); 76 format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}} 77 format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format); 78 format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format); 79 format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}} 80 81 struct foo f; 82 format_invalid_nonpod("hello %i", f); // expected-warning{{format specifies type 'int' but the argument has type 'struct foo'}} 83 84 f.format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}} 85 f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format); 86 f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format); 87 f.format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}} 88 } 89