diff options
author | Gabriel Dos Reis <gdr@integrable-solutions.net> | 2005-11-24 02:02:26 +0000 |
---|---|---|
committer | Gabriel Dos Reis <gdr@gcc.gnu.org> | 2005-11-24 02:02:26 +0000 |
commit | ff6b6641506a9aeab5dbb01a2ebd214ff3a942ba (patch) | |
tree | e7e85e47eb726ead6275d3e7b3b82823305a9fba /gcc | |
parent | 7fdc03073c4c0f664a6d5f1a5951b07c6e37fa44 (diff) | |
download | gcc-ff6b6641506a9aeab5dbb01a2ebd214ff3a942ba.zip gcc-ff6b6641506a9aeab5dbb01a2ebd214ff3a942ba.tar.gz gcc-ff6b6641506a9aeab5dbb01a2ebd214ff3a942ba.tar.bz2 |
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
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/c-common.c | 16 | ||||
-rw-r--r-- | gcc/c-common.h | 2 | ||||
-rw-r--r-- | gcc/c-typeck.c | 14 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/typeck.c | 10 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wchar-subscripts.C | 12 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wchar-subscripts.c | 12 |
7 files changed, 53 insertions, 19 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c index 7b27ba0..b843df2 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 %<char%>"); +} + + #include "gt-c-common.h" diff --git a/gcc/c-common.h b/gcc/c-common.h index cf75ed9..96905af 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 80259cc8..2150238 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 %<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); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 17094dd..5436286 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -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 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index a86ee6a..b3c155a 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 %<char%>"); + 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 0000000..bc38585 --- /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 <gdr@integrable-solutions.net> */ + +// { 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 0000000..acc6d23 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wchar-subscripts.c @@ -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']; +} |