1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Wformat -verify %s -Wno-error=non-pod-varargs
2*67e74705SXin Li
3*67e74705SXin Li #include <stdarg.h>
4*67e74705SXin Li
5*67e74705SXin Li extern "C" {
6*67e74705SXin Li extern int printf(const char *restrict, ...);
7*67e74705SXin Li extern int sprintf(char *, const char *restrict, ...);
8*67e74705SXin Li }
9*67e74705SXin Li
10*67e74705SXin Li class HasCStr {
11*67e74705SXin Li const char *str;
12*67e74705SXin Li public:
HasCStr(const char * s)13*67e74705SXin Li HasCStr(const char *s): str(s) { }
c_str()14*67e74705SXin Li const char *c_str() {return str;}
15*67e74705SXin Li };
16*67e74705SXin Li
17*67e74705SXin Li class HasNoCStr {
18*67e74705SXin Li const char *str;
19*67e74705SXin Li public:
HasNoCStr(const char * s)20*67e74705SXin Li HasNoCStr(const char *s): str(s) { }
not_c_str()21*67e74705SXin Li const char *not_c_str() {return str;}
22*67e74705SXin Li };
23*67e74705SXin Li
24*67e74705SXin Li extern const char extstr[16];
pod_test()25*67e74705SXin Li void pod_test() {
26*67e74705SXin Li char str[] = "test";
27*67e74705SXin Li char dest[32];
28*67e74705SXin Li char formatString[] = "non-const %s %s";
29*67e74705SXin Li HasCStr hcs(str);
30*67e74705SXin Li HasNoCStr hncs(str);
31*67e74705SXin Li int n = 10;
32*67e74705SXin Li
33*67e74705SXin Li printf("%d: %s\n", n, hcs.c_str());
34*67e74705SXin Li printf("%d: %s\n", n, hcs); // expected-warning{{cannot pass non-POD object of type 'HasCStr' to variadic function; expected type from format string was 'char *'}} expected-note{{did you mean to call the c_str() method?}}
35*67e74705SXin Li printf("%d: %s\n", n, hncs); // expected-warning{{cannot pass non-POD object of type 'HasNoCStr' to variadic function; expected type from format string was 'char *'}}
36*67e74705SXin Li sprintf(str, "%d: %s", n, hcs); // expected-warning{{cannot pass non-POD object of type 'HasCStr' to variadic function; expected type from format string was 'char *'}} expected-note{{did you mean to call the c_str() method?}}
37*67e74705SXin Li
38*67e74705SXin Li printf(formatString, hcs, hncs); // expected-warning{{cannot pass object of non-POD type 'HasCStr' through variadic function}} expected-warning{{cannot pass object of non-POD type 'HasNoCStr' through variadic function}}
39*67e74705SXin Li printf(extstr, hcs, n); // expected-warning{{cannot pass object of non-POD type 'HasCStr' through variadic function}}
40*67e74705SXin Li }
41*67e74705SXin Li
42*67e74705SXin Li struct Printf {
43*67e74705SXin Li Printf();
44*67e74705SXin Li Printf(const Printf&);
45*67e74705SXin Li Printf(const char *,...) __attribute__((__format__(__printf__,2,3)));
46*67e74705SXin Li };
47*67e74705SXin Li
constructor_test()48*67e74705SXin Li void constructor_test() {
49*67e74705SXin Li const char str[] = "test";
50*67e74705SXin Li HasCStr hcs(str);
51*67e74705SXin Li Printf p("%s %d %s", str, 10, 10); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
52*67e74705SXin Li Printf q("%s %d", hcs, 10); // expected-warning {{cannot pass non-POD object of type 'HasCStr' to variadic constructor; expected type from format string was 'char *'}} expected-note{{did you mean to call the c_str() method?}}
53*67e74705SXin Li }
54