(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
pr49706.c
       1  /* PR c/49706 */
       2  /* { dg-do compile } */
       3  /* { dg-options "-Wlogical-not-parentheses" } */
       4  
       5  #ifndef __cplusplus
       6  #define bool _Bool
       7  #endif
       8  enum E { A, B };
       9  bool b;
      10  extern enum E foo_e (void);
      11  extern bool foo_b (void);
      12  extern int foo_i (void);
      13  
      14  #ifdef __cplusplus
      15  template <class T, class U> bool tfn1(T t, U u) { return (!t == u); } /* { dg-warning "logical not is only applied to the left hand side of comparison" "" { target c++ } } */
      16  template <class T, class U> bool tfn2(T t, U u) { return ((!t) == u); }
      17  template <class T, class U> bool tfn3(T t, U u) { return (!g(t) == u); } /* { dg-warning "logical not is only applied to the left hand side of comparison" "" { target c++ } } */
      18  template <class T, class U> bool tfn4(T t, U u) { return ((!g(t)) == u); }
      19  template <class T, class U> bool tfn5(T t, U u) { return (!!t == u); } /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      20  template <class T, class U> bool tfn6(T t, U u) { return (!!g(t) == u); } /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      21  template <int N> bool tfn7(int i1, int i2) { return (!i1 == i2); } /* { dg-warning "logical not is only applied to the left hand side of comparison" "" { target c++ } } */
      22  #endif
      23  
      24  void
      25  fn1 (int i1, int i2, bool b1, bool b2)
      26  {
      27    b = !i1 == i2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      28    b = !i1 != i2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      29    b = !i1 < i2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      30    b = !i1 > i2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      31    b = !i1 <= i2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      32    b = !i1 >= i2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      33  
      34    b = i1 == i2;
      35    b = i1 != i2;
      36    b = i1 < i2;
      37    b = i1 > i2;
      38    b = i1 <= i2;
      39    b = i1 >= i2;
      40  
      41    /* Parens suppress the warning.  */
      42    b = (!i1) == i2;
      43    b = (!i1) != i2;
      44    b = (!i1) < i2;
      45    b = (!i1) > i2;
      46    b = (!i1) <= i2;
      47    b = (!i1) >= i2;
      48  
      49    /* ...but not these parens.  */
      50    b = (!i1 == i2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      51    b = (!i1 != i2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      52    b = (!i1 < i2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      53    b = (!i1 > i2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      54    b = (!i1 <= i2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      55    b = (!i1 >= i2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      56  
      57    b = !b1 == b2;
      58    b = !b1 != b2;
      59    b = !b1 < b2;
      60    b = !b1 > b2;
      61    b = !b1 <= b2;
      62    b = !b1 >= b2;
      63  
      64    b = !b1 == i2;
      65    b = !b1 != i2;
      66    b = !b1 < i2;
      67    b = !b1 > i2;
      68    b = !b1 <= i2;
      69    b = !b1 >= i2;
      70  
      71    b = !foo_i () == i1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      72    b = (!foo_i ()) == i1;
      73    b = !foo_b () == b1;
      74  
      75    b = !!i1 == i2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      76    b = !!i1 != i2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      77    b = !!i1 < i2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      78    b = !!i1 > i2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      79    b = !!i1 <= i2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      80    b = !!i1 >= i2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      81    b = !!foo_i () == i1; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
      82  
      83    b = !!b1 == i2;
      84    b = !!b1 != i2;
      85    b = !!b1 < i2;
      86    b = !!b1 > i2;
      87    b = !!b1 <= i2;
      88    b = !!b1 >= i2;
      89  
      90    /* Be careful here.  */
      91    b = (i1 == 0) != 0;
      92    b = (i1 == 0) == 0;
      93    b = (i1 != 0) != 0;
      94    b = (i1 != 0) == 0;
      95  
      96    b = !5 == 4; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      97    b = !!5 == 4; /* { dg-bogus "logical not is only applied to the left hand side of comparison" "" { xfail *-*-* } } */
      98    b = !1 == 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
      99    b = !!1 == 1; /* { dg-bogus "logical not is only applied to the left hand side of comparison" "" { xfail *-*-* } } */
     100  }
     101  
     102  void
     103  fn2 (enum E e)
     104  {
     105    b = e == B;
     106    b = e == foo_e ();
     107    b = foo_e () == A;
     108    b = foo_e () == foo_e ();
     109  
     110    b = !e == A;
     111    b = !e == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     112    b = !foo_e () == A;
     113    b = !foo_e () == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     114  
     115    b = !(e == A);
     116    b = !(e == foo_e ());
     117    b = !(foo_e () == A);
     118    b = !(foo_e () == foo_e ());
     119  
     120    b = (!e) == A;
     121    b = (!e) == foo_e ();
     122    b = (!foo_e ()) == A;
     123    b = (!foo_e ()) == foo_e ();
     124  }
     125  
     126  void
     127  fn3 (int i1, float f2)
     128  {
     129    b = !i1 == f2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     130    b = !i1 != f2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     131    b = !i1 < f2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     132    b = !i1 > f2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     133    b = !i1 <= f2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     134    b = !i1 >= f2; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     135  
     136    b = i1 == f2;
     137    b = i1 != f2;
     138    b = i1 < f2;
     139    b = i1 > f2;
     140    b = i1 <= f2;
     141    b = i1 >= f2;
     142  
     143    /* Parens suppress the warning.  */
     144    b = (!i1) == f2;
     145    b = (!i1) != f2;
     146    b = (!i1) < f2;
     147    b = (!i1) > f2;
     148    b = (!i1) <= f2;
     149    b = (!i1) >= f2;
     150  
     151    /* ...but not these parens.  */
     152    b = (!i1 == f2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     153    b = (!i1 != f2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     154    b = (!i1 < f2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     155    b = (!i1 > f2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     156    b = (!i1 <= f2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     157    b = (!i1 >= f2); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     158  
     159    b = !!i1 == f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
     160    b = !!i1 != f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
     161    b = !!i1 < f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
     162    b = !!i1 > f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
     163    b = !!i1 <= f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
     164    b = !!i1 >= f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */
     165  }
     166  
     167  void
     168  fn4 (enum E e)
     169  {
     170    b = e == A;
     171    b = e == foo_e ();
     172    b = foo_e () == B;
     173    b = foo_e () == foo_e ();
     174  
     175    b = !e == B; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     176    b = !e == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     177    b = !foo_e () == B; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     178    b = !foo_e () == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
     179  
     180    b = !(e == B);
     181    b = !(e == foo_e ());
     182    b = !(foo_e () == B);
     183    b = !(foo_e () == foo_e ());
     184  
     185    b = (!e) == B;
     186    b = (!e) == foo_e ();
     187    b = (!foo_e ()) == B;
     188    b = (!foo_e ()) == foo_e ();
     189  }