From 69609004bdc6df041c8a981b4d96dc09d23b9148 Mon Sep 17 00:00:00 2001 From: jakub Date: Fri, 23 Nov 2007 13:39:44 +0000 Subject: [PATCH] PR c++/34198 * c-common.c (conversion_warning): For INTEGER_TYPE to INTEGER_TYPE conversions call get_narrower on expr to avoid spurious warnings from binop shortening or when the implicit conversion can't change the value. * gcc.dg/Wconversion-5.c: New test. * g++.dg/Wconversion3.C: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@130377 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 8 ++++++++ gcc/c-common.c | 35 +++++++++++++++++++++++++++----- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/g++.dg/warn/Wconversion3.C | 35 ++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wconversion-5.c | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wconversion3.C create mode 100644 gcc/testsuite/gcc.dg/Wconversion-5.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index e6f36406f0d..722ec42882c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2007-11-23 Jakub Jelinek + + PR c++/34198 + * c-common.c (conversion_warning): For INTEGER_TYPE to + INTEGER_TYPE conversions call get_narrower on expr to avoid + spurious warnings from binop shortening or when the implicit + conversion can't change the value. + 2007-11-22 Joseph Myers PR c/14050 diff --git a/gcc/c-common.c b/gcc/c-common.c index edc9b2c17fb..6872e3acef9 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -1280,6 +1280,14 @@ conversion_warning (tree type, tree expr) else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE && TREE_CODE (type) == INTEGER_TYPE) { + /* Don't warn about unsigned char y = 0xff, x = (int) y; */ + int uns; + tree orig_expr = expr; + expr = get_narrower (expr, &uns); + + if (expr == orig_expr) + uns = TYPE_UNSIGNED (TREE_TYPE (expr)); + /* Warn for integer types converted to smaller integer types. */ if (formal_prec < TYPE_PRECISION (TREE_TYPE (expr))) give_warning = true; @@ -1287,14 +1295,31 @@ conversion_warning (tree type, tree expr) /* When they are the same width but different signedness, then the value may change. */ else if ((formal_prec == TYPE_PRECISION (TREE_TYPE (expr)) - && TYPE_UNSIGNED (TREE_TYPE (expr)) != TYPE_UNSIGNED (type)) + && uns != TYPE_UNSIGNED (type)) /* Even when converted to a bigger type, if the type is unsigned but expr is signed, then negative values will be changed. */ - || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr)))) - warning (OPT_Wsign_conversion, - "conversion to %qT from %qT may change the sign of the result", - type, TREE_TYPE (expr)); + || (TYPE_UNSIGNED (type) && !uns)) + { + if (uns != TYPE_UNSIGNED (TREE_TYPE (expr))) + { + /* For signed char s1, s2 = (int) (unsigned char) s1; + get_narrower returns s1, but uns = 1. Find the + narrowest type with uns == TYPE_UNSIGNED (type). */ + tree unsexpr = orig_expr; + + while (TREE_CODE (unsexpr) == NOP_EXPR + && unsexpr != expr + && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (unsexpr, + 0))) + == uns) + unsexpr = TREE_OPERAND (unsexpr, 0); + expr = unsexpr; + } + warning (OPT_Wsign_conversion, + "conversion to %qT from %qT may change the sign of the result", + type, TREE_TYPE (expr)); + } } /* Warn for integer types converted to real types if and only if diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d9aee04b513..7f0c52bcc22 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2007-11-23 Jakub Jelinek + + PR c++/34198 + * gcc.dg/Wconversion-5.c: New test. + * g++.dg/Wconversion3.C: New test. + 2007-11-23 Richard Guenther * gcc.dg/tree-ssa/alias-17.c: New testcase. diff --git a/gcc/testsuite/g++.dg/warn/Wconversion3.C b/gcc/testsuite/g++.dg/warn/Wconversion3.C new file mode 100644 index 00000000000..24202b7d98e --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wconversion3.C @@ -0,0 +1,35 @@ +// PR c++/34198 +// { dg-do compile } +// { dg-options "-O2 -Wconversion -Wsign-conversion" } + +signed char sc; +unsigned char uc; +short int ss; +unsigned short int us; +int si; +unsigned int ui; + +void test1 (void) +{ + int a = uc & 0xff; + int b = sc & 0x7f; + int c = 0xff & uc; + int d = 0x7f & sc; + int e = uc & sc; + unsigned char f = (int) uc; + signed char g = (int) sc; + unsigned char h = (unsigned int) (short int) uc; + signed char i = (int) (unsigned short int) sc; // { dg-warning "may alter its value" } + unsigned char j = (unsigned int) (short int) us; // { dg-warning "may alter its value" } + signed char k = (int) (unsigned short int) ss; // { dg-warning "may alter its value" } +} + +void test2 (void) +{ + signed char a = (unsigned char) sc; // { dg-warning "may change the sign" } + unsigned char b = (signed char) uc; // { dg-warning "may change the sign" } + signed char c = (int) (unsigned char) sc; // { dg-warning "may change the sign" } + unsigned char d = (int) (signed char) uc; // { dg-warning "may change the sign" } + int e = (unsigned int) si; // { dg-warning "may change the sign" } + unsigned int f = (int) ui; // { dg-warning "may change the sign" } +} diff --git a/gcc/testsuite/gcc.dg/Wconversion-5.c b/gcc/testsuite/gcc.dg/Wconversion-5.c new file mode 100644 index 00000000000..a09caae4def --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wconversion-5.c @@ -0,0 +1,35 @@ +/* PR c++/34198 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wconversion" } */ + +signed char sc; +unsigned char uc; +short int ss; +unsigned short int us; +int si; +unsigned int ui; + +void test1 (void) +{ + int a = uc & 0xff; + int b = sc & 0x7f; + int c = 0xff & uc; + int d = 0x7f & sc; + int e = uc & sc; + unsigned char f = (int) uc; + signed char g = (int) sc; + unsigned char h = (unsigned int) (short int) uc; + signed char i = (int) (unsigned short int) sc; /* { dg-warning "may alter its value" } */ + unsigned char j = (unsigned int) (short int) us; /* { dg-warning "may alter its value" } */ + signed char k = (int) (unsigned short int) ss; /* { dg-warning "may alter its value" } */ +} + +void test2 (void) +{ + signed char a = (unsigned char) sc; /* { dg-warning "may change the sign" } */ + unsigned char b = (signed char) uc; /* { dg-warning "may change the sign" } */ + signed char c = (int) (unsigned char) sc; /* { dg-warning "may change the sign" } */ + unsigned char d = (int) (signed char) uc; /* { dg-warning "may change the sign" } */ + int e = (unsigned int) si; /* { dg-warning "may change the sign" } */ + unsigned int f = (int) ui; /* { dg-warning "may change the sign" } */ +} -- 2.11.0