blob: a7eeb06b9c0efe5ae88adc387ff05028e99b2a31 [file] [log] [blame]
Richard Smith831421f2012-06-25 20:30:08 +00001// RUN: %clang_cc1 -fsyntax-only -Wformat -verify %s -Wno-error=non-pod-varargs
2
3#include <stdarg.h>
4
5extern "C" {
6extern int printf(const char *restrict, ...);
7extern int sprintf(char *, const char *restrict, ...);
8}
9
10class HasCStr {
11 const char *str;
12 public:
13 HasCStr(const char *s): str(s) { }
14 const char *c_str() {return str;}
15};
16
17class HasNoCStr {
18 const char *str;
19 public:
20 HasNoCStr(const char *s): str(s) { }
21 const char *not_c_str() {return str;}
22};
23
24extern const char extstr[16];
25void 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
42struct Printf {
43 Printf();
44 Printf(const Printf&);
45 Printf(const char *,...) __attribute__((__format__(__printf__,2,3)));
46};
47
48void 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'}}
Jordan Roseddcfbc92012-07-19 18:10:23 +000052 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?}}
Richard Smith831421f2012-06-25 20:30:08 +000053}