summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/builtin-object-size-7.c
blob: 41742b974ef1ce0daa8aea21c38ca4f175d9a790 (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
/* { dg-do run } */
/* { dg-options "-O2" } */

typedef __SIZE_TYPE__ size_t;
extern void *malloc (size_t);
extern void abort (void);

struct A
{
  int i, j, k;
  char buf[255];
  int l, m, n, o;
};

int
main (void)
{
  const size_t s = sizeof (struct A);
  const size_t o = __builtin_offsetof (struct A, buf);
  struct A *a = malloc (s);
  struct A *b = malloc (o + 212);
  if (__builtin_object_size (a->buf, 0) != s - o)
    abort ();
  if (__builtin_object_size (a->buf, 1) != sizeof (a->buf))
    abort ();
  if (__builtin_object_size (a->buf, 2) != s - o)
    abort ();
  if (__builtin_object_size (a->buf, 3) != sizeof (a->buf))
    abort ();
  if (__builtin_object_size (&a->buf[0], 0) != s - o)
    abort ();
  if (__builtin_object_size (&a->buf[0], 1) != sizeof (a->buf))
    abort ();
  if (__builtin_object_size (&a->buf[0], 2) != s - o)
    abort ();
  if (__builtin_object_size (&a->buf[0], 3) != sizeof (a->buf))
    abort ();
  if (__builtin_object_size (&a->buf[6], 0) != s - o - 6)
    abort ();
  if (__builtin_object_size (&a->buf[6], 1) != sizeof (a->buf) - 6)
    abort ();
  if (__builtin_object_size (&a->buf[6], 2) != s - o - 6)
    abort ();
  if (__builtin_object_size (&a->buf[6], 3) != sizeof (a->buf) - 6)
    abort ();
  if (__builtin_object_size (b->buf, 0) != 212)
    abort ();
  if (__builtin_object_size (b->buf, 1) != 212)
    abort ();
  if (__builtin_object_size (b->buf, 2) != 212)
    abort ();
  if (__builtin_object_size (b->buf, 3) != 212)
    abort ();
  if (__builtin_object_size (&b->buf[0], 0) != 212)
    abort ();
  if (__builtin_object_size (&b->buf[0], 1) != 212)
    abort ();
  if (__builtin_object_size (&b->buf[0], 2) != 212)
    abort ();
  if (__builtin_object_size (&b->buf[0], 3) != 212)
    abort ();
  if (__builtin_object_size (&b->buf[28], 0) != 212 - 28)
    abort ();
  if (__builtin_object_size (&b->buf[28], 1) != 212 - 28)
    abort ();
  if (__builtin_object_size (&b->buf[28], 2) != 212 - 28)
    abort ();
  if (__builtin_object_size (&b->buf[28], 3) != 212 - 28)
    abort ();
  return 0;
}