52 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify=compat,expected -Wunterminated-string-initialization %s -x c2// RUN: %clang_cc1 -fsyntax-only -verify=cxx -Wunterminated-string-initialization %s -x c++3// REQUIRES: !system-linux4 5#ifndef __cplusplus6typedef __CHAR16_TYPE__ char16_t;7typedef __CHAR32_TYPE__ char32_t;8typedef __WCHAR_TYPE__ wchar_t;9#endif10 11// C++ is stricter so the following cases should be warned about. In12// C, the following examples are fine.13 14char 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)}}15char 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)}}16 17struct S {18 char buf[3];19 char fub[3];20} 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)}}21 22#pragma clang diagnostic push23#pragma clang diagnostic warning "-Wc++-compat"24// Test different encodings:25signed 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)}} \26 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)}}27unsigned 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)}} \28 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)}}29wchar_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)}} \30 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)}} \31 compat-warning {{identifier 'wchar_t' conflicts with a C++ keyword}}32char16_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)}} \33 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)}} \34 compat-warning {{identifier 'char16_t' conflicts with a C++ keyword}}35char32_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)}} \36 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)}} \37 compat-warning {{identifier 'char32_t' conflicts with a C++ keyword}}38#pragma clang diagnostic pop39 40// Test list initializer:41signed char scfoo_lst[3] = {'f', 'o', '\0'};42unsigned char ucfoo_lst[3] = {'f', 'o', '\0'};43wchar_t wcfoo_lst[3] = {L'f', L'o', L'\0'};44char16_t c16foo_lst[3] = {u'f', u'o', u'\0'};45char32_t c32foo_lst[3] = {U'f', U'o', U'\0'};46 47// Declaring an array of size 0 is invalid by C standard but compilers48// may allow it:49char 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?}} \50 cxx-error {{initializer-string for char array is too long, array size is 0 but initializer has size 1 (including the null terminating character)}}51char b[1] = ""; // no warn52