From: Gabriel Dos Reis Date: Thu, 24 Nov 2005 02:02:26 +0000 (+0000) Subject: re PR c++/21667 (misleading warning about array subscription) X-Git-Tag: releases/gcc-4.2.0~5782 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff6b6641506a9aeab5dbb01a2ebd214ff3a942ba;p=thirdparty%2Fgcc.git re PR c++/21667 (misleading warning about array subscription) 2005-11-23 Gabriel Dos Reis 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 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 PR c++/21667 * gcc.dg/Wchar-subscripts.c: New. * g++.dg/warn/Wchar-subscripts.C: Likewise. From-SVN: r107448 --- diff --git a/gcc/c-common.c b/gcc/c-common.c index 7b27ba06e4e9..b843df259353 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -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 %"); +} + + #include "gt-c-common.h" diff --git a/gcc/c-common.h b/gcc/c-common.h index cf75ed9e82a1..96905af107f8 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -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 *); diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 80259cc811d9..2150238ec6e1 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -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 %"); + /* ??? 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); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 17094dd3f69a..54362869f2f3 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2005-11-23 Gabriel Dos Reis + + 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 PR c++/22238 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index a86ee6a516e4..b3c155a13c0e 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -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 %"); + warn_array_subscript_with_type_char (idx); if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx))) { diff --git a/gcc/testsuite/g++.dg/warn/Wchar-subscripts.C b/gcc/testsuite/g++.dg/warn/Wchar-subscripts.C new file mode 100644 index 000000000000..bc38585d72e4 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wchar-subscripts.C @@ -0,0 +1,12 @@ +/* Copyright (C) 2005 Free Software Foundation. + + by Gabriel Dos Reis */ + +// { dg-do compile } +// { dg-options "-Wchar-subscripts" } + +int main() +{ + int ary[256] = { 0 }; + return ary['a']; +} diff --git a/gcc/testsuite/gcc.dg/Wchar-subscripts.c b/gcc/testsuite/gcc.dg/Wchar-subscripts.c new file mode 100644 index 000000000000..acc6d23578e1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wchar-subscripts.c @@ -0,0 +1,12 @@ +/* Copyright (C) 2005 Free Software Foundation. + + by Gabriel Dos Reis */ + +/* { dg-do compile } */ +/* { dg-options "-Wchar-subscripts" } */ + +int main(void) +{ + int ary[256] = { 0 }; + return ary['a']; +}