]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: improve class NTTP object pretty printing [PR111471]
authorPatrick Palka <ppalka@redhat.com>
Wed, 20 Sep 2023 16:09:36 +0000 (12:09 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 22 Sep 2023 10:17:01 +0000 (06:17 -0400)
1. Move class NTTP object pretty printing to a more general spot in
   the pretty printer, so that we always print its value instead of
   its (mangled) name even when it appears outside of a template
   argument list.
2. Print the type of an class NTTP object alongside its CONSTRUCTOR
   value, like dump_expr would have done.
3. Don't print const VIEW_CONVERT_EXPR wrappers for class NTTPs.

PR c++/111471

gcc/cp/ChangeLog:

* cxx-pretty-print.cc (cxx_pretty_printer::expression)
<case VAR_DECL>: Handle class NTTP objects by printing
their type and value.
<case VIEW_CONVERT_EXPR>: Strip const VIEW_CONVERT_EXPR
wrappers for class NTTPs.
(pp_cxx_template_argument_list): Don't handle class NTTP
objects here.

gcc/testsuite/ChangeLog:

* g++.dg/concepts/diagnostic19.C: New test.

(cherry picked from commit 75c4b0cde4835b45350da0a5cd82f1d1a0a7a2f1)

gcc/cp/cxx-pretty-print.cc
gcc/testsuite/g++.dg/concepts/diagnostic19.C [new file with mode: 0644]

index 7f4556d0da2b2ac827d3777d08bdee94dc7632bd..33820aa96b292df6aa570998d9baa029113ea782 100644 (file)
@@ -1116,6 +1116,15 @@ cxx_pretty_printer::expression (tree t)
       t = OVL_FIRST (t);
       /* FALLTHRU */
     case VAR_DECL:
+      if (DECL_NTTP_OBJECT_P (t))
+       {
+         /* Print the type followed by the CONSTRUCTOR value of the
+            NTTP object.  */
+         simple_type_specifier (cv_unqualified (TREE_TYPE (t)));
+         expression (DECL_INITIAL (t));
+         break;
+       }
+      /* FALLTHRU */
     case PARM_DECL:
     case FIELD_DECL:
     case CONST_DECL:
@@ -1256,6 +1265,14 @@ cxx_pretty_printer::expression (tree t)
       pp_cxx_right_paren (this);
       break;
 
+    case VIEW_CONVERT_EXPR:
+      if (TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX)
+       {
+         /* Strip const VIEW_CONVERT_EXPR wrappers for class NTTPs.  */
+         expression (TREE_OPERAND (t, 0));
+         break;
+       }
+      /* FALLTHRU */
     default:
       c_pretty_printer::expression (t);
       break;
@@ -1960,8 +1977,6 @@ pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
          if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
                               && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
            pp->type_id (arg);
-         else if (TREE_CODE (arg) == VAR_DECL && DECL_NTTP_OBJECT_P (arg))
-           pp->expression (DECL_INITIAL (arg));
          else
            pp->expression (arg);
        }
diff --git a/gcc/testsuite/g++.dg/concepts/diagnostic19.C b/gcc/testsuite/g++.dg/concepts/diagnostic19.C
new file mode 100644 (file)
index 0000000..70af30b
--- /dev/null
@@ -0,0 +1,20 @@
+// Verify pretty printing of class NTTP objects.
+// PR c++/111471
+// { dg-do compile { target c++20 } }
+
+struct A { bool value; };
+
+template<A V>
+  requires (V.value) // { dg-message {'\(V\).value \[with V = A\{false\}\]'} }
+void f();
+
+template<A V> struct B { static constexpr auto value = V.value; };
+
+template<class T>
+  requires T::value // { dg-message {'T::value \[with T = B<A\{false\}>\]'} }
+void g();
+
+int main() {
+  f<A{false}>(); // { dg-error "no match" }
+  g<B<A{false}>>(); // { dg-error "no match" } 
+}