(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
builtin-object-size-22.c
       1  /* PR middle-end/92815 - a variant of gcc.dg/builtin-object-size-20.c
       2     prepared for all targets, irregardless if they pack or not
       3     the structs by default.
       4     { dg-do compile }
       5     { dg-options "-O -Wall -fdump-tree-optimized" } */
       6  
       7  #define ASSERT(expr) ((expr) ? (void)0 : fail (__LINE__))
       8  #define bos0(expr) __builtin_object_size (expr, 1)
       9  #define bos1(expr) __builtin_object_size (expr, 1)
      10  #define bos2(expr) __builtin_object_size (expr, 2)
      11  #define bos3(expr) __builtin_object_size (expr, 3)
      12  
      13  typedef __SIZE_TYPE__  size_t;
      14  
      15  
      16  extern void fail (int);
      17  
      18  
      19  /* Verify sizes of a struct with a flexible array member and no padding.  */
      20  
      21  struct ACX { char n, a[]; };
      22  
      23  struct ACX ac0 = { };
      24  struct ACX ac1 = { 1, { 1 } };
      25  struct ACX ac2 = { 2, { 1, 2 } };
      26  struct ACX ac3 = { 3, { 1, 2, 3 } };
      27  
      28  extern struct ACX eacx;
      29  
      30  void facx (void)
      31  {
      32    ASSERT (bos0 (&ac0) == sizeof ac0);
      33    ASSERT (bos0 (&ac1) == 2);
      34    ASSERT (bos0 (&ac2) == 3);
      35    ASSERT (bos0 (&ac3) == 4);
      36    ASSERT (bos0 (&eacx) == (size_t)-1);
      37  
      38    ASSERT (bos1 (&ac0) == sizeof ac0);
      39    ASSERT (bos1 (&ac1) == 2);
      40    ASSERT (bos1 (&ac2) == 3);
      41    ASSERT (bos1 (&ac3) == 4);
      42    ASSERT (bos1 (&eacx) == (size_t)-1);
      43  
      44    ASSERT (bos2 (&ac0) == sizeof ac0);
      45    ASSERT (bos2 (&ac1) == 2);
      46    ASSERT (bos2 (&ac2) == 3);
      47    ASSERT (bos2 (&ac3) == 4);
      48    ASSERT (bos2 (&eacx) == sizeof eacx);
      49  
      50    ASSERT (bos3 (&ac0) == sizeof ac0);
      51    ASSERT (bos3 (&ac1) == 2);
      52    ASSERT (bos3 (&ac2) == 3);
      53    ASSERT (bos3 (&ac3) == 4);
      54    ASSERT (bos3 (&eacx) == sizeof eacx);
      55  }
      56  
      57  /* Also verify sizes of a struct with a zero length array member.  */
      58  
      59  struct A0C0 { char n, a[0]; };
      60  
      61  struct A0C0 a0c0 = { };
      62  extern struct A0C0 ea0c0;
      63  
      64  void fa0c0 (void)
      65  {
      66    ASSERT (bos0 (&a0c0) == sizeof a0c0);
      67    ASSERT (bos0 (&ea0c0) == sizeof ea0c0);
      68  
      69    ASSERT (bos1 (&a0c0) == sizeof a0c0);
      70    ASSERT (bos1 (&a0c0) == sizeof ea0c0);
      71  
      72    ASSERT (bos2 (&a0c0) == sizeof a0c0);
      73    ASSERT (bos2 (&a0c0) == sizeof ea0c0);
      74  
      75    ASSERT (bos3 (&a0c0) == sizeof a0c0);
      76    ASSERT (bos3 (&a0c0) == sizeof ea0c0);
      77  }
      78  
      79  /* { dg-final { scan-tree-dump-not "fail" "optimized" } } */