public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Add GDB printer for std::integral_constant
Date: Thu, 11 Jan 2024 17:56:46 +0000	[thread overview]
Message-ID: <20240111175837.420604-1-jwakely@redhat.com> (raw)

I was finding it frustrating when returning from a function in GDB and
the return value was shown as $1 = { <no fields> }, so this makes it
print std::true_type or std::false_type.

There are some contexts where the output isn't ideal, e.g. a type
derived from std::true_type will now show something like:

$2 = {{std::integral_constant<bool, true> = std::true_type}};

But I think in most contexts it's an improvement.

Tested aarch64-linux, pushed to trunk.

-- >8 --

libstdc++-v3/ChangeLog:

	* python/libstdcxx/v6/printers.py (StdIntegralConstantPrinter):
	Add printer for std::integral_constant.
	* testsuite/libstdc++-prettyprinters/cxx11.cc: Test it.
---
 libstdc++-v3/python/libstdcxx/v6/printers.py  | 20 +++++++++++++++++++
 .../libstdc++-prettyprinters/cxx11.cc         |  7 +++++++
 2 files changed, 27 insertions(+)

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index bf0dc52ab8c..032a7aa58a2 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -2306,6 +2306,23 @@ class StdLocalePrinter(printer_base):
                     mod = ' with "{}={}"'.format(cat, other)
         return 'std::locale = "{}"{}'.format(name, mod)
 
+class StdIntegralConstantPrinter(printer_base):
+    """Print a std::true_type or std::false_type."""
+
+    def __init__(self, typename, val):
+        self._val = val
+        self._typename = typename
+
+    def to_string(self):
+        value_type = self._val.type.template_argument(0)
+        value = self._val.type.template_argument(1)
+        if value_type.code == gdb.TYPE_CODE_BOOL:
+            if value:
+                return "std::true_type"
+            else:
+                return "std::false_type"
+        typename = strip_versioned_namespace(self._typename)
+        return "{}<{}, {}>".format(typename, value_type, value)
 
 # A "regular expression" printer which conforms to the
 # "SubPrettyPrinter" protocol from gdb.printing.
@@ -2788,6 +2805,9 @@ def build_libstdcxx_dictionary():
     # vector<bool>
     libstdcxx_printer.add_version('std::', 'locale', StdLocalePrinter)
 
+    libstdcxx_printer.add_version('std::', 'integral_constant',
+                                  StdIntegralConstantPrinter)
+
     if hasattr(gdb.Value, 'dynamic_type'):
         libstdcxx_printer.add_version('std::', 'error_code',
                                       StdErrorCodePrinter)
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc
index bc869fd4122..f867ea18306 100644
--- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc
@@ -207,6 +207,13 @@ main()
   std::atomic<Value> av{{8, 9}};
   // { dg-final { note-test av {std::atomic<Value> = { {i = 8, j = 9} }} } }
 
+  std::integral_constant<int, 1> one;
+  // { dg-final { note-test one {std::integral_constant<int, 1>} } }
+  std::integral_constant<bool, true> truth;
+  // { dg-final { note-test truth {std::true_type} } }
+  std::integral_constant<bool, 0> lies;
+  // { dg-final { note-test lies {std::false_type} } }
+
   placeholder(""); // Mark SPOT
   use(efl);
   use(fl);
-- 
2.43.0


                 reply	other threads:[~2024-01-11 17:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240111175837.420604-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).