[libcxx-commits] [libcxx] [libcxx] Make std::pair pretty-printer ABI-independent (PR #201768)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 19 08:42:51 PDT 2026


https://github.com/aokblast updated https://github.com/llvm/llvm-project/pull/201768

>From e7929f245e5ff9c656e470d630518bbd55c729e2 Mon Sep 17 00:00:00 2001
From: aokblast <aokblast at FreeBSD.org>
Date: Fri, 5 Jun 2026 15:26:41 +0800
Subject: [PATCH] [llbcxx] Make std::pair pretty-printer ABI-independent

std::pair is printed explicitly instead of relying on GDB's default
struct formatting to keep output stable across ABI configurations.

With _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR (default on
some platforms, e.g. FreeBSD), std::pair gains an empty
__non_trivially_copyable_base base class. GDB would otherwise render
this as <...__non_trivially_copyable_base<...>> = {<No data fields>},
which makes output ABI-dependent.

Only first and second are meaningful, so print them directly.
---
 .../libcxx/gdb/gdb_pretty_printer_test.sh.cpp     | 13 +++++++++++++
 libcxx/utils/gdb/libcxx/printers.py               | 15 +++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/libcxx/test/libcxx/gdb/gdb_pretty_printer_test.sh.cpp b/libcxx/test/libcxx/gdb/gdb_pretty_printer_test.sh.cpp
index 1fa7e5a5ceb7f..b720ef5218d1a 100644
--- a/libcxx/test/libcxx/gdb/gdb_pretty_printer_test.sh.cpp
+++ b/libcxx/test/libcxx/gdb/gdb_pretty_printer_test.sh.cpp
@@ -41,6 +41,7 @@
 #include <tuple>
 #include <unordered_map>
 #include <unordered_set>
+#include <utility>
 
 #include "test_macros.h"
 
@@ -295,6 +296,17 @@ void tuple_test() {
       "empty std::tuple");
 }
 
+void pair_test() {
+  std::pair<int, int> ints(1, 2);
+  ComparePrettyPrintToChars(ints, "{first = 1, second = 2}");
+
+  std::pair<std::string, int> mixed("hello", 42);
+  ComparePrettyPrintToChars(mixed, "{first = \"hello\", second = 42}");
+
+  std::pair<int, std::pair<int, int>> nested(1, {2, 3});
+  ComparePrettyPrintToChars(nested, "{first = 1, second = {first = 2, second = 3}}");
+}
+
 void unique_ptr_test() {
   std::unique_ptr<std::string> matilda(new std::string("Matilda"));
   ComparePrettyPrintToRegex(
@@ -747,6 +759,7 @@ int main(int, char**) {
   //u16string_test();
   u32string_test();
   tuple_test();
+  pair_test();
   unique_ptr_test();
   shared_ptr_test();
   bitset_test();
diff --git a/libcxx/utils/gdb/libcxx/printers.py b/libcxx/utils/gdb/libcxx/printers.py
index ccaa6e9a019e8..06867b9015555 100644
--- a/libcxx/utils/gdb/libcxx/printers.py
+++ b/libcxx/utils/gdb/libcxx/printers.py
@@ -182,6 +182,20 @@ def children(self):
             return iter(())
         return self._Children(self.val)
 
+
+class StdPairPrinter(object):
+    """Print a std::pair.
+
+    Note that we don't rely on GDB's default struct formatting so that we can
+    provide a stable output for std::pair regardless of libc++'s ABI configuration.
+    """
+
+    def __init__(self, val):
+        self.val = val
+
+    def children(self):
+        return iter([("first", self.val["first"]), ("second", self.val["second"])])
+
 class StdStringPrinter(object):
     """Print a std::string."""
 
@@ -901,6 +915,7 @@ def __init__(self, name):
             "basic_string": StdStringPrinter,
             "string": StdStringPrinter,
             "string_view": StdStringViewPrinter,
+            "pair": StdPairPrinter,
             "tuple": StdTuplePrinter,
             "unique_ptr": StdUniquePtrPrinter,
             "shared_ptr": StdSharedPointerPrinter,



More information about the libcxx-commits mailing list