public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/16302] New: gcc fails to warn about some common logic errors
@ 2004-06-30 19:38 trt at acm dot org
  2004-07-01  1:23 ` [Bug c/16302] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: trt at acm dot org @ 2004-06-30 19:38 UTC (permalink / raw)
  To: gcc-bugs

A "gcc -Wextra -O" misses the 4 bugs in this test program:

int
main (int argc, char *argv[])
{
   if (argc != 1 || argc != 2) return 1;
   if (argc < 0 && argc > 10) return 1;
   if (argc || !argc) return 1;
   if (argc && !argc) return 1;
   return 0;
}

With a tweak to fold-const.c (below), they will be caught:

t.c:4: warning: `or' of collectively exhaustive tests is always 1
t.c:5: warning: `and' of mutually exclusive tests is always 0
t.c:6: warning: `or' of collectively exhaustive tests is always 1
t.c:7: warning: `and' of mutually exclusive tests is always 0

A bootstrap of gcc-3.5-20040627 reports such a warning in fold-const.c itself,
the patch below includes the obvious fix.

====
Comment: The -O is needed due to this code in fold-const.c:

      /* We only do these simplifications if we are optimizing.  */
      if (!optimize)
        return t;

This is probably from when fold-const.c was much simpler,
but I think is no longer appropriate and recommend it be deleted.
====

*** fold-const.c.orig   Sun Jun 27 11:23:44 2004
--- fold-const.c        Wed Jun 30 12:43:39 2004
***************
*** 4377,4379 ****
                                         in_p, low, high))))
!     return or_op ? invert_truthvalue (tem) : tem;
   
--- 4377,4388 ----
                                         in_p, low, high))))
!     {
!       if (TREE_CODE (tem) == INTEGER_CST && extra_warnings)
!         {
!           if (or_op)
!             warning ("`or' of collectively exhaustive tests is always 1");
!           else
!             warning ("`and' of mutually exclusive tests is always 0");
!         }
!       return or_op ? invert_truthvalue (tem) : tem;
!     }
   
***************
*** 9998,10000 ****
          || (TREE_CODE (op0) == REAL_CST
!             && TREE_CODE (op0) != REAL_CST))
        {
--- 10007,10009 ----
          || (TREE_CODE (op0) == REAL_CST
!             && TREE_CODE (op1) != REAL_CST))
        {

-- 
           Summary: gcc fails to warn about some common logic errors
           Product: gcc
           Version: 3.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: trt at acm dot org
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16302


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-04-29 20:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-30 19:38 [Bug c/16302] New: gcc fails to warn about some common logic errors trt at acm dot org
2004-07-01  1:23 ` [Bug c/16302] " pinskia at gcc dot gnu dot org
2004-07-01 15:28 ` trt at acm dot org
2004-07-01 15:53 ` bangerth at dealii dot org
2004-07-01 17:45 ` jsm at polyomino dot org dot uk
2004-07-07 15:36 ` trt at acm dot org
2004-08-03  7:32 ` [Bug middle-end/16302] " pinskia at gcc dot gnu dot org
2004-09-02 18:20 ` trt at acm dot org
2005-04-29 20:20 ` trt at acm dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).