public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/33738]  New: -Wtype-limits misses a warning when comparing enums
@ 2007-10-11 17:50 dnovillo at gcc dot gnu dot org
  2007-10-11 18:09 ` [Bug c++/33738] " pinskia at gcc dot gnu dot org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2007-10-11 17:50 UTC (permalink / raw)
  To: gcc-bugs

This was found on GCC 4.2.1.  In this test case, VRP quietly folds a comparison
between an enum type and a constant value that the enum type can never take.
With -Wtype-limits, this should give the warning:

comparison always false due to limited range of data type

extern void link_error (void);

enum Alpha {
 ZERO = 0, ONE, TWO, THREE
};

Alpha a2;

int m1 = -1;
int GetM1() {
 return m1;
}

int main() {
 a2 = static_cast<Alpha>(GetM1());
 if (a2 == -1) {                   <-- VRP should warn when folding this.
    link_error ();
 }
 return 0;
}

This is not warned by the front end because we promote -1 to the same type as
a2.  But during VRP, we *do* fold the predicate, so warning when -Wtype-limits
is given in this case would be a good QOI feature.

I have a patch in the works to make VRP warn when it linearizes this predicate.


-- 
           Summary: -Wtype-limits misses a warning when comparing enums
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: dnovillo at gcc dot gnu dot org
        ReportedBy: dnovillo at gcc dot gnu dot org


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
@ 2007-10-11 18:09 ` pinskia at gcc dot gnu dot org
  2007-11-13  4:56 ` manu at gcc dot gnu dot org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-10-11 18:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-10-11 18:09 -------
Warnings from optimizers are semi a no-no.  Yes we have them for strict
aliasing and overflow but I think those cases are a bit weird.  This is
unspecified behavior no matter what and there is no flag to change the behavior
here.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
  2007-10-11 18:09 ` [Bug c++/33738] " pinskia at gcc dot gnu dot org
