From: Charles Li Date: Mon, 13 Feb 2017 18:57:06 +0000 (+0000) Subject: [Test] Make Lit tests C++11 compatible - printf format string X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=76e835515565011fa4000791096a2953f54aafed;p=clang [Test] Make Lit tests C++11 compatible - printf format string Different diagnostics when format string does not match actual arg type. This commit contains the first 2 of 3 tests reviewed in D29685 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@294979 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/SemaCXX/format-strings.cpp b/test/SemaCXX/format-strings.cpp index b7ef1d709f..a0dcf01910 100644 --- a/test/SemaCXX/format-strings.cpp +++ b/test/SemaCXX/format-strings.cpp @@ -1,4 +1,6 @@ // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks -std=c++98 %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks -std=c++11 %s #include @@ -9,9 +11,13 @@ extern int vprintf(const char *restrict, va_list); } void f(char **sp, float *fp) { - scanf("%as", sp); // expected-warning{{'a' length modifier is not supported by ISO C}} + scanf("%as", sp); +#if __cplusplus <= 199711L + // expected-warning@-2 {{'a' length modifier is not supported by ISO C}} +#else + // expected-warning@-4 {{format specifies type 'float *' but the argument has type 'char **'}} +#endif - // TODO: Warn that the 'a' conversion specifier is a C++11 feature. printf("%a", 1.0); scanf("%afoobar", fp); } @@ -46,11 +52,19 @@ void h(int *i) { // Test handling __null for format string literal checking. extern "C" { int test_null_format(const char *format, ...) __attribute__((__format__ (__printf__, 1, 2))); +#if __cplusplus >= 201103L + // expected-note@-2 {{candidate function not viable: no known conversion from 'bool' to 'const char *' for 1st argument}} +#endif } void rdar8269537(const char *f) { - test_null_format(false); // expected-warning {{null from a constant boolean}} + test_null_format(false); +#if __cplusplus <= 199711L + // expected-warning@-2 {{null from a constant boolean}} +#else + // expected-error@-4 {{no matching function for call to 'test_null_format'}} +#endif test_null_format(0); // no-warning test_null_format(__null); // no-warning test_null_format(f); // expected-warning {{not a string literal}} diff --git a/test/SemaCXX/printf-cstr.cpp b/test/SemaCXX/printf-cstr.cpp index a7eeb06b9c..32f4d98a49 100644 --- a/test/SemaCXX/printf-cstr.cpp +++ b/test/SemaCXX/printf-cstr.cpp @@ -1,4 +1,6 @@ // RUN: %clang_cc1 -fsyntax-only -Wformat -verify %s -Wno-error=non-pod-varargs +// RUN: %clang_cc1 -fsyntax-only -Wformat -verify -std=c++98 %s -Wno-error=non-pod-varargs +// RUN: %clang_cc1 -fsyntax-only -Wformat -verify -std=c++11 %s -Wno-error=non-pod-varargs #include @@ -31,12 +33,39 @@ void pod_test() { int n = 10; printf("%d: %s\n", n, hcs.c_str()); - 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?}} - 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 *'}} - 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?}} + printf("%d: %s\n", n, hcs); +#if __cplusplus <= 199711L + // expected-warning@-2 {{cannot pass non-POD object of type 'HasCStr' to variadic function; expected type from format string was 'char *'}} + // expected-note@-3 {{did you mean to call the c_str() method?}} +#else + // expected-warning@-5 {{format specifies type 'char *' but the argument has type 'HasCStr'}} +#endif - 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}} - printf(extstr, hcs, n); // expected-warning{{cannot pass object of non-POD type 'HasCStr' through variadic function}} + printf("%d: %s\n", n, hncs); +#if __cplusplus <= 199711L + // expected-warning@-2 {{cannot pass non-POD object of type 'HasNoCStr' to variadic function; expected type from format string was 'char *'}} +#else + // expected-warning@-4 {{format specifies type 'char *' but the argument has type 'HasNoCStr'}} +#endif + + sprintf(str, "%d: %s", n, hcs); +#if __cplusplus <= 199711L + // expected-warning@-2 {{cannot pass non-POD object of type 'HasCStr' to variadic function; expected type from format string was 'char *'}} + // expected-note@-3 {{did you mean to call the c_str() method?}} +#else + // expected-warning@-5 {{format specifies type 'char *' but the argument has type 'HasCStr'}} +#endif + + printf(formatString, hcs, hncs); +#if __cplusplus <= 199711L + // expected-warning@-2 {{cannot pass object of non-POD type 'HasCStr' through variadic function}} + // expected-warning@-3 {{cannot pass object of non-POD type 'HasNoCStr' through variadic function}} +#endif + + printf(extstr, hcs, n); +#if __cplusplus <= 199711L + // expected-warning@-2 {{cannot pass object of non-POD type 'HasCStr' through variadic function}} +#endif } struct Printf { @@ -49,5 +78,11 @@ void constructor_test() { const char str[] = "test"; HasCStr hcs(str); Printf p("%s %d %s", str, 10, 10); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}} - 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?}} + Printf q("%s %d", hcs, 10); +#if __cplusplus <= 199711L + // expected-warning@-2 {{cannot pass non-POD object of type 'HasCStr' to variadic constructor; expected type from format string was 'char *'}} + // expected-note@-3 {{did you mean to call the c_str() method?}} +#else + // expected-warning@-5 {{format specifies type 'char *' but the argument has type 'HasCStr'}} +#endif }