File: auto-init-padding-3.c

package info (click to toggle)
gcc-arm-none-eabi 15%3A12.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 959,712 kB
  • sloc: cpp: 3,275,382; ansic: 2,061,766; ada: 840,956; f90: 208,513; makefile: 76,132; asm: 73,433; xml: 50,448; exp: 34,146; sh: 32,436; objc: 15,637; fortran: 14,012; python: 11,991; pascal: 6,787; awk: 4,779; perl: 3,054; yacc: 338; ml: 285; lex: 201; haskell: 122
file content (114 lines) | stat: -rw-r--r-- 3,625 bytes parent folder | download | duplicates (2)
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
/* To test that the compiler can fill all the paddings to zeroes for the 
   structures when the auto variable is partially initialized,  fully 
   initialized, or not initialized for -ftrivial-auto-var-init=pattern.  */
/* { dg-do run } */
/* { dg-options "-ftrivial-auto-var-init=pattern" } */

/* Structure with no padding. */
struct test_packed {
  unsigned long one;
  unsigned long two;
  unsigned long three;
  unsigned long four;
} p1;

/* Simple structure with padding likely to be covered by compiler. */
struct test_small_hole {
  unsigned long one;
  char two;
  /* 3 byte padding hole here. */
  int three;
  unsigned long four;
} sh1;

/* Try to trigger unhandled padding in a structure. */
struct test_aligned {
  unsigned int internal1;
  unsigned long long internal2;
} __attribute__((__aligned__(64)));

struct test_aligned a1;

struct test_big_hole {
  unsigned char one;
  unsigned char two;
  unsigned char three;
  /* 61 byte padding hole here. */
  struct test_aligned four;
} __attribute__((__aligned__(64))); 

struct test_big_hole bh1;

struct test_trailing_hole {
  char *one;
  char *two;
  char *three;
  char four;
  /* "sizeof(unsigned long) - 1" byte padding hole here. */
} th1;

__attribute__((noipa)) void
foo (struct test_packed *p, struct test_small_hole *sh, struct test_aligned *a,
     struct test_big_hole *bh, struct test_trailing_hole *th)
{
  p->one = 1; p->two = 2; p->three = 3; p->four = 4;
  sh->one = 11; sh->two = 12; sh->three = 13; sh->four = 14;
  a->internal1 = 21; a->internal2 = 22;
  bh->one = 31; bh->two = 32; bh->three = 33;
  bh->four.internal1 = 34; bh->four.internal2 = 35; 
  th->one = 0; th->two = 0; th->three = 0; th->four = 44;
}

int main ()
{
  struct test_packed p2;
  struct test_small_hole sh2;
  struct test_aligned a2;
  struct test_big_hole bh2;
  struct test_trailing_hole th2;

  struct test_packed p3 = {.one = 1};
  struct test_small_hole sh3 = {.two = 12};
  struct test_aligned a3 = {.internal1 = 21};
  struct test_big_hole bh3 = {.one = 31};
  struct test_trailing_hole th3 = {.three = 0};

  struct test_packed p4 = {.one = 1, .two = 2, .three = 3, .four = 4};
  struct test_small_hole sh4 = {.one = 11, .two = 12, .three = 13, .four = 14};
  struct test_aligned a4 = {.internal1 = 21, .internal2 = 22};
  struct test_big_hole bh4 = {.one = 31, .two = 32, .three = 33};
  struct test_trailing_hole th4 = {.one = 0, .two = 0, .three = 0, .four = 44};

  foo (&p1, &sh1, &a1, &bh1, &th1);
  foo (&p2, &sh2, &a2, &bh2, &th2);
  foo (&p3, &sh3, &a3, &bh3, &th3);
  bh4.four.internal1 = 34; bh4.four.internal2 = 35;

  __builtin_clear_padding (&p1);
  __builtin_clear_padding (&sh1);
  __builtin_clear_padding (&a1);
  __builtin_clear_padding (&bh1);
  __builtin_clear_padding (&th1);

  if (__builtin_memcmp (&p1, &p2, sizeof (p1))
      || __builtin_memcmp (&sh1, &sh2, sizeof (sh1))
      || __builtin_memcmp (&a1, &a2, sizeof (a1))
      || __builtin_memcmp (&bh1, &bh2, sizeof (bh1))
      || __builtin_memcmp (&th1, &th2, sizeof (th1)))
    __builtin_abort ();
  if (__builtin_memcmp (&p1, &p3, sizeof (p1))
      || __builtin_memcmp (&sh1, &sh3, sizeof (sh1))
      || __builtin_memcmp (&a1, &a3, sizeof (a1))
      || __builtin_memcmp (&bh1, &bh3, sizeof (bh1))
      || __builtin_memcmp (&th1, &th3, sizeof (th1)))
    __builtin_abort ();
  if (__builtin_memcmp (&p1, &p4, sizeof (p1))
      || __builtin_memcmp (&sh1, &sh4, sizeof (sh1))
      || __builtin_memcmp (&a1, &a4, sizeof (a1))
      || __builtin_memcmp (&bh1, &bh4, sizeof (bh1))
      || __builtin_memcmp (&th1, &th4, sizeof (th1)))
    __builtin_abort ();


  return 0;
}