re PR c++/21667 (misleading warning about array subscription)

2005-11-23  Gabriel Dos Reis  <gdr@integrable-solutions.net>

        PR c++/21667
        * c-typeck.c (build_array_ref): Avoid code duplicate.  Use
        common
        C/C++ diagnostic function warn_array_subscript_with_type_char.
        * c-common.h (warn_array_subscript_with_type_char): Declare.
        * c-common.c (warn_array_subscript_with_type_char): Define.

cp/
2005-11-23  Gabriel Dos Reis  <gdr@integrable-solutions.net>

        PR c++/21667
        * typeck.c (build_array_ref): Avoid code duplicate.  Use
        common
        C/C++ diagnostic function warn_array_subscript_with_type_char.

testsuite/
2005-11-23  Gabriel Dos Reis  <gdr@integrable-solutions.net>

        PR c++/21667
        * gcc.dg/Wchar-subscripts.c: New.
        * g++.dg/warn/Wchar-subscripts.C: Likewise.

From-SVN: r107448
This commit is contained in:
Gabriel Dos Reis 2005-11-24 02:02:26 +00:00 committed by Gabriel Dos Reis
parent 7fdc03073c
commit ff6b664150
7 changed files with 53 additions and 19 deletions

View File

@ -6284,4 +6284,20 @@ check_missing_format_attribute (tree ltype, tree rtype)
return false;
}
/* Subscripting with type char is likely to lose on a machine where
chars are signed. 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. Furthermore, we reduce the false positive load by
warning only for non-constant value of type char. */
void
warn_array_subscript_with_type_char (tree index)
{
if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
&& TREE_CODE (index) != INTEGER_CST)
warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
}
#include "gt-c-common.h"

View File

@ -836,6 +836,8 @@ extern int complete_array_type (tree *, tree, bool);
extern tree builtin_type_for_size (int, bool);
extern void warn_array_subscript_with_type_char (tree);
/* In c-gimplify.c */
extern void c_genericize (tree);
extern int c_gimplify_expr (tree *, tree *, tree *);

View File

@ -1859,16 +1859,10 @@ build_array_ref (tree array, tree index)
return error_mark_node;
}
/* Subscripting with type char is likely to lose on a machine where
chars are signed. 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. ??? Existing practice has also been to warn only
when the char index is syntactically the index, not for
char[array]. */
if (!swapped
&& TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
/* ??? Existing practice has been to warn only when the char
index is syntactically the index, not for char[array]. */
if (!swapped)
warn_array_subscript_with_type_char (index);
/* Apply default promotions *after* noticing character types. */
index = default_conversion (index);

View File

@ -1,3 +1,9 @@
2005-11-23 Gabriel Dos Reis <gdr@integrable-solutions.net>
PR c++/21667
* typeck.c (build_array_ref): Avoid code duplicate. Use common
C/C++ diagnostic function warn_array_subscript_with_type_char.
2005-11-21 Gabriel Dos Reis <gdr@integrable-solutions.net>
PR c++/22238

View File

@ -2253,15 +2253,7 @@ build_array_ref (tree array, tree idx)
{
tree rval, type;
/* Subscripting with type char is likely to lose
on a machine where chars are signed.
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 (0, "array subscript has type %<char%>");
warn_array_subscript_with_type_char (idx);
if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
{

View File

@ -0,0 +1,12 @@
/* Copyright (C) 2005 Free Software Foundation.
by Gabriel Dos Reis <gdr@integrable-solutions.net> */
// { dg-do compile }
// { dg-options "-Wchar-subscripts" }
int main()
{
int ary[256] = { 0 };
return ary['a'];
}

View File

@ -0,0 +1,12 @@
/* Copyright (C) 2005 Free Software Foundation.
by Gabriel Dos Reis <gdr@integrable-solutions.net> */
/* { dg-do compile } */
/* { dg-options "-Wchar-subscripts" } */
int main(void)
{
int ary[256] = { 0 };
return ary['a'];
}