@ 2007-11-13  4:56 ` manu at gcc dot gnu dot org
  2008-02-05  4:19 ` dnovillo at gcc dot gnu dot org
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: manu at gcc dot gnu dot org @ 2007-11-13  4:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from manu at gcc dot gnu dot org  2007-11-13 04:55 -------
(In reply to comment #1)
> Warnings from optimizers are semi a no-no.  Yes we have them for strict
> aliasing and overflow but I think those cases are a bit weird.  This is
> unspecified behavior no matter what and there is no flag to change the behavior
> here.

Well, Wtype-limits is supposed to warn about this and it doesn't. Can we do it
from the front-end? If not, then warning from the middle-end seems the right
thing to do as long as it is not complex/intrusive.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2007-11-13 04:55:56
               date|                            |


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
  2007-10-11 18:09 ` [Bug c++/33738] " pinskia at gcc dot gnu dot org
  2007-11-13  4:56 ` manu at gcc dot gnu dot org
@ 2008-02-05  4:19 ` dnovillo at gcc dot gnu dot org
  2008-02-05  4:30 ` dnovillo at gcc dot gnu dot org
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2008-02-05  4:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from dnovillo at gcc dot gnu dot org  2008-02-05 04:18 -------
Subject: Bug 33738

Author: dnovillo
Date: Tue Feb  5 04:17:58 2008
New Revision: 132111

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=132111
Log:

        http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00110.html

        PR 33738
        * tree-vrp.c (vrp_evaluate_conditional): With
        -Wtype-limits, emit a warning when comparing against a
        constant outside the natural range of OP0's type.

testsuite/ChangeLog

        PR 33738
        * testsuite/g++.dg/warn/pr33738.C: New.


Added:
    trunk/gcc/testsuite/g++.dg/warn/pr33738.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c


-- 


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (2 preceding siblings ...)
  2008-02-05  4:19 ` dnovillo at gcc dot gnu dot org
@ 2008-02-05  4:30 ` dnovillo at gcc dot gnu dot org
  2008-02-05 10:19 ` rguenth at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2008-02-05  4:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from dnovillo at gcc dot gnu dot org  2008-02-05 04:29 -------

Fixed.  http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00110.html.


-- 

dnovillo at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (3 preceding siblings ...)
  2008-02-05  4:30 ` dnovillo at gcc dot gnu dot org
@ 2008-02-05 10:19 ` rguenth at gcc dot gnu dot org
  2008-02-05 11:22 ` manu at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-02-05 10:19 UTC (permalink / raw)
  To: gcc-bugs



-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
   Target Milestone|---                         |4.3.0


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (4 preceding siblings ...)
  2008-02-05 10:19 ` rguenth at gcc dot gnu dot org
@ 2008-02-05 11:22 ` manu at gcc dot gnu dot org
  2008-02-05 16:16 ` dnovillo at google dot com
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-02-05 11:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from manu at gcc dot gnu dot org  2008-02-05 11:21 -------
You should use OPT_Wtype_limits instead of OPT_Wextra.

Also, the code could simply do:

+      tree op0 = TREE_OPERAND (cond, 0);
+      tree op1 = TREE_OPERAND (cond, 1);
+      tree type = TREE_TYPE (op0);
+      value_range_t *vr0 = get_value_range (op0);
+
+      if (vr0->type != VR_VARYING
+         && INTEGRAL_TYPE_P (type)
+         && vrp_val_is_min (vr0->min)
+         && vrp_val_is_max (vr0->max)
+         && is_gimple_min_invariant (op1))
+       {
+         location_t locus;
+         const char *warnmsg = NULL;
+
+         if (!EXPR_HAS_LOCATION (stmt))
+           locus = input_location;
+         else
+           locus = EXPR_LOCATION (stmt);
+
+         if (integer_zerop (ret))
+           warnmsg = G_("comparison always false due to limited range of "
+                        "data type");
+         else
+           warnmsg = G_("comparison always true due to limited range of "
+                        "data type");
+
+         warning (OPT_Wextra, "%H%s", &locus, warnmsg);
+       }


And BTW, what is G_ for?


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2007-11-13 04:55:56         |2008-02-05 11:21:26
               date|                            |


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (5 preceding siblings ...)
  2008-02-05 11:22 ` manu at gcc dot gnu dot org
@ 2008-02-05 16:16 ` dnovillo at google dot com
  2008-02-05 16:32 ` dnovillo at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dnovillo at google dot com @ 2008-02-05 16:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from dnovillo at google dot com  2008-02-05 16:15 -------
Subject: Re:  -Wtype-limits misses a warning when comparing enums

On 5 Feb 2008 11:21:26 -0000, manu at gcc dot gnu dot org
<gcc-bugzilla@gcc.gnu.org> wrote:

>  You should use OPT_Wtype_limits instead of OPT_Wextra.

Ah, yes, thanks.

>  Also, the code could simply do:

Well, I explicitly wanted to separate the decision making from the
warning machinery.


>  And BTW, what is G_ for?

That's the i18n marker.  I c-n-p it from other messages in the same file.


-- 


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (6 preceding siblings ...)
  2008-02-05 16:16 ` dnovillo at google dot com
@ 2008-02-05 16:32 ` dnovillo at gcc dot gnu dot org
  2008-02-05 19:54 ` reichelt at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2008-02-05 16:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from dnovillo at gcc dot gnu dot org  2008-02-05 16:32 -------
Subject: Bug 33738

Author: dnovillo
Date: Tue Feb  5 16:31:20 2008
New Revision: 132124

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=132124
Log:

        http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00140.html

        PR 33738
        * tree-vrp.c (vrp_evaluate_conditional): Revert fix for
        PR 33738.

testsuite/ChangeLog

        PR 33738
        * g++.dg/warn/pr33738.C: Remove.



Removed:
    trunk/gcc/testsuite/g++.dg/warn/pr33738.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c


-- 


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (7 preceding siblings ...)
  2008-02-05 16:32 ` dnovillo at gcc dot gnu dot org
@ 2008-02-05 19:54 ` reichelt at gcc dot gnu dot org
  2008-02-05 19:57 ` manu at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: reichelt at gcc dot gnu dot org @ 2008-02-05 19:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from reichelt at gcc dot gnu dot org  2008-02-05 19:54 -------
Reopened, since the patch was reverted.


-- 

reichelt at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |
   Target Milestone|4.3.0                       |---


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (8 preceding siblings ...)
  2008-02-05 19:54 ` reichelt at gcc dot gnu dot org
@ 2008-02-05 19:57 ` manu at gcc dot gnu dot org
  2008-02-05 19:59 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-02-05 19:57 UTC (permalink / raw)
  To: gcc-bugs



-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
OtherBugsDependingO|                            |33702
              nThis|                            |
   Target Milestone|---                         |4.3.0


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (9 preceding siblings ...)
  2008-02-05 19:57 ` manu at gcc dot gnu dot org
@ 2008-02-05 19:59 ` pinskia at gcc dot gnu dot org
  2008-02-24 16:42 ` dnovillo at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-02-05 19:59 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu dot
                   |                            |org
   Target Milestone|4.3.0                       |---


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (10 preceding siblings ...)
  2008-02-05 19:59 ` pinskia at gcc dot gnu dot org
@ 2008-02-24 16:42 ` dnovillo at gcc dot gnu dot org
  2008-03-11  5:56 ` dnovillo at gcc dot gnu dot org
  2008-12-29  6:11 ` pinskia at gcc dot gnu dot org
  13 siblings, 0 replies; 15+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2008-02-24 16:42 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from dnovillo at gcc dot gnu dot org  2008-02-24 16:41 -------
Subject: Bug 33738

Author: dnovillo
Date: Sun Feb 24 16:40:32 2008
New Revision: 132591

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=132591
Log:

        http://gcc.gnu.org/ml/gcc-patches/2008-02/msg01094.html

        PR 33738
        * tree-vrp.c (vrp_evaluate_conditional): With
        -Wtype-limits, emit a warning when comparing against a
        constant outside the natural range of OP0's type.
        * c.opt (Wtype-limits): Move ...
        * common.opt (Wtype-limits): ... here.

testsuite/ChangeLog

        PR 33738
        * g++.dg/warn/pr33738.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/warn/pr33738.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c.opt
    trunk/gcc/common.opt
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c


-- 


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (11 preceding siblings ...)
  2008-02-24 16:42 ` dnovillo at gcc dot gnu dot org
@ 2008-03-11  5:56 ` dnovillo at gcc dot gnu dot org
  2008-12-29  6:11 ` pinskia at gcc dot gnu dot org
  13 siblings, 0 replies; 15+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2008-03-11  5:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from dnovillo at gcc dot gnu dot org  2008-03-11 05:56 -------

Fixed on mainline (4.4).


-- 

dnovillo at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED


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


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

* [Bug c++/33738] -Wtype-limits misses a warning when comparing enums
  2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
                   ` (12 preceding siblings ...)
  2008-03-11  5:56 ` dnovillo at gcc dot gnu dot org
@ 2008-12-29  6:11 ` pinskia at gcc dot gnu dot org
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-12-29  6:11 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.4.0


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


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

end of thread, other threads:[~2008-12-29  6:11 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-11 17:50 [Bug c++/33738] New: -Wtype-limits misses a warning when comparing enums dnovillo at gcc dot gnu dot org
2007-10-11 18:09 ` [Bug c++/33738] " pinskia at gcc dot gnu dot org
2007-11-13  4:56 ` manu at gcc dot gnu dot org
2008-02-05  4:19 ` dnovillo at gcc dot gnu dot org
2008-02-05  4:30 ` dnovillo at gcc dot gnu dot org
2008-02-05 10:19 ` rguenth at gcc dot gnu dot org
2008-02-05 11:22 ` manu at gcc dot gnu dot org
2008-02-05 16:16 ` dnovillo at google dot com
2008-02-05 16:32 ` dnovillo at gcc dot gnu dot org
2008-02-05 19:54 ` reichelt at gcc dot gnu dot org
2008-02-05 19:57 ` manu at gcc dot gnu dot org
2008-02-05 19:59 ` pinskia at gcc dot gnu dot org
2008-02-24 16:42 ` dnovillo at gcc dot gnu dot org
2008-03-11  5:56 ` dnovillo at gcc dot gnu dot org
2008-12-29  6:11 ` pinskia at gcc dot gnu 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).