133 lines · c
1// Both of these should enable everything.2// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var-compat,unsafe-field-compat,zero-init-var,zero-init-field -Wc++-compat -Wno-tentative-definition-compat -fstrict-flex-arrays=2 %s3// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var,unsafe-field,zero-init-var,zero-init-field,unsafe-field-restricted-flex -Wdefault-const-init -fstrict-flex-arrays=2 %s4 5// This should enable nothing.6// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-default-const-init-unsafe %s7 8// Only unsafe field and variable diagnostics9// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var,unsafe-field,unsafe-field-restricted-flex -fstrict-flex-arrays=2 %s10// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var,unsafe-field,unsafe-field-restricted-flex -Wdefault-const-init-unsafe -fstrict-flex-arrays=2 %s11 12// Only zero init field and variable diagnostics13// RUN: %clang_cc1 -fsyntax-only -verify=zero-init-var,zero-init-field -Wdefault-const-init -Wno-default-const-init-unsafe %s14 15// Only zero init and unsafe field diagnostics16// RUN: %clang_cc1 -fsyntax-only -verify=zero-init-field,unsafe-field,unsafe-field-restricted-flex -Wno-default-const-init-var-unsafe -Wdefault-const-init-field -fstrict-flex-arrays=2 %s17 18// Only zero init and unsafe variable diagnostics19// RUN: %clang_cc1 -fsyntax-only -verify=zero-init-var,unsafe-var -Wno-default-const-init-field-unsafe -Wdefault-const-init-var %s20 21// Only unsafe field diagnostics, but with flexible arrays set to any kind of array22// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-field -Wno-default-const-init-var-unsafe -Wdefault-const-init-field-unsafe -fstrict-flex-arrays=0 %s23 24// C++ tests25// RUN: %clang_cc1 -fsyntax-only -verify=cxx -x c++ %s26 27// good-no-diagnostics28 29struct A { int i; };30struct S{ const int i; }; // unsafe-field-note 2 {{member 'i' declared 'const' here}} \31 unsafe-field-compat-note 2 {{member 'i' declared 'const' here}} \32 cxx-note 3 {{default constructor of 'S' is implicitly deleted because field 'i' of const-qualified type 'const int' would not be initialized}}33struct T { struct S s; }; // cxx-note {{default constructor of 'T' is implicitly deleted because field 's' has a deleted default constructor}}34struct U { struct S s; const int j; };35struct V { int i; const struct A a; }; // unsafe-field-note {{member 'a' declared 'const' here}} \36 unsafe-field-compat-note {{member 'a' declared 'const' here}} \37 cxx-note {{default constructor of 'V' is implicitly deleted because field 'a' of const-qualified type 'const struct A' would not be initialized}}38struct W { struct A a; const int j; }; // unsafe-field-note {{member 'j' declared 'const' here}} \39 unsafe-field-compat-note {{member 'j' declared 'const' here}} \40 cxx-note {{default constructor of 'W' is implicitly deleted because field 'j' of const-qualified type 'const int' would not be initialized}}41union Z1 { int i; const int j; };42union Z2 { int i; const struct A a; };43 44void f() {45 struct S s1; // unsafe-field-warning {{default initialization of an object of type 'struct S' with const member leaves the object uninitialized}} \46 unsafe-field-compat-warning {{default initialization of an object of type 'struct S' with const member leaves the object uninitialized and is incompatible with C++}} \47 cxx-error {{call to implicitly-deleted default constructor of 'struct S'}}48 struct S s2 = { 0 };49}50void g() {51 struct T t1; // unsafe-field-warning {{default initialization of an object of type 'struct T' with const member leaves the object uninitialized}} \52 unsafe-field-compat-warning {{default initialization of an object of type 'struct T' with const member leaves the object uninitialized and is incompatible with C++}} \53 cxx-error {{call to implicitly-deleted default constructor of 'struct T'}}54 struct T t2 = { { 0 } };55}56void h() {57 struct U u1 = { { 0 } };58 struct U u2 = { { 0 }, 0 };59}60void x() {61 struct V v1; // unsafe-field-warning {{default initialization of an object of type 'struct V' with const member leaves the object uninitialized}} \62 unsafe-field-compat-warning {{default initialization of an object of type 'struct V' with const member leaves the object uninitialized and is incompatible with C++}} \63 cxx-error {{call to implicitly-deleted default constructor of 'struct V'}}64 struct V v2 = { 0 };65 struct V v3 = { 0, { 0 } };66}67void y() {68 struct W w1; // unsafe-field-warning {{default initialization of an object of type 'struct W' with const member leaves the object uninitialized}} \69 unsafe-field-compat-warning {{default initialization of an object of type 'struct W' with const member leaves the object uninitialized and is incompatible with C++}} \70 cxx-error {{call to implicitly-deleted default constructor of 'struct W'}}71 struct W w2 = { 0 };72 struct W w3 = { { 0 }, 0 };73}74void z() {75 // Note, we explicitly do not diagnose default initialization of unions with76 // a const member. Those can be reasonable, the only problematic case is if77 // the only member of the union is const, but that's a very odd situation to78 // begin with so we accept it as a false negative.79 union Z1 z1;80 union Z2 z2;81}82 83// Test a tentative definition which does eventually get an initializer.84extern const int i;85const int i = 12;86 87static const int j; // zero-init-var-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \88 cxx-error {{default initialization of an object of const type 'const int'}}89const int k; // zero-init-var-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \90 cxx-error {{default initialization of an object of const type 'const int'}}91const struct S s; // zero-init-var-warning {{default initialization of an object of type 'const struct S' is incompatible with C++}} \92 cxx-error {{call to implicitly-deleted default constructor of 'const struct S'}}93const union Z1 z3; // zero-init-var-warning {{default initialization of an object of type 'const union Z1' is incompatible with C++}} \94 cxx-error {{default initialization of an object of const type 'const union Z1' without a user-provided default constructor}}95 96void func() {97 const int a; // unsafe-var-warning {{default initialization of an object of type 'const int' leaves the object uninitialized}} \98 unsafe-var-compat-warning {{default initialization of an object of type 'const int' leaves the object uninitialized and is incompatible with C++}} \99 cxx-error {{default initialization of an object of const type 'const int'}}100 static const int b; // zero-init-var-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \101 cxx-error {{default initialization of an object of const type 'const int'}}102}103 104// Test the behavior of flexible array members. Those cannot be initialized105// when a stack-allocated object of the structure type is created. We handle106// degenerate flexible arrays based on -fstrict-flex-arrays. Note that C++ does107// not have flexible array members at all, which is why the test is disabled108// there.109#ifndef __cplusplus110struct RealFAM {111 int len;112 const char fam[];113};114 115struct FakeFAM {116 int len;117 const char fam[0];118};119 120struct NotTreatedAsAFAM {121 int len;122 const char fam[1]; // unsafe-field-restricted-flex-note {{member 'fam' declared 'const' here}} \123 unsafe-field-compat-note {{member 'fam' declared 'const' here}}124};125 126void test_fams() {127 struct RealFAM One;128 struct FakeFAM Two;129 struct NotTreatedAsAFAM Three; // unsafe-field-restricted-flex-warning {{default initialization of an object of type 'struct NotTreatedAsAFAM' with const member leaves the object uninitialized}} \130 unsafe-field-compat-warning {{default initialization of an object of type 'struct NotTreatedAsAFAM' with const member leaves the object uninitialized and is incompatible with C++}}131}132#endif // !defined(__cplusplus)133