xref: /aosp_15_r20/external/clang/test/SemaCXX/attr-format.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Wformat-nonliteral -verify %s
2*67e74705SXin Li struct S {
3*67e74705SXin Li   static void f(const char*, ...) __attribute__((format(printf, 1, 2)));
4*67e74705SXin Li   static const char* f2(const char*) __attribute__((format_arg(1)));
5*67e74705SXin Li 
6*67e74705SXin Li   // GCC has a hidden 'this' argument in member functions which is why
7*67e74705SXin Li   // the format argument is argument 2 here.
8*67e74705SXin Li   void g(const char*, ...) __attribute__((format(printf, 2, 3)));
9*67e74705SXin Li   const char* g2(const char*) __attribute__((format_arg(2)));
10*67e74705SXin Li 
11*67e74705SXin Li   void h(const char*, ...) __attribute__((format(printf, 1, 4))); // \
12*67e74705SXin Li       expected-error{{implicit this argument as the format string}}
13*67e74705SXin Li   void h2(const char*, ...) __attribute__((format(printf, 2, 1))); // \
14*67e74705SXin Li       expected-error{{out of bounds}}
15*67e74705SXin Li   const char* h3(const char*) __attribute__((format_arg(1))); // \
16*67e74705SXin Li       expected-error{{invalid for the implicit this argument}}
17*67e74705SXin Li 
18*67e74705SXin Li   void operator() (const char*, ...) __attribute__((format(printf, 2, 3)));
19*67e74705SXin Li };
20*67e74705SXin Li 
21*67e74705SXin Li // PR5521
22*67e74705SXin Li struct A { void a(const char*,...) __attribute((format(printf,2,3))); };
b(A x)23*67e74705SXin Li void b(A x) {
24*67e74705SXin Li   x.a("%d", 3);
25*67e74705SXin Li }
26*67e74705SXin Li 
27*67e74705SXin Li // PR8625: correctly interpret static member calls as not having an implicit
28*67e74705SXin Li // 'this' argument.
29*67e74705SXin Li namespace PR8625 {
30*67e74705SXin Li   struct S {
31*67e74705SXin Li     static void f(const char*, const char*, ...)
32*67e74705SXin Li       __attribute__((format(printf, 2, 3)));
33*67e74705SXin Li   };
test(S s,const char * str)34*67e74705SXin Li   void test(S s, const char* str) {
35*67e74705SXin Li     s.f(str, "%s", str);
36*67e74705SXin Li   }
37*67e74705SXin Li }
38*67e74705SXin Li 
39*67e74705SXin Li // Make sure we interpret member operator calls as having an implicit
40*67e74705SXin Li // this argument.
test_operator_call(S s,const char * str)41*67e74705SXin Li void test_operator_call(S s, const char* str) {
42*67e74705SXin Li   s("%s", str);
43*67e74705SXin Li }
44