Clang Project

clang_source_code/test/Sema/assign.c
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3void *test1(void) { return 0; }
4
5void test2 (const struct {int a;} *x) {
6  // expected-note@-1 {{variable 'x' declared const here}}
7
8  x->a = 10;
9  // expected-error-re@-1 {{cannot assign to variable 'x' with const-qualified type 'const struct (anonymous struct at {{.*}}assign.c:5:19) *'}}
10}
11
12typedef int arr[10];
13void test3() {
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 'int const[10]')}}
17  b2[4] = 1;        // expected-error {{cannot assign to variable 'b2' with const-qualified type 'const int [10]'}}
18}
19
20typedef 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;
24typedef struct J {
25  struct I i;
26} J;
27typedef struct K {
28  struct J *j;
29} K;
30
31void testI(struct I i1, struct I i2) {
32  i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}}
33}
34void testJ1(struct J j1, struct J j2) {
35  j1 = j2; // expected-error {{cannot assign to variable 'j1' with nested const-qualified data member 'a'}}
36}
37void 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'}}
39}
40void testK1(struct K k, struct J j) {
41  *(k.j) = j; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'a'}}
42}
43void 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'}}
45}
46
47void testI_(I i1, I i2) {
48  i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}}
49}
50void testJ1_(J j1, J j2) {
51  j1 = j2; // expected-error {{cannot assign to variable 'j1' with nested const-qualified data member 'a'}}
52}
53void 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'}}
55}
56void testK1_(K k, J j) {
57  *(k.j) = j; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'a'}}
58}
59void 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'}}
61}
62
63// PR39946: Recursive checking of hasConstFields caused stack overflow.
64struct 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'}}
66};
67void testL(struct L *l) {
68  *l = 0; // expected-error {{assigning to 'struct L' from incompatible type 'int'}}
69}
70
71// Additionally, this example overflowed the stack when figuring out the field.
72struct M1; // expected-note {{forward declaration of 'struct M1'}}
73struct 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'}}
76};
77struct M1 {
78  struct M2 field;
79};
80
81void testM(struct M1 *l) {
82  *l = 0; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'field'}}
83}
84
85struct N1; // expected-note {{forward declaration of 'struct N1'}}
86struct N2 {
87  struct N1 field; // expected-error {{field has incomplete type 'struct N1'}}
88};
89struct N1 {
90  struct N2 field;
91};
92
93void testN(struct N1 *l) {
94  *l = 0; // expected-error {{assigning to 'struct N1' from incompatible type 'int'}}
95}
96