This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH: avoid warnings on array[CHAR_CONSTANT]
- From: Eddie Kohler <kohler at icir dot org>
- To: gcc-patches at gcc dot gnu dot org
- Date: Fri, 06 Jun 2003 20:07:53 -0700
- Subject: PATCH: avoid warnings on array[CHAR_CONSTANT]
Hi,
The following patch avoids the "array subscript has type `char'" warning in
C++ when the subscript is a character constant with value between 0 and
127. This supports the following common usage pattern:
int char_flags[256];
...
char_flags['%'] = 3;
Would this be generally OK?
Eddie Kohler
diff -c /home/kohler/fsrc/gcc-cvs/gcc/cp/typeck.c /home/kohler/fsrc/gcc-3.3/gcc/cp/typeck.c
*** /home/kohler/fsrc/gcc-cvs/gcc/cp/typeck.c 2003-05-19 12:19:45.000000000 -0700
--- /home/kohler/fsrc/gcc-3.3/gcc/cp/typeck.c 2003-06-05 23:57:30.000000000 -0700
***************
*** 2442,2450 ****
So warn on any machine, but optionally.
Don't warn for unsigned char since that type is safe.
Don't warn for signed char because anyone who uses that
! must have done so deliberately. */
if (warn_char_subscripts
! && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
warning ("array subscript has type `char'");
/* Apply default promotions *after* noticing character types. */
--- 2428,2440 ----
So warn on any machine, but optionally.
Don't warn for unsigned char since that type is safe.
Don't warn for signed char because anyone who uses that
! must have done so deliberately.
! Don't warn for char constants with values < 128. */
if (warn_char_subscripts
! && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node
! && (TREE_CODE (idx) != INTEGER_CST
! || compare_tree_int (idx, 0) < 0
! || compare_tree_int (idx, 128) >= 0))
warning ("array subscript has type `char'");
/* Apply default promotions *after* noticing character types. */