|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -verify=compat,expected -Wunterminated-string-initialization %s -x c |
| 2 | +// RUN: %clang_cc1 -fsyntax-only -verify=cxx -Wunterminated-string-initialization %s -x c++ |
| 3 | + |
| 4 | +#ifndef __cplusplus |
| 5 | +typedef unsigned short char16_t; |
| 6 | +typedef unsigned int char32_t; |
| 7 | +typedef __WCHAR_TYPE__ wchar_t; |
| 8 | +#endif |
| 9 | + |
| 10 | +// C++ is stricter so the following cases should be warned about. In |
| 11 | +// C, the following examples are fine. |
| 12 | + |
| 13 | +char foo3[3] = "fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} |
| 14 | +char foo1[1] = "\0"; // cxx-error {{initializer-string for char array is too long, array size is 1 but initializer has size 2 (including the null terminating character)}} |
| 15 | + |
| 16 | +struct S { |
| 17 | + char buf[3]; |
| 18 | + char fub[3]; |
| 19 | +} s = { "ba\0", "bo\0" }; // cxx-error 2{{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} |
| 20 | + |
| 21 | +#pragma clang diagnostic push |
| 22 | +#pragma clang diagnostic warning "-Wc++-compat" |
| 23 | +// Test different encodings: |
| 24 | +signed char scfoo[3] = "fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 25 | + compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} |
| 26 | +unsigned char ucfoo[3] = "fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 27 | + compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} |
| 28 | +wchar_t wcfoo[3] = L"fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 29 | + compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 30 | + compat-warning {{identifier 'wchar_t' conflicts with a C++ keyword}} |
| 31 | +char16_t c16foo[3] = u"fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 32 | + compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 33 | + compat-warning {{identifier 'char16_t' conflicts with a C++ keyword}} |
| 34 | +char32_t c32foo[3] = U"fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 35 | + compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \ |
| 36 | + compat-warning {{identifier 'char32_t' conflicts with a C++ keyword}} |
| 37 | +#pragma clang diagnostic pop |
| 38 | + |
| 39 | +// Test list initializer: |
| 40 | +signed char scfoo_lst[3] = {'f', 'o', '\0'}; |
| 41 | +unsigned char ucfoo_lst[3] = {'f', 'o', '\0'}; |
| 42 | +wchar_t wcfoo_lst[3] = {L'f', L'o', L'\0'}; |
| 43 | +char16_t c16foo_lst[3] = {u'f', u'o', u'\0'}; |
| 44 | +char32_t c32foo_lst[3] = {U'f', U'o', U'\0'}; |
| 45 | + |
| 46 | +// Declaring an array of size 0 is invalid by C standard but compilers |
| 47 | +// may allow it: |
| 48 | +char a[0] = ""; // expected-warning {{initializer-string for character array is too long, array size is 0 but initializer has size 1 (including the null terminating character); did you mean to use the 'nonstring' attribute?}} \ |
| 49 | + cxx-error {{initializer-string for char array is too long, array size is 0 but initializer has size 1 (including the null terminating character)}} |
| 50 | +char b[1] = ""; // no warn |
0 commit comments