diff options
author | Nathan Froyd <froydnj@codesourcery.com> | 2010-12-07 13:46:29 +0000 |
---|---|---|
committer | Nathan Froyd <froydnj@gcc.gnu.org> | 2010-12-07 13:46:29 +0000 |
commit | 501c95ff0546ff1d2632017c6f66fc0cd83566f7 (patch) | |
tree | fe63f288d7fac2d1492b5370bcf38c201cc58251 /gcc/cp/name-lookup.c | |
parent | 85a47bed9340d25f861b9e2a186a4d443660cc26 (diff) | |
download | gcc-501c95ff0546ff1d2632017c6f66fc0cd83566f7.zip gcc-501c95ff0546ff1d2632017c6f66fc0cd83566f7.tar.gz gcc-501c95ff0546ff1d2632017c6f66fc0cd83566f7.tar.bz2 |
re PR c++/45330 (Suggest likely nested-name-specifiers for undeclared identifiers.)
gcc/
PR c++/45330
* params.def (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP): New parameter.
* doc/invoke.texi (cxx-max-namespaces-for-diagnostic-help): Document.
gcc/cp/
PR c++/45330
* cp-tree.h (suggest_alternatives_for, location_of): Declare.
* error.c (dump_expr): Handle TYPE_DECL.
(location_of): Unstaticize.
* name-lookup.c (suggest_alternatives_for): New function.
* lex.c (unqualified_name_lookup_error): Call it.
gcc/testsuite/
PR c++/45330
* g++.dg/pr45330.C: New test.
* g++.dg/ext/builtin3.C: Adjust.
* g++.dg/lookup/error1.C: Adjust.
* g++.dg/lookup/koenig5.C: Adjust.
* g++.dg/overload/koenig1.C: Adjust.
* g++.dg/parse/decl-specifier-1.C: Adjust.
* g++.dg/template/static10.C: Adjust.
* g++.old-deja/g++.mike/ns5.C: Adjust.
* g++.old-deja/g++.mike/ns7.C: Adjust.
* g++.old-deja/g++.ns/koenig5.C: Adjust.
* g++.old-deja/g++.ns/koenig9.C: Adjust.
* g++.old-deja/g++.other/lineno5.C: Adjust.
From-SVN: r167536
Diffstat (limited to 'gcc/cp/name-lookup.c')
-rw-r--r-- | gcc/cp/name-lookup.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 3d19c08..4cf1380 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -29,8 +29,10 @@ along with GCC; see the file COPYING3. If not see #include "name-lookup.h" #include "timevar.h" #include "diagnostic-core.h" +#include "intl.h" #include "debug.h" #include "c-family/c-pragma.h" +#include "params.h" /* The bindings for a particular name in a particular scope. */ @@ -3916,6 +3918,71 @@ remove_hidden_names (tree fns) return fns; } +/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name + lookup failed. Search through all available namespaces and print out + possible candidates. */ + +void +suggest_alternatives_for (tree name) +{ + VEC(tree,heap) *candidates = NULL; + VEC(tree,heap) *namespaces_to_search = NULL; + int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); + int n_searched = 0; + tree t; + unsigned ix; + location_t name_location; + + VEC_safe_push (tree, heap, namespaces_to_search, global_namespace); + + while (!VEC_empty (tree, namespaces_to_search) + && n_searched < max_to_search) + { + tree scope = VEC_pop (tree, namespaces_to_search); + struct scope_binding binding = EMPTY_SCOPE_BINDING; + struct cp_binding_level *level = NAMESPACE_LEVEL (scope); + + /* Look in this namespace. */ + qualified_lookup_using_namespace (name, scope, &binding, 0); + + n_searched++; + + if (binding.value) + VEC_safe_push (tree, heap, candidates, binding.value); + + /* Add child namespaces. */ + for (t = level->namespaces; t; t = DECL_CHAIN (t)) + 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, + "maximum limit of %d namespaces searched for %qE", + max_to_search, name); + + VEC_free (tree, heap, namespaces_to_search); + + /* Nothing useful to report. */ + if (VEC_empty (tree, candidates)) + return; + + inform_n (name_location, VEC_length (tree, candidates), + "suggested alternative:", + "suggested alternatives:"); + + FOR_EACH_VEC_ELT (tree, candidates, ix, t) + inform (location_of (t), " %qE", t); + + VEC_free (tree, heap, candidates); +} + /* Unscoped lookup of a global: iterate over current namespaces, considering using-directives. */ |