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