(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wstring-compare-3.c
       1  /* PR middle-end/95673 - missing -Wstring-compare for an impossible strncmp test
       2     { dg-do compile }
       3     { dg-options "-O2 -Wall -Wstring-compare -Wno-stringop-overread -ftrack-macro-expansion=0" } */
       4  
       5  typedef __SIZE_TYPE__ size_t;
       6  
       7  extern int strcmp (const char*, const char*);
       8  extern int strncmp (const char*, const char*, size_t);
       9  
      10  void sink (int, ...);
      11  
      12  extern char a3[3];
      13  
      14  int nowarn_strcmp_one_use_ltz (int c)
      15  {
      16    const char *s = c ? "1234" : a3;
      17    int n = strcmp (s, "123");
      18    return n < 0;
      19  }
      20  
      21  
      22  int nowarn_strcmp_one_use_eqnz (int c)
      23  {
      24    const char *s = c ? "12345" : a3;
      25    int n = strcmp (s, "123");
      26    return n == 1;
      27  }
      28  
      29  
      30  int warn_strcmp_one_use_eqz (int c)
      31  {
      32    const char *s = c ? "123456" : a3;
      33    int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
      34    return n == 0;                // { dg-message "in this expression" }
      35  }
      36  
      37  
      38  int warn_strcmp_one_use_bang (int c)
      39  {
      40    const char *s = c ? "1234567" : a3;
      41    int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
      42    return !n;                    // { dg-message "in this expression" }
      43  }
      44  
      45  
      46  int warn_strcmp_one_use_bang_bang (int c)
      47  {
      48    const char *s = c ? "12345678" : a3;
      49    int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
      50    return !!n;                   // { dg-message "in this expression" }
      51  }
      52  
      53  
      54  _Bool warn_one_use_bool (int c)
      55  {
      56    const char *s = c ? "123456789" : a3;
      57    int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
      58    return (_Bool)n;              // { dg-message "in this expression" }
      59  }
      60  
      61  
      62  int warn_strcmp_one_use_cond (int c)
      63  {
      64    const char *s = c ? "1234567890" : a3;
      65    int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
      66    return n ? 3 : 5;             // { dg-message "in this expression" }
      67  }
      68  
      69  
      70  int nowarn_strcmp_multiple_uses (int c)
      71  {
      72    const char *s = c ? "1234" : a3;
      73    int n = strcmp (s, "123");
      74    sink (n < 0);
      75    sink (n > 0);
      76    sink (n <= 0);
      77    sink (n >= 0);
      78    sink (n + 1);
      79    return n;
      80  }
      81  
      82  
      83  int warn_strcmp_multiple_uses (int c)
      84  {
      85    const char *s = c ? "12345" : a3;
      86    int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
      87    sink (n < 0);
      88    sink (n > 0);
      89    sink (n <= 0);
      90    sink (n >= 0);
      91    sink (n == 0);                // { dg-message "in this expression" }
      92    return n;
      93  }
      94  
      95  
      96  int warn_strncmp_multiple_uses (int c)
      97  {
      98    const char *s = a3;
      99    int n = strncmp (s, "1234", 4); // { dg-warning "'strncmp' of a string of length 4, an array of size 3 and bound of 4 evaluates to nonzero" }
     100    sink (n < 0);
     101    sink (n > 0);
     102    sink (n <= 0);
     103    sink (n >= 0);
     104    sink (n == 0);                // { dg-message "in this expression" }
     105    return n;
     106  }