This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Fix PR c++/23586: Bad diagnostic for invalid namespace-name


The following patch improves the error message for invalid namespaces.
For an invalid code snippet like

  int i;
  namespace N = i;

we issue the error message

  bug.cc:2: error: expected namespace-name before ';' token
  bug.cc:2: error: unknown namespace '<declaration error>'

with the not very helpful '<declaration error>'.

This is because the second part of the error message is emitted too
late, namely by do_namespace_alias in name-lookup.c: The invalid
namespace name has already been replaced by an error_mark_node
which is printed as '<declaration error>'.

The situation for the code snippet

  int i;
  using namespace i;

is similar. Here we get the error message:

  bug.cc:2: error: expected namespace-name before ';' token
  bug.cc:2: error: '<type error>' is not a namespace

With the old parser we got

  bug.cc:2: error: unknown namespace `i'

resp.

  bug.cc:2: error: namespace `i' undeclared

so this qualifies as a diagnostic regression.


The following patch moves the generation of the second part of the
error message from do_namespace_alias resp. do_using_directive
to the parser (cp_parser_namespace_name) where the error is diagnosed
and where all information about the invalid namespace name is still
present.

It adds checks to d_n_a and d_u_d to return early on error_mark_node
and asserts that only NAMESPACE_DECL are handled.
It also removes some code from do_using_directive that tries to handle
other cases, but appears to be dead since the new parser went in.

With the patch both error messages read:

  bug.cc:2: error: 'i' is not a namespace-name
  bug.cc:2: error: expected namespace-name before ';' token

Bootstrapped and regtested on i686-pc-linux-gnu.
Ok for mainline and 4.0 branch?
(I haven't tested the patch for the 3.4 branch yet.)

Regards,
Volker


2005-08-26  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	* parser.c (cp_parser_namespace_name): Move diagnostic for
        invalid namespace-name to here from ...
	* name-lookup.c (do_namespace_alias): ... here and ...
	(do_using_directive): ... here.  Remove dead code.


===================================================================
--- gcc/gcc/cp/parser.c	24 Aug 2005 10:21:31 -0000	1.351
+++ gcc/gcc/cp/parser.c	26 Aug 2005 16:07:04 -0000
@@ -10254,6 +10254,8 @@ cp_parser_namespace_name (cp_parser* par
   if (namespace_decl == error_mark_node
       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
     {
+      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
+	error ("%qD is not a namespace-name", identifier);
       cp_parser_error (parser, "expected namespace-name");
       namespace_decl = error_mark_node;
     }
===================================================================
--- gcc/gcc/cp/name-lookup.c	16 Aug 2005 00:13:47 -0000	1.137
+++ gcc/gcc/cp/name-lookup.c	26 Aug 2005 18:28:54 -0000
@@ -3037,12 +3037,10 @@ namespace_ancestor (tree ns1, tree ns2)
 void
 do_namespace_alias (tree alias, tree namespace)
 {
-  if (TREE_CODE (namespace) != NAMESPACE_DECL)
-    {
-      /* The parser did not find it, so it's not there.  */
-      error ("unknown namespace %qD", namespace);
-      return;
-    }
+  if (namespace == error_mark_node)
+    return;
+
+  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
 
   namespace = ORIGINAL_NAMESPACE (namespace);
 
@@ -3191,26 +3189,15 @@ do_using_directive (tree namespace)
 {
   tree context = NULL_TREE;
 
+  if (namespace == error_mark_node)
+    return;
+
+  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
+
   if (building_stmt_tree ())
     add_stmt (build_stmt (USING_STMT, namespace));
-  
-  /* using namespace A::B::C; */
-  if (TREE_CODE (namespace) == SCOPE_REF)
-      namespace = TREE_OPERAND (namespace, 1);
-  if (TREE_CODE (namespace) == IDENTIFIER_NODE)
-    {
-      /* Lookup in lexer did not find a namespace.  */
-      if (!processing_template_decl)
-	error ("namespace %qT undeclared", namespace);
-      return;
-    }
-  if (TREE_CODE (namespace) != NAMESPACE_DECL)
-    {
-      if (!processing_template_decl)
-	error ("%qT is not a namespace", namespace);
-      return;
-    }
   namespace = ORIGINAL_NAMESPACE (namespace);
+
   if (!toplevel_bindings_p ())
     {
       push_using_directive (namespace);
===================================================================



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]