[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / attr-format.cpp
blobadc05fc46776ca0b00bc5229558a49c6de0b0cf2
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 bool b = false;
76 format("bare string");
77 format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
78 format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format);
79 format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format);
80 format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}}
82 format("%c %c %hhd %hd %d\n", (char)'a', 'a', 'a', (short)123, (int)123);
83 format("%f %f %f\n", (__fp16)123.f, 123.f, 123.);
84 format("%Lf", (__fp16)123.f); // expected-warning{{format specifies type 'long double' but the argument has type '__fp16'}}
85 format("%Lf", 123.f); // expected-warning{{format specifies type 'long double' but the argument has type 'float'}}
86 format("%hhi %hhu %hi %hu %i %u", b, b, b, b, b, b);
87 format("%li", b); // expected-warning{{format specifies type 'long' but the argument has type 'bool'}}
89 struct foo f;
90 format_invalid_nonpod("hello %i", f); // expected-warning{{format specifies type 'int' but the argument has type 'struct foo'}}
92 f.format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
93 f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format);
94 f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format);
95 f.format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}}