| 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 %s |
| 3 |
// 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 %s |
| 4 |
|
| 5 |
// This should enable nothing. |
| 6 |
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-default-const-init-unsafe %s |
| 7 |
|
| 8 |
// Only unsafe field and variable diagnostics |
| 9 |
// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var,unsafe-field,unsafe-field-restricted-flex -fstrict-flex-arrays=2 %s |
| 10 |
// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var,unsafe-field,unsafe-field-restricted-flex -Wdefault-const-init-unsafe -fstrict-flex-arrays=2 %s |
| 11 |
|
| 12 |
// Only zero init field and variable diagnostics |
| 13 |
// RUN: %clang_cc1 -fsyntax-only -verify=zero-init-var,zero-init-field -Wdefault-const-init -Wno-default-const-init-unsafe %s |
| 14 |
|
| 15 |
// Only zero init and unsafe field diagnostics |
| 16 |
// 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 %s |
| 17 |
|
| 18 |
// Only zero init and unsafe variable diagnostics |
| 19 |
// RUN: %clang_cc1 -fsyntax-only -verify=zero-init-var,unsafe-var -Wno-default-const-init-field-unsafe -Wdefault-const-init-var %s |
| 20 |
|
| 21 |
// Only unsafe field diagnostics, but with flexible arrays set to any kind of array |
| 22 |
// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-field -Wno-default-const-init-var-unsafe -Wdefault-const-init-field-unsafe -fstrict-flex-arrays=0 %s |
| 23 |
|
| 24 |
// C++ tests |
| 25 |
// RUN: %clang_cc1 -fsyntax-only -verify=cxx -x c++ %s |
| 26 |
|
| 27 |
// good-no-diagnostics |
| 28 |
|
| 29 |
struct A { int i; }; |
| 30 |
struct 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}} |
| 33 |
struct T { struct S s; }; // cxx-note {{default constructor of 'T' is implicitly deleted because field 's' has a deleted default constructor}} |
| 34 |
struct U { struct S s; const int j; }; |
| 35 |
struct 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}} |
| 38 |
struct 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}} |
| 41 |
union Z1 { int i; const int j; }; |
| 42 |
union Z2 { int i; const struct A a; }; |
| 43 |
|
| 44 |
void 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 |
} |
| 50 |
void 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 |
} |
| 56 |
void h() { |
| 57 |
struct U u1 = { { 0 } }; |
| 58 |
struct U u2 = { { 0 }, 0 }; |
| 59 |
} |
| 60 |
void 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 |
} |
| 67 |
void 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 |
} |
| 74 |
void z() { |
| 75 |
// Note, we explicitly do not diagnose default initialization of unions with |
| 76 |
// a const member. Those can be reasonable, the only problematic case is if |
| 77 |
// the only member of the union is const, but that's a very odd situation to |
| 78 |
// 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. |
| 84 |
extern const int i; |
| 85 |
const int i = 12; |
| 86 |
|
| 87 |
static 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'}} |
| 89 |
const 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'}} |
| 91 |
const 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'}} |
| 93 |
const 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 |
|
| 96 |
void 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 initialized |
| 105 |
// when a stack-allocated object of the structure type is created. We handle |
| 106 |
// degenerate flexible arrays based on -fstrict-flex-arrays. Note that C++ does |
| 107 |
// not have flexible array members at all, which is why the test is disabled |
| 108 |
// there. |
| 109 |
#ifndef __cplusplus |
| 110 |
struct RealFAM { |
| 111 |
int len; |
| 112 |
const char fam[]; |
| 113 |
}; |
| 114 |
|
| 115 |
struct FakeFAM { |
| 116 |
int len; |
| 117 |
const char fam[0]; |
| 118 |
}; |
| 119 |
|
| 120 |
struct 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 |
|
| 126 |
void 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) |