We can improve various -Wshadow warnings by using DECL_SOURCE_LOCATION rather than whatever is in input_location.
Bootstrapped/regtested on x86_64-linux, ok for trunk? 2019-09-23 Marek Polacek <pola...@redhat.com> PR c++/91868 - improve -Wshadow location. * name-lookup.c (check_local_shadow): Use DECL_SOURCE_LOCATION instead of input_location. * g++.dg/warn/Wshadow-16.C: New test. diff --git gcc/cp/name-lookup.c gcc/cp/name-lookup.c index 8bbb92ddc9f..74f1072fa8c 100644 --- gcc/cp/name-lookup.c +++ gcc/cp/name-lookup.c @@ -2771,7 +2771,7 @@ check_local_shadow (tree decl) msg = "declaration of %qD shadows a previous local"; auto_diagnostic_group d; - if (warning_at (input_location, warning_code, msg, decl)) + if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl)) inform_shadowed (old); return; } @@ -2798,7 +2798,7 @@ check_local_shadow (tree decl) || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))) { auto_diagnostic_group d; - if (warning_at (input_location, OPT_Wshadow, + if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow, "declaration of %qD shadows a member of %qT", decl, current_nonlambda_class_type ()) && DECL_P (member)) @@ -2818,7 +2818,7 @@ check_local_shadow (tree decl) /* XXX shadow warnings in outer-more namespaces */ { auto_diagnostic_group d; - if (warning_at (input_location, OPT_Wshadow, + if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow, "declaration of %qD shadows a global declaration", decl)) inform_shadowed (old); diff --git gcc/testsuite/g++.dg/warn/Wshadow-16.C gcc/testsuite/g++.dg/warn/Wshadow-16.C new file mode 100644 index 00000000000..1ba54ec107d --- /dev/null +++ gcc/testsuite/g++.dg/warn/Wshadow-16.C @@ -0,0 +1,24 @@ +// PR c++/91868 - improve -Wshadow location. +// { dg-options "-Wshadow" } + +int global; // { dg-message "shadowed declaration" } + +struct S +{ + static int bar; // { dg-message "shadowed declaration" } + S (int i) { int bar // { dg-warning "19:declaration of .bar. shadows a member" } + (1); + int global // { dg-warning "9:declaration of .global. shadows a global declaration" } + (42); + } +}; + +void +foo () +{ + int xx; // { dg-message "shadowed declaration" } + { + S xx // { dg-warning "7:declaration of .xx. shadows a previous local" } + (42); + } +}