[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / SemaCXX / attr-format.cpp
blobdb9e92be6ce9598d3b3451359b41f54d8490f370
1 // RUN: %clang_cc1 -fsyntax-only -Wformat-nonliteral -verify %s
2 #include <stdarg.h>
4 int printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
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)));
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)));
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}}
22 void operator() (const char*, ...) __attribute__((format(printf, 2, 3)));
25 // PR5521
26 struct A { void a(const char*,...) __attribute((format(printf,2,3))); };
27 void b(A x) {
28 x.a("%d", 3);
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)));
38 void test(S s, const char* str) {
39 s.f(str, "%s", str);
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);
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)));
53 template <typename Arg>
54 Arg &expand(Arg &a) { return a; }
56 struct foo {
57 int big[10];
58 foo();
59 ~foo();
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)...);
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)));
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}}
81 struct foo f;
82 format_invalid_nonpod("hello %i", f); // expected-warning{{format specifies type 'int' but the argument has type 'struct foo'}}
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}}