summaryrefslogtreecommitdiff
path: root/support/regression/tests/structscope.c
blob: 599af4a2f0b6821704fc8ba4f064f01feb8c92a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Test struct scoping rules

 */

#include <testfwk.h>

/* declare an incomplete struct type */
struct tag2;

struct tag1 {
  struct tag1 *mykind;
  unsigned char x;
  unsigned char y;
  struct tag2 *other;
} s1;

/* complete the previously incomplete type */
struct tag2 {
  unsigned char z;
  struct tag1 *other;
} s2;

void
test_global(void)
{
  s1.mykind = &s1;
  s1.other = &s2;
  s2.other = &s1;
  s1.x = 1;
  s1.y = 2;
  s2.z = 3;
  ASSERT(s2.other->x == 1);
  ASSERT(s1.other->z == 3);
  ASSERT(s1.other->other->y == 2);
  ASSERT(s1.mykind->y == 2);
}

void
test_nested(void)
{
  {
    struct tag1 {
      unsigned char a;
    } ls1;
    ls1.a=1;
  }
  {
    struct tag1 ls2; /* should bind to global level tag1 */
    ls2.x=2;
  }
  {
    struct tag1 {
      unsigned char b;
      struct tag2 *globals2; /* should bind to global level tag2 */
    } ls3;
    ls3.b = 3;
    ls3.globals2 = &s2;
  }
  {
    struct tag2; /* incomplete local tag */
    struct tag1 {
      struct tag2 *locals2; /* should bind to local level s2 tag */
    } ls4;
    struct tag2 {
      unsigned char c;
    } ls5;

    ls4.locals2 = &ls5;
    ls4.locals2->c = 9;
  }
  s1.x = 0;
}