public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "manu at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/12242] g++ should warn about out-of-range int->enum conversions Date: Fri, 15 Feb 2008 11:10:00 -0000 [thread overview] Message-ID: <20080215110929.22588.qmail@sourceware.org> (raw) In-Reply-To: <bug-12242-702@http.gcc.gnu.org/bugzilla/> ------- Comment #9 from manu at gcc dot gnu dot org 2008-02-15 11:09 ------- (In reply to comment #0) > enum E { A }; > ... > E e = static_cast<E> (10); > > Yes, i know that result of this is undefined. Is this true? I haven't found any mention of this. > However, previous versions of g++ (and every other c++ compiler i've used) > has implemented this in what seems like the simplest way: the compiler > simply assigns the value `10' to the enum value, regardless of the > fact that this is not a valid value for the enum. We assing 10 in GCC 4.3. > Therefore, the compiler is silently changing the behavior of the > code from what was likely intended. So i think a warning would > be appropriate in this situation. The current verison of 3.4 does > not issue any warning, even with `-Wall -pedantic'. The following patch implements the warning for this testcase. Do you know other cases that may be interesting to warn about? Index: gcc/testsuite/g++.dg/warn/pr12242.C =================================================================== --- gcc/testsuite/g++.dg/warn/pr12242.C (revision 0) +++ gcc/testsuite/g++.dg/warn/pr12242.C (revision 0) @@ -0,0 +1,4 @@ +// { dg-do compile } +// { dg-options "-Wall -Wextra -Wconversion" } +enum E { A }; +E e = static_cast<E> (10); // { dg-warning "warning.*undefined" } Index: gcc/cp/typeck.c =================================================================== --- gcc/cp/typeck.c (revision 132310) +++ gcc/cp/typeck.c (working copy) @@ -5069,10 +5069,15 @@ build_static_cast_1 (tree type, tree exp types which are integral, floating, or enumeration types can be performed. */ if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type)) && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype))) { + if (TREE_CODE (expr) == INTEGER_CST + && TREE_CODE (type) == ENUMERAL_TYPE + && !int_fits_type_p (expr, type)) + warning (0, "this conversion is undefined"); + expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL); /* Ignore any integer overflow caused by the cast. */ expr = ignore_overflows (expr, orig); return expr; -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12242
next prev parent reply other threads:[~2008-02-15 11:10 UTC|newest] Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top [not found] <bug-12242-702@http.gcc.gnu.org/bugzilla/> 2006-01-23 1:20 ` gdr at gcc dot gnu dot org 2006-07-08 18:34 ` lopezibanez at gmail dot com 2007-01-21 17:48 ` manu at gcc dot gnu dot org 2008-02-15 11:10 ` manu at gcc dot gnu dot org [this message] 2008-02-15 11:15 ` pinskia at gcc dot gnu dot org 2008-02-15 11:33 ` manu at gcc dot gnu dot org 2008-02-15 11:35 ` rguenth at gcc dot gnu dot org 2008-02-15 11:42 ` manu at gcc dot gnu dot org 2008-08-07 5:42 ` bangerth at dealii dot org 2008-08-07 5:43 ` bangerth at dealii dot org 2008-08-07 7:52 ` manu at gcc dot gnu dot org 2008-08-07 13:14 ` bangerth at dealii dot org 2008-08-09 0:33 ` manu at gcc dot gnu dot org 2008-08-09 0:36 ` manu at gcc dot gnu dot org 2003-09-10 22:28 [Bug c++/12242] New: " gcc-bugzilla at gcc dot gnu dot org 2003-09-10 22:30 ` [Bug c++/12242] " pinskia at gcc dot gnu dot org 2003-09-10 23:10 ` bangerth at dealii dot org 2003-09-10 23:10 ` bangerth at dealii dot org 2003-09-10 23:11 ` bangerth at dealii dot org 2003-12-06 8:22 ` pinskia at gcc dot gnu dot org 2004-08-13 23:45 ` pinskia at gcc dot gnu dot org 2005-02-13 16:22 ` pinskia at gcc dot gnu dot org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20080215110929.22588.qmail@sourceware.org \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).