xref: /aosp_15_r20/external/clang/test/SemaCXX/vararg-class.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -verify -Wclass-varargs -std=c++98 %s
2*67e74705SXin Li // RUN: %clang_cc1 -verify -Wclass-varargs -std=c++11 %s
3*67e74705SXin Li 
4*67e74705SXin Li struct A {};
5*67e74705SXin Li struct B { ~B(); };
6*67e74705SXin Li class C { char *c_str(); };
7*67e74705SXin Li struct D { char *c_str(); };
8*67e74705SXin Li struct E { E(); };
9*67e74705SXin Li struct F { F(); char *c_str(); };
10*67e74705SXin Li 
11*67e74705SXin Li void v(...);
12*67e74705SXin Li void w(const char*, ...) __attribute__((format(printf, 1, 2)));
13*67e74705SXin Li 
test(A a,B b,C c,D d,E e,F f)14*67e74705SXin Li void test(A a, B b, C c, D d, E e, F f) {
15*67e74705SXin Li   v(a); // expected-warning-re {{passing object of class type 'A' through variadic function{{$}}}}
16*67e74705SXin Li   v(b); // expected-error-re {{cannot pass object of non-{{POD|trivial}} type 'B' through variadic function; call will abort at runtime}}
17*67e74705SXin Li   v(c); // expected-warning {{passing object of class type 'C' through variadic function; did you mean to call '.c_str()'?}}
18*67e74705SXin Li   v(d); // expected-warning {{passing object of class type 'D' through variadic function; did you mean to call '.c_str()'?}}
19*67e74705SXin Li   v(e);
20*67e74705SXin Li   v(f);
21*67e74705SXin Li #if __cplusplus < 201103L
22*67e74705SXin Li   // expected-error@-3 {{cannot pass object of non-POD type 'E' through variadic function; call will abort at runtime}}
23*67e74705SXin Li   // expected-error@-3 {{cannot pass object of non-POD type 'F' through variadic function; call will abort at runtime}}
24*67e74705SXin Li #else
25*67e74705SXin Li   // expected-warning-re@-6 {{passing object of class type 'E' through variadic function{{$}}}}
26*67e74705SXin Li   // expected-warning@-6 {{passing object of class type 'F' through variadic function; did you mean to call '.c_str()'?}}
27*67e74705SXin Li #endif
28*67e74705SXin Li 
29*67e74705SXin Li   v(d.c_str());
30*67e74705SXin Li   v(f.c_str());
31*67e74705SXin Li   v(0);
32*67e74705SXin Li   v('x');
33*67e74705SXin Li 
34*67e74705SXin Li   w("%s", a); // expected-warning {{format specifies type 'char *' but the argument has type 'A'}}
35*67e74705SXin Li   w("%s", b); // expected-error-re {{cannot pass non-{{POD|trivial}} object of type 'B' to variadic function; expected type from format string was 'char *'}}
36*67e74705SXin Li   w("%s", c); // expected-warning {{format specifies type 'char *' but the argument has type 'C'}}
37*67e74705SXin Li   w("%s", d); // expected-warning {{format specifies type 'char *' but the argument has type 'D'}}
38*67e74705SXin Li   w("%s", e);
39*67e74705SXin Li   w("%s", f);
40*67e74705SXin Li #if __cplusplus < 201103L
41*67e74705SXin Li   // expected-error@-3 {{cannot pass non-POD object of type 'E' to variadic function; expected type from format string was 'char *'}}
42*67e74705SXin Li   // expected-error@-3 {{cannot pass non-POD object of type 'F' to variadic function; expected type from format string was 'char *'}}
43*67e74705SXin Li   // expected-note@-4 {{did you mean to call the c_str() method?}}
44*67e74705SXin Li #else
45*67e74705SXin Li   // expected-warning@-7 {{format specifies type 'char *' but the argument has type 'E'}}
46*67e74705SXin Li   // expected-warning@-7 {{format specifies type 'char *' but the argument has type 'F'}}
47*67e74705SXin Li #endif
48*67e74705SXin Li }
49