We can make some function signatures shorter to print by omitting redundant
nested-name-specifiers in the rest of the declarator.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

        * error.c (current_dump_scope): New variable.
        (dump_scope): Check it.
        (dump_function_decl): Set it.

gcc/testsuite/ChangeLog:

        * g++.dg/diagnostic/scope1.C: New test.
---
 gcc/cp/error.c                           | 10 +++++++++-
 gcc/testsuite/g++.dg/diagnostic/scope1.C | 12 ++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/scope1.C

diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 98c1f0e4bdf..daea3b39a15 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -211,6 +211,10 @@ dump_module_suffix (cxx_pretty_printer *pp, tree decl)
       }
 }
 
+/* The scope of the declaration we're currently printing, to avoid redundantly
+   dumping the same scope on parameter types.  */
+static tree current_dump_scope;
+
 /* Dump a scope, if deemed necessary.  */
 
 static void
@@ -218,7 +222,7 @@ dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
 {
   int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
 
-  if (scope == NULL_TREE)
+  if (scope == NULL_TREE || scope == current_dump_scope)
     return;
 
   /* Enum values within an unscoped enum will be CONST_DECL with an
@@ -1756,6 +1760,10 @@ dump_function_decl (cxx_pretty_printer *pp, tree t, int 
flags)
   else
     dump_scope (pp, CP_DECL_CONTEXT (t), flags);
 
+  /* Name lookup for the rest of the function declarator is implicitly in the
+     scope of the function, so avoid printing redundant scope qualifiers.  */
+  auto cds = make_temp_override (current_dump_scope, CP_DECL_CONTEXT (t));
+
   dump_function_name (pp, t, dump_function_name_flags);
 
   if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
diff --git a/gcc/testsuite/g++.dg/diagnostic/scope1.C 
b/gcc/testsuite/g++.dg/diagnostic/scope1.C
new file mode 100644
index 00000000000..14d0a1bfab6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/scope1.C
@@ -0,0 +1,12 @@
+// Test for avoiding redundant scope qualifiers.
+
+struct A
+{
+  struct B { };
+  static void f(B,B);          // { dg-message {A::f\(B, B\)} }
+};
+
+int main()
+{
+  A::f(42);                    // { dg-error "no match" }
+}

base-commit: 7e71909af2cf3aeec9bed4f6a3cc42c1d17cd661
-- 
2.27.0

Reply via email to