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