(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
uninit-32.c
       1  /* PR middle-end/10138 - warn for uninitialized arrays passed as const*
       2     arguments
       3     { dg-do compile }
       4     { dg-options "-O -Wall" }
       5     { dg-require-effective-target alloca } */
       6  
       7  typedef __SIZE_TYPE__ size_t;
       8  
       9  void* alloca (size_t);
      10  void* malloc (size_t);
      11  void* realloc (void*, size_t);
      12  
      13  void fpi (int*);
      14  void fpci (const int*);
      15  void fpcv (const void*);
      16  
      17  
      18  void nowarn_scalar_fpi (void)
      19  {
      20    int x;
      21    fpi (&x);
      22  }
      23  
      24  void nowarn_scalar_plus_cst_fpi (void)
      25  {
      26    int x;
      27    // This deserves a warning other than -Wuninitialized.
      28    fpi (&x + 1);
      29  }
      30  
      31  void nowarn_scalar_plus_var_fpi (int i)
      32  {
      33    int x;
      34    // Same as above, this deserves a warning other than -Wuninitialized.
      35    fpi (&x + i);
      36  }
      37  
      38  void nowarn_array_assign_fpci (void)
      39  {
      40    int a[2];
      41    a[0] = 0;
      42    fpci (a);
      43  }
      44  
      45  void nowarn_array_assign_plus_cst_fpci (void)
      46  {
      47    int a[4];
      48    a[1] = 0;
      49    a[2] = 1;
      50    fpci (a + 1);
      51  }
      52  
      53  void nowarn_array_init_fpci (void)
      54  {
      55    int a[4] = { 0 };
      56    fpci (a);
      57  }
      58  
      59  void nowarn_array_compound_fpi (void)
      60  {
      61    fpi ((int[2]){ 1 });
      62  }
      63  
      64  void nowarn_array_compound_fpci (void)
      65  {
      66    fpci ((int[3]){ 1 });
      67  }
      68  
      69  void warn_array_fpci (void)
      70  {
      71    int a[4];                   // { dg-message "declared here" }"
      72    fpci (a);                   // { dg-warning "\\\[-Wmaybe-uninitialized" }
      73  }
      74  
      75  void warn_array_plus_cst_fpci (void)
      76  {
      77    int a[4];
      78    fpci (a + 1);               // { dg-warning "\\\[-Wmaybe-uninitialized" }
      79  }
      80  
      81  void warn_array_plus_var_fpci (int i)
      82  {
      83    int a[4];
      84    fpci (a + i);               // { dg-warning "\\\[-Wmaybe-uninitialized" }
      85  }
      86  
      87  void nowarn_array_end_fpci (void)
      88  {
      89    int a[4];
      90    /* This should be diagnosed by a warning other than -Wuninitialized
      91       because the just-past-the-end pointer cannot be dereferenced and
      92       the function doesn't take any other pointer to tell where the start
      93       of the array is.  -Wuninitialized isn't appropriate because there
      94       is nothing to initialize at that offset.  */
      95    fpci (a + 4);
      96  }
      97  
      98  void warn_matrix_fpcv (void)
      99  {
     100    int a[2][2];
     101    fpci (a[1]);                // { dg-warning "\\\[-Wmaybe-uninitialized" }
     102  }
     103  
     104  void warn_scalar_fpcv (void)
     105  {
     106    int i;
     107    fpci (&i);                  // { dg-warning "\\\[-Wmaybe-uninitialized" }
     108  }
     109  
     110  void warn_scalar_plus_cst_fpcv (void)
     111  {
     112    int x;
     113    /* Same as above, this deserves a warning other than -Wuninitialized
     114       for passing the function a past-the-end pointer with no other
     115       argument.  */
     116    fpci (&x + 1);
     117  }
     118  
     119  void warn_scalar_plus_var_fpcv (int i)
     120  {
     121    int x;
     122    fpci (&x + i);              // { dg-warning "\\\[-Wmaybe-uninitialized" }
     123  }
     124  
     125  void nowarn_struct_assign_fpci (void)
     126  {
     127    struct { int a, b; } s;
     128    s.a = 0;
     129    fpci (&s.a);
     130  }
     131  
     132  void warn_struct_assign_fpci (void)
     133  {
     134    struct { int a, b; } s;
     135    s.a = 0;
     136    fpci (&s.b);                // { dg-warning "\\\[-Wmaybe-uninitialized" }
     137  }
     138  
     139  void nowarn_struct_init_fpci (void)
     140  {
     141    struct { int a, b; } s = { 0 };
     142    fpci (&s.a);
     143    fpci (&s.b);
     144  }
     145  
     146  void nowarn_struct_compound_fpci (void)
     147  {
     148    struct S { int a, b; };
     149    fpci (&(struct S){ }.a);
     150    fpci (&(struct S){ }.b);
     151  }
     152  
     153  /* Verify that passing a just-past-the-end pointer to a const pointer
     154     argument to a function that takes another argument is not diagnosed
     155     since the two arguments together could outline a range.  */
     156  void nowarn_fp_p (void)
     157  {
     158    extern void fpi_pci (int*, const int*);
     159  
     160    {
     161      int i;
     162      fpi_pci (&i, &i + 1);
     163    }
     164    {
     165      int j;
     166      fpi_pci (&j + 1, &j + 1);
     167    }
     168  
     169    extern void fpc_pcc (char*, const char*);
     170  
     171    {
     172      char a[2];
     173      fpc_pcc (a, a + 2);
     174    }
     175    {
     176      char a[3];
     177      fpc_pcc (a, a + 3);
     178    }
     179  
     180    extern void fpcc_pcc (const char*, const char*);
     181  
     182    {
     183      char a[4];
     184      fpcc_pcc (a + 4, a + 4);
     185    }
     186  }
     187  
     188  
     189  /* Verify passing addresses of empty uninitialized objects doesn't
     190     trigger a warning.  */
     191  void nowarn_fpcEmpty (void)
     192  {
     193    struct Empty { };
     194    extern void fpcEmpty (const struct Empty*);
     195  
     196    /* Since Empty has no members warning for it isn't really necessary.
     197       See also PR 38908.  */
     198    struct Empty s;
     199    fpcEmpty (&s);
     200  }
     201  
     202  
     203  /* Verify passing addresses of uninitialized objects to functions
     204     declared without a proptotype doesn't trigger a warning.  */
     205  void nowarn_noproto (void)
     206  {
     207    extern void fnoproto ();
     208    int i, a[2];
     209  
     210    fnoproto (&i, a, a + 2);
     211  }
     212  
     213  
     214  /* Verify passing addresses of uninitialized objects to variadic
     215     functions doesn't trigger a warning.  */
     216  void nowarn_vararg (void)
     217  {
     218    extern void fvararg (int, ...);
     219  
     220    int i, a[2];
     221  
     222    fvararg (0, &i, a, a + 2);
     223  }
     224  
     225  
     226  void nowarn_alloca_assign_fpci (unsigned n)
     227  {
     228    int *p = (int*)alloca (n);
     229    p[0] = 0;
     230    fpci (p);
     231  }
     232  
     233  void nowarn_alloca_assign_plus_cst_fpci (unsigned n)
     234  {
     235    int *p = (int*)alloca (n);
     236    p[1] = 0;
     237    p[2] = 1;
     238    fpci (p + 1);
     239  }
     240  
     241  void warn_alloca_fpci (unsigned n)
     242  {
     243    int *p = (int*)alloca (n);
     244    fpci (p);                   // { dg-warning "\\\[-Wmaybe-uninitialized" }
     245  }
     246  
     247  void warn_alloca_assign_plus_cst_fpci (unsigned n)
     248  {
     249    int *p = (int*)alloca (n);
     250    p[1] = 0;
     251    p[2] = 1;
     252    fpci (p + 3);               // { dg-warning "\\\[-Wmaybe-uninitialized" }
     253  }
     254  
     255  
     256  void nowarn_vla_assign_fpci (unsigned n)
     257  {
     258    int a[n];
     259    a[0] = 0;
     260    fpci (a);
     261  }
     262  
     263  void nowarn_vla_assign_plus_cst_fpci (unsigned n)
     264  {
     265    int vla[n];
     266    vla[1] = 0;
     267    vla[2] = 1;
     268    fpci (vla + 1);
     269  }
     270  
     271  void warn_vla_fpci (unsigned n)
     272  {
     273    int vla[n];                 // { dg-message "declared here" "pr?????" { xfail *-*-* } }"
     274    fpci (vla);                 // { dg-warning "\\\[-Wmaybe-uninitialized" }
     275  }
     276  
     277  void warn_vla_assign_plus_cst_fpci (unsigned n)
     278  {
     279    int vla[n];                 // { dg-message "declared here" "pr?????" { xfail *-*-* } }"
     280    vla[1] = 0;
     281    vla[2] = 1;
     282    fpci (vla + 3);             // { dg-warning "\\\[-Wmaybe-uninitialized" }
     283  }
     284  
     285  
     286  void nowarn_malloc_assign_fpci (unsigned n)
     287  {
     288    int *p = (int*)malloc (n);
     289    p[0] = 0;
     290    fpci (p);
     291  }
     292  
     293  void nowarn_malloc_assign_plus_cst_fpci (unsigned n)
     294  {
     295    int *p = (int*)malloc (n);
     296    p[1] = 0;
     297    p[2] = 1;
     298    fpci (p + 1);
     299  }
     300  
     301  void warn_malloc_fpci (unsigned n)
     302  {
     303    int *p = (int*)malloc (n);
     304    fpci (p);                   // { dg-warning "\\\[-Wmaybe-uninitialized" }
     305  }
     306  
     307  void warn_malloc_assign_plus_cst_fpci (unsigned n)
     308  {
     309    int *p = (int*)malloc (n);  // { dg-message "allocated here" "pr?????" { xfail *-*-* } }"
     310    p[1] = 0;
     311    p[2] = 1;
     312    fpci (p + 3);               // { dg-warning "\\\[-Wmaybe-uninitialized" }
     313  }