[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / assign.c
blobfe2a9f14344790d8c578c21350555f71449b1875
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
3 void *test1(void) { return 0; }
5 void test2 (const struct {int a;} *x) {
6 // expected-note@-1 {{variable 'x' declared const here}}
8 x->a = 10;
9 // expected-error-re@-1 {{cannot assign to variable 'x' with const-qualified type 'const struct (unnamed struct at {{.*}}assign.c:5:19) *'}}
12 typedef int arr[10];
13 void test3(void) {
14 const arr b; // expected-note {{variable 'b' declared const here}}
15 const int b2[10]; // expected-note {{variable 'b2' declared const here}}
16 b[4] = 1; // expected-error {{cannot assign to variable 'b' with const-qualified type 'const arr' (aka 'const int[10]')}}
17 b2[4] = 1; // expected-error {{cannot assign to variable 'b2' with const-qualified type 'const int[10]'}}
20 typedef struct I {
21 const int a; // expected-note 4{{nested data member 'a' declared const here}} \
22 expected-note 6{{data member 'a' declared const here}}
23 } I;
24 typedef struct J {
25 struct I i;
26 } J;
27 typedef struct K {
28 struct J *j;
29 } K;
31 void testI(struct I i1, struct I i2) {
32 i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}}
34 void testJ1(struct J j1, struct J j2) {
35 j1 = j2; // expected-error {{cannot assign to variable 'j1' with nested const-qualified data member 'a'}}
37 void testJ2(struct J j, struct I i) {
38 j.i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
40 void testK1(struct K k, struct J j) {
41 *(k.j) = j; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'a'}}
43 void testK2(struct K k, struct I i) {
44 k.j->i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
47 void testI_(I i1, I i2) {
48 i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}}
50 void testJ1_(J j1, J j2) {
51 j1 = j2; // expected-error {{cannot assign to variable 'j1' with nested const-qualified data member 'a'}}
53 void testJ2_(J j, I i) {
54 j.i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
56 void testK1_(K k, J j) {
57 *(k.j) = j; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'a'}}
59 void testK2_(K k, I i) {
60 k.j->i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
63 // PR39946: Recursive checking of hasConstFields caused stack overflow.
64 struct L { // expected-note {{definition of 'struct L' is not complete until the closing '}'}}
65 struct L field; // expected-error {{field has incomplete type 'struct L'}}
67 void testL(struct L *l) {
68 *l = 0; // expected-error {{assigning to 'struct L' from incompatible type 'int'}}
71 // Additionally, this example overflowed the stack when figuring out the field.
72 struct M1; // expected-note {{forward declaration of 'struct M1'}}
73 struct M2 {
74 //expected-note@+1 {{nested data member 'field' declared const here}}
75 const struct M1 field; // expected-error {{field has incomplete type 'const struct M1'}}
77 struct M1 {
78 struct M2 field;
81 void testM(struct M1 *l) {
82 *l = 0; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'field'}}
85 struct N1; // expected-note {{forward declaration of 'struct N1'}}
86 struct N2 {
87 struct N1 field; // expected-error {{field has incomplete type 'struct N1'}}
89 struct N1 {
90 struct N2 field;
93 void testN(struct N1 *l) {
94 *l = 0; // expected-error {{assigning to 'struct N1' from incompatible type 'int'}}