1  /* PR middle-end/10138 - warn for uninitialized arrays passed as const*
       2     arguments
       3     Verify that passing pointers to uninitialized objects to arguments
       4     to functions declared with attribute access is diagnosed where expected.
       5     { dg-do compile }
       6     { dg-options "-O -Wall" } */
       7  
       8  #define RO(...) __attribute__ ((access (read_only, __VA_ARGS__)))
       9  #define RW(...) __attribute__ ((access (read_write, __VA_ARGS__)))
      10  #define WO(...) __attribute__ ((access (write_only, __VA_ARGS__)))
      11  
      12  RO (1) void fpri (int*);      // { dg-message "in a call to 'fpri' declared with attribute 'access \\\(read_only, 1\\\)' here" }
      13  
      14  RO (1) void fpcri (const int*);
      15  
      16  RO (1, 2) void fpcri1_2 (const int*, int);
      17  
      18  
      19  void warn_scalar_fpri (void)
      20  {
      21    int i;                      // { dg-message "declared here" }
      22    fpri (&i);                  // { dg-warning "'i' is used uninitialized" }
      23  }
      24  
      25  void nowarn_scalar_plus_fpri (void)
      26  {
      27    int i;
      28    /* This gets a -Wstringop-overflow for reading past the end but not
      29       -Wuninitialized because there's nothing to initialize there.  */
      30    fpri (&i + 1);              // { dg-warning "\\\[-Wstringop-overread" }
      31  }
      32  
      33  void nowarn_array_assign_fpcri (void)
      34  {
      35    int a[2];
      36    a[0] = 0;
      37    fpcri (a);
      38  }
      39  
      40  void nowarn_array_init_fpcri (void)
      41  {
      42    int a[4] = { 0 };
      43    fpcri (a);
      44  }
      45  
      46  void nowarn_array_compound_fpri (void)
      47  {
      48    fpri ((int[2]){ 0 });
      49  }
      50  
      51  void nowarn_array_compound_fpcri (void)
      52  {
      53    fpcri ((int[3]){ 1 });
      54  }
      55  
      56  void warn_scalar_fpcri (void)
      57  {
      58    int i;
      59    fpcri (&i);                 // { dg-warning "\\\[-Wuninitialized" }
      60  }
      61  
      62  void warn_array_fpcri (void)
      63  {
      64    int a[4];
      65    fpcri (a);                  // { dg-warning "\\\[-Wuninitialized" }
      66  }
      67  
      68  void warn_array_plus_cst_fpcri (void)
      69  {
      70    int a[4];
      71    fpcri (a + 1);              // { dg-warning "\\\[-Wuninitialized" }
      72  }
      73  
      74  void warn_array_plus_var_fpcri (int i)
      75  {
      76    int a[4];
      77    fpcri (a + i);              // { dg-warning "\\\[-Wuninitialized" }
      78  }
      79  
      80  void nowarn_struct_assign_fpcri (void)
      81  {
      82    struct { int a, b; } s;
      83    s.a = 0;
      84    fpcri (&s.a);
      85  }
      86  
      87  void warn_struct_assign_fpcri (void)
      88  {
      89    struct { int a, b; } s;
      90    s.a = 0;
      91    fpcri (&s.b);               // { dg-warning "\\\[-Wuninitialized" }
      92  }
      93  
      94  void nowarn_struct_init_fpcri (void)
      95  {
      96    struct { int a, b; } s = { 0 };
      97    fpcri (&s.a);
      98    fpcri (&s.b);
      99  }
     100  
     101  void nowarn_struct_compound_fpcri (void)
     102  {
     103    struct S { int a, b; };
     104    fpcri (&(struct S){ }.a);
     105    fpcri (&(struct S){ }.b);
     106  }
     107  
     108  
     109  void nowarn_scalar_fpcri1_2 (void)
     110  {
     111    int i;
     112    fpcri1_2 (&i, 0);
     113  }
     114  
     115  void nowarn_array_assign_fpcri1_2 (void)
     116  {
     117    int a[2];
     118    a[0] = 0;
     119    fpcri1_2 (a, 1);
     120  }
     121  
     122  void nowarn_array_assign_fpcri1_2_plus_cst (void)
     123  {
     124    int a[3];
     125    a[1] = 0;
     126    fpcri1_2 (a + 1, 1);
     127  }
     128  
     129  void nowarn_array_init_fpcri1_2 (void)
     130  {
     131    int a[4] = { 0 };
     132    fpcri1_2 (a, 2);
     133  }
     134  
     135  void warn_array_fpcri1_2_rd1 (void)
     136  {
     137    int a[4];
     138    fpcri1_2 (a, 1);            // { dg-warning "\\\[-Wuninitialized" }
     139  }
     140  
     141  void warn_array_fpcri1_2_rd2 (void)
     142  {
     143    int a[4];
     144    fpcri1_2 (a, 2);            // { dg-warning "\\\[-Wuninitialized" }
     145  }