This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH: PR 19565
- From: Mark Mitchell <mark at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Fri, 14 Oct 2005 12:52:53 -0700
- Subject: C++ PATCH: PR 19565
- Reply-to: mark at codesourcery dot com
This PR is about missing warnings from the C++ front-end, regarding
overflows when converting integral constants to different types. I
revised the C++ handling to leverage the functions in c-common.c
designed for this purpose.
I'm not going to apply this to the 4.0 branch because a missed warning
is a very minor problem and this patch does carry some risk.
Tested on x86_64-unknown-linux-gnu, applied on the mainline.
--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com
2005-10-14 Mark Mitchell <mark@codesourcery.com>
PR c++/19565
* g++.dg/warn/Wconversion1.C: New test.
* g++.dg/ext/packed4.C: Compile with -w.
* g++.dg/opt/20050511-1.C: Likewise.
* g++.old-deja/g++.other/warn4.C: Compiler with -Wconversion.
2005-10-14 Mark Mitchell <mark@codesourcery.com>
PR c++/19565
* g++.dg/warn/Wconversion1.C: New test.
* g++.dg/ext/packed4.C: Compile with -w.
* g++.dg/opt/20050511-1.C: Likewise.
* g++.old-deja/g++.other/warn4.C: Compiler with -Wconversion.
Index: gcc/cp/call.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/call.c,v
retrieving revision 1.558
diff -c -5 -p -r1.558 call.c
*** gcc/cp/call.c 13 Oct 2005 08:38:39 -0000 1.558
--- gcc/cp/call.c 14 Oct 2005 19:43:40 -0000
*************** convert_like_real (conversion *convs, tr
*** 4214,4238 ****
warning (0, "passing %qT for argument %P to %qD",
TREE_TYPE (expr), argnum, fn);
else
warning (0, "converting to %qT from %qT", t, TREE_TYPE (expr));
}
- /* And warn about assigning a negative value to an unsigned
- variable. */
- else if (TYPE_UNSIGNED (t) && TREE_CODE (t) != BOOLEAN_TYPE)
- {
- if (TREE_CODE (expr) == INTEGER_CST && TREE_NEGATED_INT (expr))
- {
- if (fn)
- warning (0, "passing negative value %qE for argument %P to %qD",
- expr, argnum, fn);
- else
- warning (0, "converting negative value %qE to %qT", expr, t);
- }
-
- overflow_warning (expr);
- }
}
switch (convs->kind)
{
case ck_user:
--- 4214,4223 ----
*************** convert_like_real (conversion *convs, tr
*** 4428,4439 ****
c_cast_p);
default:
break;
}
! return ocp_convert (totype, expr, CONV_IMPLICIT,
! LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
}
/* Build a call to __builtin_trap. */
static tree
--- 4413,4429 ----
c_cast_p);
default:
break;
}
!
! if (issue_conversion_warnings)
! expr = convert_and_check (totype, expr);
! else
! expr = convert (totype, expr);
!
! return expr;
}
/* Build a call to __builtin_trap. */
static tree
Index: gcc/cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1434
diff -c -5 -p -r1.1434 decl.c
*** gcc/cp/decl.c 13 Oct 2005 23:59:57 -0000 1.1434
--- gcc/cp/decl.c 14 Oct 2005 19:43:41 -0000
*************** finish_enum (tree enumtype)
*** 9748,9757 ****
--- 9748,9759 ----
initializing value. */
TREE_TYPE (decl) = enumtype;
/* Update the minimum and maximum values, if appropriate. */
value = DECL_INITIAL (decl);
+ if (value == error_mark_node)
+ value = integer_zero_node;
/* Figure out what the minimum and maximum values of the
enumerators are. */
if (!minnode)
minnode = maxnode = value;
else if (tree_int_cst_lt (maxnode, value))
*************** finish_enum (tree enumtype)
*** 9850,9862 ****
--- 9852,9869 ----
/* Convert each of the enumerators to the type of the underlying
type of the enumeration. */
for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
{
+ location_t saved_location;
+
decl = TREE_VALUE (values);
+ saved_location = input_location;
+ input_location = DECL_SOURCE_LOCATION (decl);
value = perform_implicit_conversion (underlying_type,
DECL_INITIAL (decl));
+ input_location = saved_location;
/* Do not clobber shared ints. */
value = copy_node (value);
TREE_TYPE (value) = enumtype;
*************** build_enumerator (tree name, tree value,
*** 9942,9952 ****
1, 0, &lo, &hi);
value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
if (overflowed)
! error ("overflow in enumeration values at %qD", name);
}
else
value = integer_zero_node;
}
--- 9949,9962 ----
1, 0, &lo, &hi);
value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
if (overflowed)
! {
! error ("overflow in enumeration values at %qD", name);
! value = error_mark_node;
! }
}
else
value = integer_zero_node;
}
Index: gcc/cp/typeck2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck2.c,v
retrieving revision 1.192
diff -c -5 -p -r1.192 typeck2.c
*** gcc/cp/typeck2.c 1 Aug 2005 04:02:26 -0000 1.192
--- gcc/cp/typeck2.c 14 Oct 2005 19:43:42 -0000
*************** digest_init (tree type, tree init)
*** 693,706 ****
}
return init;
}
}
! /* Handle scalar types, including conversions,
! and signature pointers and references. */
! if (SCALAR_TYPE_P (type)
! || code == REFERENCE_TYPE)
return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
"initialization", NULL_TREE, 0);
/* Come here only for aggregates: records, arrays, unions, complex numbers
and vectors. */
--- 693,704 ----
}
return init;
}
}
! /* Handle scalar types (including conversions) and references. */
! if (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)
return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
"initialization", NULL_TREE, 0);
/* Come here only for aggregates: records, arrays, unions, complex numbers
and vectors. */
Index: gcc/testsuite/g++.dg/ext/packed4.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/ext/packed4.C,v
retrieving revision 1.2
diff -c -5 -p -r1.2 packed4.C
*** gcc/testsuite/g++.dg/ext/packed4.C 31 May 2004 21:24:31 -0000 1.2
--- gcc/testsuite/g++.dg/ext/packed4.C 14 Oct 2005 19:43:52 -0000
***************
*** 1,6 ****
--- 1,7 ----
// { dg-do run }
+ // { dg-options "-w" }
// Copyright (C) 2003 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 15 Jul 2003 <nathan@codesourcery.com>
// Packed fields are unsuitable for direct reference binding.
Index: gcc/testsuite/g++.dg/opt/20050511-1.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/opt/20050511-1.C,v
retrieving revision 1.1
diff -c -5 -p -r1.1 20050511-1.C
*** gcc/testsuite/g++.dg/opt/20050511-1.C 18 May 2005 23:54:36 -0000 1.1
--- gcc/testsuite/g++.dg/opt/20050511-1.C 14 Oct 2005 19:43:52 -0000
***************
*** 1,7 ****
/* { dg-do run } */
! /* { dg-options "-O3" { target powerpc*-*-* } } */
#include <stdio.h>
#include <stdlib.h>
typedef signed short SINT16 ;
typedef unsigned long UINT32 ;
--- 1,8 ----
/* { dg-do run } */
! /* { dg-options "-w" } */
! /* { dg-options "-O3 -w" { target powerpc*-*-* } } */
#include <stdio.h>
#include <stdlib.h>
typedef signed short SINT16 ;
typedef unsigned long UINT32 ;
Index: gcc/testsuite/g++.dg/warn/Wconversion1.C
===================================================================
RCS file: gcc/testsuite/g++.dg/warn/Wconversion1.C
diff -N gcc/testsuite/g++.dg/warn/Wconversion1.C
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- gcc/testsuite/g++.dg/warn/Wconversion1.C 14 Oct 2005 19:43:56 -0000
***************
*** 0 ****
--- 1,12 ----
+ // { dg-options "-Wconversion" }
+
+ char c1 = 1024; // { dg-warning "overflow" }
+ char c2 = char(1024);
+ char c3 = (char) 1024;
+ char c4 = static_cast<char>(1024);
+
+ unsigned char uc1 = -129; // { dg-warning "unsigned" }
+
+ bool b1 = -3;
+
+ int i1 = 0x80000000;
Index: gcc/testsuite/g++.old-deja/g++.other/warn4.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.other/warn4.C,v
retrieving revision 1.2
diff -c -5 -p -r1.2 warn4.C
*** gcc/testsuite/g++.old-deja/g++.other/warn4.C 1 May 2003 02:02:52 -0000 1.2
--- gcc/testsuite/g++.old-deja/g++.other/warn4.C 14 Oct 2005 19:43:57 -0000
***************
*** 1,6 ****
--- 1,7 ----
// { dg-do assemble }
+ // { dg-options "-Wconversion" }
// Copyright (C) 1999 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 21 Nov 1999 <nathan@acm.org>
// make sure we only warn on assigning a negative (signed) value