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