]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/21667 (misleading warning about array subscription)
authorGabriel Dos Reis <gdr@integrable-solutions.net>
Thu, 24 Nov 2005 02:02:26 +0000 (02:02 +0000)
committerGabriel Dos Reis <gdr@gcc.gnu.org>
Thu, 24 Nov 2005 02:02:26 +0000 (02:02 +0000)
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

gcc/c-common.c
gcc/c-common.h
gcc/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/g++.dg/warn/Wchar-subscripts.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wchar-subscripts.c [new file with mode: 0644]

index 7b27ba06e4e9d7795765f267fcfd9c33c48c400a..b843df2593537c3c2d56ce605e8fd339039dde35 100644 (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"
index cf75ed9e82a1294671d14b37a1e020c168b2bf60..96905af107f8f32e430e99d80fa2fef9dcc8a907 100644 (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 *);
index 80259cc811d9fdf38cc87c43b522762136a806bc..2150238ec6e1abc5c445b5c28d520eb15a2fafa4 100644 (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);
index 17094dd3f69a60c4cd2863d73f5116ca9c91a151..54362869f2f33798110e6652e93d667035e3c28c 100644 (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
index a86ee6a516e4e8938397f9017669c55db7467c45..b3c155a13c0e23b82be6f1fb331f528128c7d6bf 100644 (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)))
        {
diff --git a/gcc/testsuite/g++.dg/warn/Wchar-subscripts.C b/gcc/testsuite/g++.dg/warn/Wchar-subscripts.C
new file mode 100644 (file)
index 0000000..bc38585
--- /dev/null
@@ -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 (file)
index 0000000..acc6d23
--- /dev/null
@@ -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'];
+}