(root)/
gcc-13.2.0/
gcc/
testsuite/
gfortran.dg/
c-interop/
select-errors-c.c
       1  #include <stdlib.h>
       2  #include <stdint.h>
       3  #include <stdio.h>
       4  #include <string.h>
       5  
       6  #include <ISO_Fortran_binding.h>
       7  #include "dump-descriptors.h"
       8  
       9  /* Source is an array of structs.  */
      10  struct ss {
      11    int i, j;
      12    char c[16];
      13    double _Complex dc;
      14  } s[10];
      15  
      16  CFI_index_t extents[] = {10};
      17  
      18  /* External entry point.  */
      19  extern void ctest (void);
      20  
      21  void
      22  ctest (void)
      23  {
      24    int bad = 0;
      25    int status;
      26    CFI_CDESC_T(1) sdesc;
      27    CFI_cdesc_t *source = (CFI_cdesc_t *) &sdesc;
      28    CFI_CDESC_T(3) rdesc;
      29    CFI_cdesc_t *result = (CFI_cdesc_t *) &rdesc;
      30  
      31    /* Create a descriptor for the source array.  */
      32    check_CFI_status ("CFI_establish",
      33  		    CFI_establish (source, (void *)s, CFI_attribute_other,
      34  				   CFI_type_struct,
      35  				   sizeof (struct ss), 1, extents));
      36  
      37    /* The attribute member of result shall have the value
      38       CFI_attribute_other or CFI_attribute_pointer.  */
      39    check_CFI_status ("CFI_establish", 
      40  		    CFI_establish (result, NULL, CFI_attribute_allocatable,
      41  				   CFI_type_int, 0, 1, NULL));
      42    status = CFI_select_part (result, source, offsetof (struct ss, j), 0);
      43    if (status == CFI_SUCCESS)
      44      {
      45        fprintf (stderr,
      46  	       "no error for CFI_attribute_allocatable result\n");
      47        bad ++;
      48      }
      49  
      50    /* The rank member of the result C descriptor shall have the same value
      51       as the rank member of the C descriptor at the address specified
      52       by source.  */
      53    check_CFI_status ("CFI_establish", 
      54  		    CFI_establish (result, NULL, CFI_attribute_pointer,
      55  				   CFI_type_int, 0, 0, NULL));
      56    status = CFI_select_part (result, source, offsetof (struct ss, j), 0);
      57    if (status == CFI_SUCCESS)
      58      {
      59        fprintf (stderr,
      60  	       "no error for rank mismatch (too small)\n");
      61        bad ++;
      62      }
      63  
      64    check_CFI_status ("CFI_establish", 
      65  		    CFI_establish (result, NULL, CFI_attribute_pointer,
      66  				   CFI_type_int, 0, 3, NULL));
      67    status = CFI_select_part (result, source, offsetof (struct ss, j), 0);
      68    if (status == CFI_SUCCESS)
      69      {
      70        fprintf (stderr,
      71  	       "no error for rank mismatch (too large)\n");
      72        bad ++;
      73      }
      74  
      75    /* The value of displacement shall be between 0 and source->elem_len - 1
      76       inclusive. */
      77    check_CFI_status ("CFI_establish", 
      78  		    CFI_establish (result, NULL, CFI_attribute_pointer,
      79  				   CFI_type_int, 0, 1, NULL));
      80    status = CFI_select_part (result, source, -8, 0);
      81    if (status == CFI_SUCCESS)
      82      {
      83        fprintf (stderr,
      84  	       "no error for negative displacement\n");
      85        bad ++;
      86      }
      87    status = CFI_select_part (result, source, source->elem_len, 0);
      88    if (status == CFI_SUCCESS)
      89      {
      90        fprintf (stderr,
      91  	       "no error for too-large displacement\n");
      92        bad ++;
      93      }
      94  
      95    /* source shall be the address of a C descriptor for a nonallocatable
      96       nonpointer array, an allocated allocatable array, or an associated
      97       array pointer.  */
      98    check_CFI_status ("CFI_establish",
      99  		    CFI_establish (source, NULL, CFI_attribute_allocatable,
     100  				   CFI_type_struct,
     101  				   sizeof (struct ss), 1, NULL));
     102    status = CFI_select_part (result, source, offsetof (struct ss, j), 0);
     103    if (status == CFI_SUCCESS)
     104      {
     105        fprintf (stderr,
     106  	       "no error for unallocated allocatable source array\n");
     107        bad ++;
     108      }
     109  
     110    check_CFI_status ("CFI_establish",
     111  		    CFI_establish (source, NULL, CFI_attribute_pointer,
     112  				   CFI_type_struct,
     113  				   sizeof (struct ss), 1, NULL));
     114    status = CFI_select_part (result, source, offsetof (struct ss, j), 0);
     115    if (status == CFI_SUCCESS)
     116      {
     117        fprintf (stderr,
     118  	       "no error for unassociated pointer source array\n");
     119        bad ++;
     120      }
     121  
     122    if (bad)
     123      abort ();
     124  }
     125