warn_array_subscript_with_type_char wasn't getting a location, so the caret location was off. Thus fixed.
Bootstrapped/regtested on x86_64-linux, applying to trunk. 2015-01-05 Marek Polacek <pola...@redhat.com> PR c/64423 c-family/ * c-common.c (warn_array_subscript_with_type_char): Add location_t parameter. Use it. * c-common.h (warn_array_subscript_with_type_char): Update declaration. c/ * c-typeck.c (build_array_ref): Pass loc down to warn_array_subscript_with_type_char. cp/ * typeck.c (cp_build_array_ref): Pass loc down to warn_array_subscript_with_type_char. testsuite/ * gcc.dg/pr64423.c: New test. diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index 1066c6b..af7a07e 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -11238,11 +11238,12 @@ check_missing_format_attribute (tree ltype, tree rtype) warning only for non-constant value of type char. */ void -warn_array_subscript_with_type_char (tree index) +warn_array_subscript_with_type_char (location_t loc, 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%>"); + warning_at (loc, OPT_Wchar_subscripts, + "array subscript has type %<char%>"); } /* Implement -Wparentheses for the unexpected C precedence rules, to diff --git gcc/c-family/c-common.h gcc/c-family/c-common.h index c7eebcf..d1f09a6 100644 --- gcc/c-family/c-common.h +++ gcc/c-family/c-common.h @@ -1014,7 +1014,7 @@ extern tree builtin_type_for_size (int, bool); extern void c_common_mark_addressable_vec (tree); -extern void warn_array_subscript_with_type_char (tree); +extern void warn_array_subscript_with_type_char (location_t, tree); extern void warn_about_parentheses (location_t, enum tree_code, enum tree_code, tree, diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c index abd452a..37beb64 100644 --- gcc/c/c-typeck.c +++ gcc/c/c-typeck.c @@ -2501,7 +2501,7 @@ build_array_ref (location_t loc, tree array, tree index) /* ??? 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); + warn_array_subscript_with_type_char (loc, index); /* Apply default promotions *after* noticing character types. */ index = default_conversion (index); diff --git gcc/cp/typeck.c gcc/cp/typeck.c index 9368b49..fc85ec3 100644 --- gcc/cp/typeck.c +++ gcc/cp/typeck.c @@ -3081,7 +3081,7 @@ cp_build_array_ref (location_t loc, tree array, tree idx, { tree rval, type; - warn_array_subscript_with_type_char (idx); + warn_array_subscript_with_type_char (loc, idx); if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx))) { @@ -3191,7 +3191,7 @@ cp_build_array_ref (location_t loc, tree array, tree idx, return error_mark_node; } - warn_array_subscript_with_type_char (idx); + warn_array_subscript_with_type_char (loc, idx); ret = cp_build_indirect_ref (cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, diff --git gcc/testsuite/gcc.dg/pr64423.c gcc/testsuite/gcc.dg/pr64423.c index e69de29..c228acb 100644 --- gcc/testsuite/gcc.dg/pr64423.c +++ gcc/testsuite/gcc.dg/pr64423.c @@ -0,0 +1,13 @@ +/* PR c/64423 */ +/* { dg-do compile } */ +/* { dg-options "-Wchar-subscripts" } */ + +int a[100]; + +int +f (char c) +{ + return a[c] /* { dg-warning "11:array subscript has type .char." } */ + + a[c] /* { dg-warning "14:array subscript has type .char." } */ + + a[c]; /* { dg-warning "16:array subscript has type .char." } */ +} Marek