(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
builtin-has-attribute-4.c
       1  /* Verify __builtin_has_attribute return value for variables.
       2     { dg-do compile }
       3     { dg-skip-if "No section attribute" { { hppa*-*-hpux* } && { ! lp64 } } }
       4     { dg-options "-Wall -ftrack-macro-expansion=0" }
       5     { dg-options "-Wall -Wno-narrowing -Wno-unused -ftrack-macro-expansion=0" { target c++ } }
       6     { dg-additional-options "-DSKIP_ALIAS" { target *-*-darwin* } }
       7     { dg-require-visibility "hidden" }
       8  */
       9  
      10  #define ATTR(...) __attribute__ ((__VA_ARGS__))
      11  
      12  #define A(expect, sym, attr)						\
      13    typedef int Assert [1 - 2 * !(__builtin_has_attribute (sym, attr) == expect)]
      14  
      15  int vnone;
      16  
      17  ATTR (aligned) char valigned;
      18  ATTR (aligned (1)) char valigned_1;
      19  ATTR (aligned (2)) char valigned_2;
      20  ATTR (aligned (4)) char valigned_4;
      21  ATTR (aligned (8)) char valigned_8;
      22  
      23  void test_aligned (void)
      24  {
      25    A (0, vnone, aligned);
      26    A (0, vnone, aligned (0));      /* { dg-warning "requested alignment .0. is not a positive power of 2" } */
      27    A (0, vnone, aligned (1));
      28    A (0, vnone, aligned (2));
      29    A (0, vnone, aligned (4));
      30    A (0, vnone, aligned (8));
      31    A (0, vnone, aligned (16));
      32  
      33    A (1, valigned, aligned);
      34    A (0, valigned, aligned (0));   /* { dg-warning "requested alignment .0. is not a positive power of 2" } */
      35    A (0, valigned, aligned (1));
      36    A (0, valigned, aligned (2));
      37  
      38    A (1, valigned_1, aligned);
      39    A (0, valigned_1, aligned (0)); /* { dg-warning "requested alignment .0. is not a positive power of 2" } */
      40    A (1, valigned_1, aligned (1));
      41    A (0, valigned_1, aligned (2));
      42    A (0, valigned_1, aligned (4));
      43  
      44    A (1, valigned_2, aligned);
      45    A (0, valigned_2, aligned (0)); /* { dg-warning "requested alignment .0. is not a positive power of 2" } */
      46    A (0, valigned_2, aligned (1));
      47    A (1, valigned_2, aligned (2));
      48    A (0, valigned_2, aligned (4));
      49  }
      50  
      51  
      52  #ifndef SKIP_ALIAS
      53  int vtarget;
      54  extern ATTR (alias ("vtarget")) int valias;
      55  
      56  void test_alias (void)
      57  {
      58    A (0, vnone, alias);
      59    A (1, valias, alias);
      60    A (1, valias, alias ("vtarget"));
      61    A (0, valias, alias ("vnone"));
      62  }
      63  #endif
      64  
      65  void test_cleanup (void)
      66  {
      67    extern void fpv (void*);
      68    extern void fcleanup (void*);
      69  
      70    int var;
      71    ATTR (cleanup (fcleanup)) int var_cleanup;
      72    A (0, var, cleanup);
      73    A (1, var_cleanup, cleanup);
      74    A (1, var_cleanup, cleanup (fcleanup));
      75    A (0, var_cleanup, cleanup (fpv));
      76  }
      77  
      78  
      79  ATTR (common) int vcommon;
      80  ATTR (nocommon) int vnocommon;
      81  
      82  void test_common (void)
      83  {
      84    A (0, vnone, common);
      85    A (0, vnone, nocommon);
      86  
      87    A (1, vcommon, common);
      88    A (0, vcommon, nocommon);
      89  
      90    A (0, vnocommon, common);
      91    A (1, vnocommon, nocommon);
      92  }
      93  
      94  
      95  void test_externally_visible (void)
      96  {
      97    extern int vexternally_visible;
      98  
      99    A (0, vexternally_visible, externally_visible);
     100  
     101    extern ATTR (externally_visible) int vexternally_visible;
     102  
     103    A (1, vexternally_visible, externally_visible);
     104  }
     105  
     106  
     107  int test_mode (void)
     108  {
     109    ATTR (mode (byte)) int i8;
     110    return __builtin_has_attribute (i8, mode);   /* { dg-warning ".mode. attribute not supported in .__builtin_has_attribute." } */
     111  }
     112  
     113  
     114  void test_nonstring (void)
     115  {
     116    char arr[1];
     117    char* ptr = arr;
     118  
     119    ATTR (nonstring) char arr_nonstring[1];
     120    ATTR (nonstring) char *ptr_nonstring =  arr_nonstring;
     121  
     122    A (0, arr, nonstring);
     123    A (0, ptr, nonstring);
     124  
     125    A (1, arr_nonstring, nonstring);
     126    A (1, ptr_nonstring, nonstring);
     127  }
     128  
     129  struct PackedMember
     130  {
     131    char c;
     132    short s;
     133    int i;
     134    ATTR (packed) int a[2]; /* { dg-warning "attribute ignored" "" { target default_packed } } */
     135  } gpak[2];
     136  
     137  void test_packed (struct PackedMember *p)
     138  {
     139    int vunpacked;
     140    ATTR (packed) int vpacked;   /* { dg-warning ".packed. attribute ignored" } */
     141  
     142    A (0, vunpacked, packed);
     143    A (0, vpacked, packed);
     144  
     145    int arr_unpacked[2];
     146    ATTR (packed) int arr_packed[2];   /* { dg-warning ".packed. attribute ignored" } */
     147  
     148    A (0, arr_unpacked, packed);
     149    A (0, arr_packed, packed);
     150    A (0, arr_unpacked[0], packed);
     151    A (0, arr_packed[0], packed);
     152  
     153    A (0, gpak, packed);
     154    A (0, gpak[0], packed);
     155    A (0, *gpak, packed);
     156    A (0, gpak[0].c, packed);
     157    A (0, gpak[1].s, packed);
     158    A (1, gpak->a, packed);
     159    /* It's the array that's declared packed but not its elements.  */
     160    A (0, (*gpak).a[0], packed);
     161  
     162    /* The following fails because in C it's represented as
     163         INDIRECT_REF (POINTER_PLUS (NOP_EXPR (ADDR_EXPR (gpak)), ...))
     164       with no reference to the member.  Avoid testing it.
     165    A (1, *gpak[9].a, packed);  */
     166  
     167    A (0, p->c, packed);
     168    A (0, p->s, packed);
     169    A (1, p->a, packed);
     170    /* It's the array that's declared packed but not its elements.  */
     171    A (0, p->a[0], packed);
     172    /* Similar to the comment above.
     173     A (1, *p->a, packed);  */
     174  }
     175  
     176  
     177  ATTR (section ("sectA")) int var_sectA;
     178  ATTR (section ("sectB")) int var_sectB;
     179  
     180  void test_section (void)
     181  {
     182    int var = 0;
     183    A (0, var, section);
     184    A (0, var, section ("sectA"));
     185  
     186    A (1, var_sectA, section);
     187    A (1, var_sectA, section ("sectA"));
     188    A (0, var_sectA, section ("sectB"));
     189  
     190    A (1, var_sectB, section);
     191    A (0, var_sectB, section ("sectA"));
     192    A (1, var_sectB, section ("sectB"));
     193  }
     194  
     195  
     196  void test_vector_size (void)
     197  {
     198    char c;
     199    extern int arrx[];
     200    extern int arr1[1];
     201  
     202    A (0, c, vector_size);
     203    A (0, c, vector_size (1));
     204    A (0, arrx, vector_size);
     205    A (0, arrx, vector_size (4));
     206    A (0, arr1, vector_size);
     207    A (0, arr1, vector_size (8));
     208  
     209    ATTR (vector_size (4)) char cv4;
     210    ATTR (vector_size (16)) int iv16;
     211  
     212    A (1, cv4, vector_size);
     213    A (0, cv4, vector_size (1));
     214    A (0, cv4, vector_size (2));
     215    A (1, cv4, vector_size (4));
     216    A (0, cv4, vector_size (8));
     217  
     218    A (1, iv16, vector_size);
     219    A (0, iv16, vector_size (1));
     220    A (0, iv16, vector_size (8));
     221    A (1, iv16, vector_size (16));
     222    A (0, iv16, vector_size (32));
     223  
     224    /* Verify that the attribute not detected on an array of vectors
     225       but is detected on its elements.  */
     226    typedef ATTR (vector_size (8)) float afv8_t[4];
     227    A (0, afv8_t, vector_size);
     228    A (0, afv8_t, vector_size (1));
     229    A (0, afv8_t, vector_size (2));
     230    A (0, afv8_t, vector_size (4));
     231    A (0, afv8_t, vector_size (8));
     232    A (0, afv8_t, vector_size (16));
     233  
     234    A (1, __typeof__ ((*(afv8_t*)0)[0]), vector_size);
     235    A (0, __typeof__ ((*(afv8_t*)0)[1]), vector_size (1));
     236    A (0, __typeof__ ((*(afv8_t*)0)[2]), vector_size (2));
     237    A (0, __typeof__ ((*(afv8_t*)0)[3]), vector_size (4));
     238    A (1, __typeof__ ((*(afv8_t*)0)[0]), vector_size (8));
     239    A (0, __typeof__ ((*(afv8_t*)0)[1]), vector_size (16));
     240  
     241    A (1, __typeof__ (**(afv8_t*)0), vector_size);
     242    A (0, __typeof__ (**(afv8_t*)0), vector_size (1));
     243    A (0, __typeof__ (**(afv8_t*)0), vector_size (2));
     244    A (0, __typeof__ (**(afv8_t*)0), vector_size (4));
     245    A (1, __typeof__ (**(afv8_t*)0), vector_size (8));
     246    A (0, __typeof__ (**(afv8_t*)0), vector_size (16));
     247  
     248    ATTR (vector_size (8)) float afv8[4];
     249    A (0, afv8, vector_size);
     250    A (0, afv8, vector_size (1));
     251    A (0, afv8, vector_size (2));
     252    A (0, afv8, vector_size (4));
     253    A (0, afv8, vector_size (8));
     254    A (0, afv8, vector_size (16));
     255  
     256    A (1, afv8[0], vector_size);
     257    A (0, afv8[1], vector_size (1));
     258    A (0, afv8[2], vector_size (2));
     259    A (0, afv8[3], vector_size (4));
     260    A (1, afv8[0], vector_size (8));
     261    A (0, afv8[1], vector_size (16));
     262  
     263    A (1, *afv8, vector_size);
     264    A (0, *afv8, vector_size (1));
     265    A (0, *afv8, vector_size (2));
     266    A (0, *afv8, vector_size (4));
     267    A (1, *afv8, vector_size (8));
     268    A (0, *afv8, vector_size (16));
     269  
     270    /* sizeof (long double) is 12 on i386.  */
     271    enum { VecSize = 8 * sizeof (long double) };
     272    ATTR (vector_size (VecSize)) long double aldv[1][2][3];
     273    A (0, aldv, vector_size);
     274    A (0, aldv[0], vector_size);
     275    A (0, aldv[0][0], vector_size);
     276    A (1, aldv[0][0][0], vector_size);
     277    A (0, aldv[0][0][1], vector_size (VecSize / 2));
     278    A (1, aldv[0][0][2], vector_size (VecSize));
     279  
     280    A (0, aldv[0][0][0][0], vector_size);
     281  
     282    A (0, *aldv, vector_size);
     283    A (0, **aldv, vector_size);
     284    A (1, ***aldv, vector_size);
     285    A (1, ***aldv, vector_size (VecSize));
     286  }
     287  
     288  
     289  ATTR (visibility ("default")) int vdefault;
     290  ATTR (visibility ("hidden")) int vhidden;
     291  ATTR (visibility ("internal")) int vinternal;
     292  ATTR (visibility ("protected")) int vprotected;
     293  
     294  void test_visibility (void)
     295  {
     296    A (0, vnone, visibility ("default"));
     297    A (0, vnone, visibility ("hidden"));
     298    A (0, vnone, visibility ("internal"));
     299    A (0, vnone, visibility ("protected"));
     300  
     301    A (1, vdefault, visibility ("default"));
     302    A (0, vdefault, visibility ("hidden"));
     303    A (0, vdefault, visibility ("internal"));
     304    A (0, vdefault, visibility ("protected"));
     305  
     306    A (0, vhidden, visibility ("default"));
     307    A (1, vhidden, visibility ("hidden"));
     308    A (0, vhidden, visibility ("internal"));
     309    A (0, vhidden, visibility ("protected"));
     310  
     311    A (0, vinternal, visibility ("default"));
     312    A (0, vinternal, visibility ("hidden"));
     313    A (1, vinternal, visibility ("internal"));
     314    A (0, vinternal, visibility ("protected"));
     315  
     316    A (0, vprotected, visibility ("default"));
     317    A (0, vprotected, visibility ("hidden"));
     318    A (0, vprotected, visibility ("internal"));
     319    A (1, vprotected, visibility ("protected"));
     320  }
     321  
     322  
     323  int var_init_strong = 123;
     324  int var_uninit_strong;
     325  static int var_extern_strong;
     326  static int var_static_strong;
     327  
     328  ATTR (weak) int var_init_weak = 234;
     329  ATTR (weak) int var_uninit_weak;
     330  
     331  void test_weak (void)
     332  {
     333    int var_local = 0;
     334    static int var_static_local = 0;
     335  
     336    A (0, var_init_strong, weak);
     337    A (0, var_uninit_strong, weak);
     338    A (0, var_extern_strong, weak);
     339    A (0, var_static_strong, weak);
     340    A (0, var_local, weak);
     341    A (0, var_static_local, weak);
     342  
     343    A (1, var_init_weak, weak);
     344    A (1, var_uninit_weak, weak);
     345  } /* { dg-warning "protected visibility attribute not supported" "" { target { *-*-darwin* } } } */
     346  
     347  /* { dg-prune-output "specifies less restrictive attribute" } */