summaryrefslogtreecommitdiff
path: root/clang/test/Sema/anonymous-struct-union.c
blob: e0822901b0ed01f6632d365c4d20ac05bb14f9d4 (about) (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// RUN: %clang_cc1 -fsyntax-only -verify %s
struct X {
  union {
    float f3;
    double d2;
  } named;

  union {
    int i;
    float f;
    
    union {
      float f2;
      double d;
    };
  };

  struct {
    int a;
    float b;
  };
};

void test_unqual_references(struct X x, const struct X xc) {
  x.i = 0;
  x.f = 0.0;
  x.f2 = x.f;
  x.d = x.f;
  x.f3 = 0; // expected-error{{no member named 'f3'}}
  x.a = 0;

  xc.d = 0.0; // expected-error{{read-only variable is not assignable}}
  xc.f = 0; // expected-error{{read-only variable is not assignable}}
  xc.a = 0; // expected-error{{read-only variable is not assignable}}
}


struct Redecl {
  int x; // expected-note{{previous declaration is here}}
  struct y { };

  union {
    int x; // expected-error{{member of anonymous union redeclares 'x'}}
    float y;
    double z; // expected-note{{previous declaration is here}}
    double zz; // expected-note{{previous declaration is here}}
  };

  int z; // expected-error{{duplicate member 'z'}}
  void zz(); // expected-error{{duplicate member 'zz'}} 
};

union { // expected-warning{{declaration does not declare anything}}
  int int_val;
  float float_val;
};

static union { // expected-warning{{declaration does not declare anything}}
  int int_val2;
  float float_val2;
};

void f() {
  int_val2 = 0; // expected-error{{use of undeclared identifier}}
  float_val2 = 0.0; // expected-error{{use of undeclared identifier}}
}

void g() {
  union { // expected-warning{{declaration does not declare anything}}
    int i;
    float f2;
  };
  i = 0; // expected-error{{use of undeclared identifier}}
  f2 = 0.0; // expected-error{{use of undeclared identifier}}
}

// <rdar://problem/6483159>
struct s0 { union { int f0; }; };

// <rdar://problem/6481130>
typedef struct { }; // expected-warning{{declaration does not declare anything}}

// PR3675
struct s1 {
  int f0; // expected-note{{previous declaration is here}}
  union {
    int f0; // expected-error{{member of anonymous union redeclares 'f0'}}
  };
};

// PR3680
struct {}; // expected-warning{{declaration does not declare anything}}

struct s2 {
  union {
    int a;
  } // expected-warning{{expected ';' at end of declaration list}}
}; // expected-error{{expected member name or ';' after declaration specifiers}}

// Make sure we don't a.k.a. anonymous structs.
typedef struct {
  int x;
} a_struct;
int tmp = (a_struct) { .x = 0 }; // expected-error {{initializing 'int' with an expression of incompatible type 'a_struct'}}

// This example comes out of the C11 standard; make sure we don't accidentally reject it.
struct s {
  struct { int i; };
  int a[];
};