(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
builtin-has-attribute.c
       1  /* Verify __builtin_has_attribute error handling.
       2     { dg-do compile }
       3     { dg-options "-Wall -ftrack-macro-expansion=0" }  */
       4  
       5  #define ATTR(list) __attribute__ (list)
       6  
       7  void fnone (void);
       8  
       9  ATTR ((aligned)) void faligned (void);
      10  ATTR ((aligned (8))) void faligned_8 (void);
      11  
      12  #define has_attr(x, attr)   __builtin_has_attribute (x, attr)
      13  
      14  #define A(expect, sym, attr)						\
      15    typedef int Assert [1 - 2 * !(has_attr (sym, attr) == expect)]
      16  
      17  
      18  int b;
      19  
      20  /* Exercise syntactically invalid arguments.  */
      21  
      22  void test_bad_arguments (void)
      23  {
      24    b = __builtin_has_attribute ();            /* { dg-error "expected \(primary-\)?expression|expected .,." } */
      25    b = __builtin_has_attribute (1);           /* { dg-error "expected .,." } */
      26    b = __builtin_has_attribute (void);        /* { dg-error "expected .,." } */
      27    b = __builtin_has_attribute (foo);         /* { dg-error ".foo. \(undeclared|was not declared\)" } */
      28    /* { dg-error "expected .,." "missing comma" { target *-*-* } .-1 } */
      29  
      30    /* Verify the implementationm doesn't ICE.  */
      31    b = __builtin_has_attribute (foobar, aligned);  /* { dg-error ".foobar. \(undeclared|was not declared\)" } */
      32  
      33    b = __builtin_has_attribute (1, 2, 3);     /* { dg-error "expected identifier" } */
      34    b = __builtin_has_attribute (int, 1 + 2);  /* { dg-error "expected identifier" } */
      35    b = __builtin_has_attribute (2, "aligned"); /* { dg-error "expected identifier" } */
      36  }
      37  
      38  /* Exercise syntactically valid arguments applied in invalid ways.  */
      39  
      40  void test_invalid_arguments (void)
      41  {
      42    b = has_attr (fnone, align);        /* { dg-error "unknown attribute .align." } */
      43    b = has_attr (b, aligned__);        /* { dg-error "unknown attribute .aligned__." } */
      44    b = has_attr (fnone, aligned (3));  /* { dg-error "alignment .3. is not a positive power of 2" } */
      45  
      46    /* Verify the out-of-bounds arguments are diagnosed and the result
      47       of the built-in is false.  */
      48    A (0, fnone, alloc_size (1));       /* { dg-warning "\\\[-Wattributes]" } */
      49    A (0, fnone, alloc_size (2));       /* { dg-warning "\\\[-Wattributes]" } */
      50  
      51    A (0, int, alloc_size (1));         /* { dg-warning ".alloc_size. attribute only applies to function types" } */
      52  
      53    int i = 1;
      54    A (0, i, alloc_size (1));           /* { dg-warning ".alloc_size. attribute only applies to function types" } */
      55  
      56    A (0, faligned_8, aligned (i));     /* { dg-error "alignment is not an integer constant" } */
      57  
      58    typedef ATTR ((aligned (2))) char CA2;
      59    b = has_attr (CA2[2], aligned);     /* { dg-error "alignment of array elements is greater than element size" } */
      60  }