re PR c++/45330 (Suggest likely nested-name-specifiers for undeclared identifiers.)
authorNathan Froyd <froydnj@codesourcery.com>
Tue, 14 Dec 2010 20:31:22 +0000 (20:31 +0000)
committerNathan Froyd <froydnj@gcc.gnu.org>
Tue, 14 Dec 2010 20:31:22 +0000 (20:31 +0000)
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
gcc/cp/cp-tree.h
gcc/cp/error.c
gcc/cp/lex.c
gcc/cp/name-lookup.c
gcc/cp/semantics.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/lookup/suggestions1.C [new file with mode: 0644]

index 3ca26973c87a774b8a0f0f0886ceb9b289b8c41f..d9d8f04b5b300eea72c51ca8b5c79a1ea5e9228c 100644 (file)
@@ -1,3 +1,13 @@
+2010-12-14  Nathan Froyd  <froydnj@codesourcery.com>
+
+       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  <jason@redhat.com>
 
        PR c++/46873
index 79f9f24eee04918e15fb54c2e4f83e6fa68774a8..7b18973c49355e374bb2dc38616de6f63d804e41 100644 (file)
@@ -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++ */
 
index e1bac244418f5599a90570ede4edaa8504ff39a9..3e91115ba9d32e336d2e8f56ed10193404921908 100644 (file)
@@ -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);
+    }
+}
index 5a2ae41ff18ae030196e6ab033b00bd5f4ab51a5..b04c89f46b6130e21ba5ea315ab2d6f8266448ba 100644 (file)
@@ -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.  */
index 4cf13809031dc27931f27876735fb9fc5e286288..e2e54507c4694447f1b4f9f42eb81e0b675dc95b 100644 (file)
@@ -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:");
 
index 27e2982a98c392fef3c3c7199f66ce53864f9dd5..25b99320fa67ec1a04661074559ed0f820d1f84f 100644 (file)
@@ -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.
index 5dfc9c2de7cdb25b687ea0c7fdd7df9e1b9b3f67..99c8103ddd1b259dff419bc1232119d8f61b2c6e 100644 (file)
@@ -1,3 +1,8 @@
+2010-12-14  Nathan Froyd  <froydnj@codesourcery.com>
+
+       PR c++/45330
+       * g++.dg/lookup/suggestions1.C: New test.
+
 2010-12-14  Tobias Burnus  <burnus@net-b.de>
 
        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 (file)
index 0000000..1185d58
--- /dev/null
@@ -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 }