Commit ff6b6641 by Gabriel Dos Reis Committed by Gabriel Dos Reis

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
parent 7fdc0307
...@@ -6284,4 +6284,20 @@ check_missing_format_attribute (tree ltype, tree rtype) ...@@ -6284,4 +6284,20 @@ check_missing_format_attribute (tree ltype, tree rtype)
return false; 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" #include "gt-c-common.h"
...@@ -836,6 +836,8 @@ extern int complete_array_type (tree *, tree, bool); ...@@ -836,6 +836,8 @@ extern int complete_array_type (tree *, tree, bool);
extern tree builtin_type_for_size (int, bool); extern tree builtin_type_for_size (int, bool);
extern void warn_array_subscript_with_type_char (tree);
/* In c-gimplify.c */ /* In c-gimplify.c */
extern void c_genericize (tree); extern void c_genericize (tree);
extern int c_gimplify_expr (tree *, tree *, tree *); extern int c_gimplify_expr (tree *, tree *, tree *);
......
...@@ -1859,16 +1859,10 @@ build_array_ref (tree array, tree index) ...@@ -1859,16 +1859,10 @@ build_array_ref (tree array, tree index)
return error_mark_node; return error_mark_node;
} }
/* Subscripting with type char is likely to lose on a machine where /* ??? Existing practice has been to warn only when the char
chars are signed. So warn on any machine, but optionally. Don't index is syntactically the index, not for char[array]. */
warn for unsigned char since that type is safe. Don't warn for if (!swapped)
signed char because anyone who uses that must have done so warn_array_subscript_with_type_char (index);
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%>");
/* Apply default promotions *after* noticing character types. */ /* Apply default promotions *after* noticing character types. */
index = default_conversion (index); index = default_conversion (index);
......
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> 2005-11-21 Gabriel Dos Reis <gdr@integrable-solutions.net>
PR c++/22238 PR c++/22238
......
...@@ -2253,15 +2253,7 @@ build_array_ref (tree array, tree idx) ...@@ -2253,15 +2253,7 @@ build_array_ref (tree array, tree idx)
{ {
tree rval, type; tree rval, type;
/* Subscripting with type char is likely to lose warn_array_subscript_with_type_char (idx);
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%>");
if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx))) if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
{ {
......
/* 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'];
}
/* 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'];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment