]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: avoid redundant scope in diagnostics
authorJason Merrill <jason@redhat.com>
Fri, 18 Jun 2021 20:04:28 +0000 (16:04 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 3 Dec 2021 22:21:12 +0000 (17:21 -0500)
We can make some function signatures shorter to print by omitting redundant
nested-name-specifiers in the rest of the declarator.

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
gcc/testsuite/g++.dg/diagnostic/scope1.C [new file with mode: 0644]

index 98c1f0e4bdfdf30c1d195eb451cfa5dd98d80a72..daea3b39a15cf4bfc1803a23d03e7f8988ec1f96 100644 (file)
@@ -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 (file)
index 0000000..14d0a1b
--- /dev/null
@@ -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" }
+}