89 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -Wformat -verify %s -Wno-error=non-pod-varargs2// RUN: %clang_cc1 -fsyntax-only -Wformat -verify -std=c++98 %s -Wno-error=non-pod-varargs3// RUN: %clang_cc1 -fsyntax-only -Wformat -verify -std=c++11 %s -Wno-error=non-pod-varargs4 5#include <stdarg.h>6 7extern "C" {8extern int printf(const char *restrict, ...);9extern int sprintf(char *, const char *restrict, ...);10}11 12class HasCStr {13 const char *str;14 public:15 HasCStr(const char *s): str(s) { }16 const char *c_str() {return str;}17};18 19class HasNoCStr {20 const char *str;21 public:22 HasNoCStr(const char *s): str(s) { }23 const char *not_c_str() {return str;}24};25 26extern const char extstr[16];27void pod_test() {28 char str[] = "test";29 char dest[32];30 char formatString[] = "non-const %s %s";31 HasCStr hcs(str);32 HasNoCStr hncs(str);33 int n = 10;34 35 printf("%d: %s\n", n, hcs.c_str());36 printf("%d: %s\n", n, hcs);37#if __cplusplus <= 199711L38 // expected-warning@-2 {{cannot pass non-POD object of type 'HasCStr' to variadic function; expected type from format string was 'char *'}}39 // expected-note@-3 {{did you mean to call the c_str() method?}}40#else41 // expected-warning@-5 {{format specifies type 'char *' but the argument has type 'HasCStr'}}42#endif43 44 printf("%d: %s\n", n, hncs);45#if __cplusplus <= 199711L46 // expected-warning@-2 {{cannot pass non-POD object of type 'HasNoCStr' to variadic function; expected type from format string was 'char *'}}47#else48 // expected-warning@-4 {{format specifies type 'char *' but the argument has type 'HasNoCStr'}}49#endif50 51 sprintf(str, "%d: %s", n, hcs);52#if __cplusplus <= 199711L53 // expected-warning@-2 {{cannot pass non-POD object of type 'HasCStr' to variadic function; expected type from format string was 'char *'}}54 // expected-note@-3 {{did you mean to call the c_str() method?}}55#else56 // expected-warning@-5 {{format specifies type 'char *' but the argument has type 'HasCStr'}}57#endif58 59 printf(formatString, hcs, hncs);60#if __cplusplus <= 199711L61 // expected-warning@-2 {{cannot pass object of non-POD type 'HasCStr' through variadic function}}62 // expected-warning@-3 {{cannot pass object of non-POD type 'HasNoCStr' through variadic function}}63#endif64 65 printf(extstr, hcs, n);66#if __cplusplus <= 199711L67 // expected-warning@-2 {{cannot pass object of non-POD type 'HasCStr' through variadic function}}68#endif69}70 71struct Printf {72 Printf();73 Printf(const Printf&);74 Printf(const char *,...) __attribute__((__format__(__printf__,2,3)));75};76 77void constructor_test() {78 const char str[] = "test";79 HasCStr hcs(str);80 Printf p("%s %d %s", str, 10, 10); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}81 Printf q("%s %d", hcs, 10);82#if __cplusplus <= 199711L83 // expected-warning@-2 {{cannot pass non-POD object of type 'HasCStr' to variadic constructor; expected type from format string was 'char *'}}84 // expected-note@-3 {{did you mean to call the c_str() method?}}85#else86 // expected-warning@-5 {{format specifies type 'char *' but the argument has type 'HasCStr'}}87#endif88}89