This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/51294] spurious warning from -Wconversion in C and C++ in conditional expressions
- From: "manu at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 12 May 2012 18:38:33 +0000
- Subject: [Bug c/51294] spurious warning from -Wconversion in C and C++ in conditional expressions
- Auto-submitted: auto-generated
- References: <bug-51294-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51294
--- Comment #10 from Manuel LÃpez-IbÃÃez <manu at gcc dot gnu.org> 2012-05-12 18:38:33 UTC ---
Latest patch by Paolo, who run out of time:
http://gcc.gnu.org/ml/gcc-patches/2012-05/msg00861.html
BTW, I think the patch is not wrong, and if fixes some cases, then it is a step
forward.
I think a more complete fix would be something like:
Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c (revision 187427)
+++ gcc/c-family/c-common.c (working copy)
@@ -2262,10 +2262,15 @@ conversion_warning (tree type, tree expr
location_t loc = EXPR_LOC_OR_HERE (expr);
if (!warn_conversion && !warn_sign_conversion)
return;
+ STRIP_USELESS_TYPE_CONVERSION (expr);
+ /* Don't warn about unsigned char y = 0xff, x = (int) y; */
+ expr = get_unwidened (expr, 0);
+ expr_type = TREE_TYPE (expr);
+
switch (TREE_CODE (expr))
{
case EQ_EXPR:
case NE_EXPR:
case LE_EXPR:
@@ -2294,26 +2299,18 @@ conversion_warning (tree type, tree expr
type, expr_type);
return;
case COND_EXPR:
{
- /* In case of COND_EXPR, if both operands are constants or
- COND_EXPR, then we do not care about the type of COND_EXPR,
- only about the conversion of each operand. */
- tree op1 = TREE_OPERAND (expr, 1);
- tree op2 = TREE_OPERAND (expr, 2);
-
- if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
- || TREE_CODE (op1) == COND_EXPR)
- && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
- || TREE_CODE (op2) == COND_EXPR))
- {
- conversion_warning (type, op1);
- conversion_warning (type, op2);
- return;
- }
- /* Fall through. */
+ /* In case of COND_EXPR, we do not care about the type of
+ COND_EXPR, only about the conversion of each operand. */
+ tree op1 = TREE_OPERAND (expr, 1);
+ tree op2 = TREE_OPERAND (expr, 2);
+
+ conversion_warning (type, op1);
+ conversion_warning (type, op2);
+ return;
}
default: /* 'expr' is not a constant. */
if (unsafe_conversion_p (type, expr, true))
warning_at (loc, OPT_Wconversion,
Totally untested, and it lacks testcases, ChangeLog, etc. Neither Paolo nor I
have time to pursue this, so someone new will have to step forward.