From 993acb366ebf8a43f66669950b385710eba931cb Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Tue, 14 Dec 2010 20:31:22 +0000 Subject: [PATCH] re PR c++/45330 (Suggest likely nested-name-specifiers for undeclared identifiers.) gcc/cp/ PR c++/45330 * cp-tree.h (suggest_alternatives_for): Add location_t parameter. * name-lookup.c (suggest_alternatives_for): Likewise. Adjust. * lex.c (unqualified_name_lookup_error): Adjust call to it. * semantics.c (qualified_name_lookup_error): Move to... * error.c (qualified_name_lookup_error): ...here. Call. suggest_alternatives_for. gcc/testsuite/ PR c++/45330 * g++.dg/lookup/suggestions1.C: New test. From-SVN: r167814 --- gcc/cp/ChangeLog | 10 ++++++ gcc/cp/cp-tree.h | 6 ++-- gcc/cp/error.c | 36 ++++++++++++++++++++++ gcc/cp/lex.c | 2 +- gcc/cp/name-lookup.c | 9 ++---- gcc/cp/semantics.c | 31 ------------------- gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/g++.dg/lookup/suggestions1.C | 8 +++++ 8 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 gcc/testsuite/g++.dg/lookup/suggestions1.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3ca26973c87..d9d8f04b5b3 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,13 @@ +2010-12-14 Nathan Froyd + + PR c++/45330 + * cp-tree.h (suggest_alternatives_for): Add location_t parameter. + * name-lookup.c (suggest_alternatives_for): Likewise. Adjust. + * lex.c (unqualified_name_lookup_error): Adjust call to it. + * semantics.c (qualified_name_lookup_error): Move to... + * error.c (qualified_name_lookup_error): ...here. Call. + suggest_alternatives_for. + 2010-12-13 Jason Merrill PR c++/46873 diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 79f9f24eee0..7b18973c493 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -4898,6 +4898,8 @@ extern void maybe_warn_variadic_templates (void); extern void maybe_warn_cpp0x (cpp0x_warn_str str); extern bool pedwarn_cxx98 (location_t, int, const char *, ...) ATTRIBUTE_GCC_DIAG(3,4); extern location_t location_of (tree); +extern void qualified_name_lookup_error (tree, tree, tree, + location_t); /* in except.c */ extern void init_exception_processing (void); @@ -5286,8 +5288,6 @@ extern void finish_template_decl (tree); extern tree finish_template_type (tree, tree, int); extern tree finish_base_specifier (tree, tree, bool); extern void finish_member_declaration (tree); -extern void qualified_name_lookup_error (tree, tree, tree, - location_t); extern tree finish_id_expression (tree, tree, tree, cp_id_kind *, bool, bool, bool *, @@ -5631,7 +5631,7 @@ extern void cxx_omp_finish_clause (tree); extern bool cxx_omp_privatize_by_reference (const_tree); /* in name-lookup.c */ -extern void suggest_alternatives_for (tree); +extern void suggest_alternatives_for (location_t, tree); /* -- end of C++ */ diff --git a/gcc/cp/error.c b/gcc/cp/error.c index e1bac244418..3e91115ba9d 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -3167,3 +3167,39 @@ pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...) va_end (ap); return report_diagnostic (&diagnostic); } + +/* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what + we found when we tried to do the lookup. LOCATION is the location of + the NAME identifier. */ + +void +qualified_name_lookup_error (tree scope, tree name, + tree decl, location_t location) +{ + if (scope == error_mark_node) + ; /* We already complained. */ + else if (TYPE_P (scope)) + { + if (!COMPLETE_TYPE_P (scope)) + error_at (location, "incomplete type %qT used in nested name specifier", + scope); + else if (TREE_CODE (decl) == TREE_LIST) + { + error_at (location, "reference to %<%T::%D%> is ambiguous", + scope, name); + print_candidates (decl); + } + else + error_at (location, "%qD is not a member of %qT", name, scope); + } + else if (scope != global_namespace) + { + error_at (location, "%qD is not a member of %qD", name, scope); + suggest_alternatives_for (location, name); + } + else + { + error_at (location, "%<::%D%> has not been declared", name); + suggest_alternatives_for (location, name); + } +} diff --git a/gcc/cp/lex.c b/gcc/cp/lex.c index 5a2ae41ff18..b04c89f46b6 100644 --- a/gcc/cp/lex.c +++ b/gcc/cp/lex.c @@ -452,7 +452,7 @@ unqualified_name_lookup_error (tree name) if (!objc_diagnose_private_ivar (name)) { error ("%qD was not declared in this scope", name); - suggest_alternatives_for (name); + suggest_alternatives_for (location_of (name), name); } /* Prevent repeated error messages by creating a VAR_DECL with this NAME in the innermost block scope. */ diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 4cf13809031..e2e54507c46 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -3923,7 +3923,7 @@ remove_hidden_names (tree fns) possible candidates. */ void -suggest_alternatives_for (tree name) +suggest_alternatives_for (location_t location, tree name) { VEC(tree,heap) *candidates = NULL; VEC(tree,heap) *namespaces_to_search = NULL; @@ -3931,7 +3931,6 @@ suggest_alternatives_for (tree name) int n_searched = 0; tree t; unsigned ix; - location_t name_location; VEC_safe_push (tree, heap, namespaces_to_search, global_namespace); @@ -3955,15 +3954,13 @@ suggest_alternatives_for (tree name) VEC_safe_push (tree, heap, namespaces_to_search, t); } - name_location = location_of (name); - /* If we stopped before we could examine all namespaces, inform the user. Do this even if we don't have any candidates, since there might be more candidates further down that we weren't able to find. */ if (n_searched >= max_to_search && !VEC_empty (tree, namespaces_to_search)) - inform (name_location, + inform (location, "maximum limit of %d namespaces searched for %qE", max_to_search, name); @@ -3973,7 +3970,7 @@ suggest_alternatives_for (tree name) if (VEC_empty (tree, candidates)) return; - inform_n (name_location, VEC_length (tree, candidates), + inform_n (location, VEC_length (tree, candidates), "suggested alternative:", "suggested alternatives:"); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 27e2982a98c..25b99320fa6 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2646,37 +2646,6 @@ finish_base_specifier (tree base, tree access, bool virtual_p) return result; } -/* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is - what we found when we tried to do the lookup. - LOCATION is the location of the NAME identifier; - The location is used in the error message*/ - -void -qualified_name_lookup_error (tree scope, tree name, - tree decl, location_t location) -{ - if (scope == error_mark_node) - ; /* We already complained. */ - else if (TYPE_P (scope)) - { - if (!COMPLETE_TYPE_P (scope)) - error_at (location, "incomplete type %qT used in nested name specifier", - scope); - else if (TREE_CODE (decl) == TREE_LIST) - { - error_at (location, "reference to %<%T::%D%> is ambiguous", - scope, name); - print_candidates (decl); - } - else - error_at (location, "%qD is not a member of %qT", name, scope); - } - else if (scope != global_namespace) - error_at (location, "%qD is not a member of %qD", name, scope); - else - error_at (location, "%<::%D%> has not been declared", name); -} - /* If FNS is a member function, a set of member functions, or a template-id referring to one or more member functions, return a BASELINK for FNS, incorporating the current access context. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5dfc9c2de7c..99c8103ddd1 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2010-12-14 Nathan Froyd + + PR c++/45330 + * g++.dg/lookup/suggestions1.C: New test. + 2010-12-14 Tobias Burnus PR fortran/46937 diff --git a/gcc/testsuite/g++.dg/lookup/suggestions1.C b/gcc/testsuite/g++.dg/lookup/suggestions1.C new file mode 100644 index 00000000000..1185d58416a --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/suggestions1.C @@ -0,0 +1,8 @@ +// { dg-do compile } + +namespace N { namespace M { int foo; } } // { dg-message "N::M::foo" } +int f (void) { return N::foo; } // { dg-error "not a member" } +// { dg-message "suggested alternative" "missing namespace" { target *-*-* } 4 } + +int g (void) { return ::foo; } // { dg-error "not been declared" } +// { dg-message "suggested alternative" "omitted namespace" { target *-*-* } 7 } -- 2.30.2