aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/countof.c
blob: 534488501c6af79ce68ef195d6e1c0a7b61822a0 (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
111
112
113
114
115
116
117
118
119
120
/* { dg-do run } */
/* { dg-options "-std=c2y -pedantic-errors" } */

#define assert(e)  ((e) ? (void) 0 : __builtin_abort ())

void
array (void)
{
  short a[7];

  static_assert (_Countof (a) == 7);
  static_assert (_Countof (unsigned [99]) == 99);
}

void
completed (void)
{
  int a[] = {1, 2, 3};

  static_assert (_Countof (a) == 3);
}

void
vla (void)
{
  unsigned n;

  n = 99;
  assert (_Countof (short [n - 10]) == 99 - 10);

  int v[n / 2];
  assert (_Countof (v) == 99 / 2);
}

void
member (void)
{
  struct {
    int a[8];
  } s;

  static_assert (_Countof (s.a) == 8);
}

void
vla_eval (void)
{
  int i;

  i = 7;
  assert (_Countof (struct {int x;}[i++]) == 7);
  assert (i == 7 + 1);

  int v[i];
  int (*p)[i];
  p = &v;
  assert (_Countof (*p++) == i);
  assert (p - 1 == &v);
}

void
array_noeval (void)
{
  long a[5];
  long (*p)[_Countof (a)];

  p = &a;
  static_assert (_Countof (*p++) == 5);
  assert (p == &a);
}

void
matrix_fixed (void)
{
  int i;

  static_assert (_Countof (int [7][4]) == 7);
  i = 3;
  static_assert (_Countof (int [7][i]) == 7);
}

void
matrix_vla (void)
{
  int i, j;

  i = 7;
  assert (_Countof (int [i++][4]) == 7);
  assert (i == 7 + 1);

  i = 9;
  j = 3;
  assert (_Countof (int [i++][j]) == 9);
  assert (i == 9 + 1);
}

void
no_parens(void)
{
  int n = 3;
  int a[7];
  int v[n];

  static_assert (_Countof a == 7); 
  assert (_Countof v == 3); 
}

int
main (void)
{
  array ();
  completed ();
  vla ();
  member ();
  vla_eval ();
  array_noeval ();
  matrix_fixed ();
  matrix_vla ();
  no_parens ();
}