Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Sendao Yan <yansendao@126.com>
To: gdb-patches@sourceware.org
Cc: tom@tromey.com, aburgess@redhat.com, keiths@redhat.com,
	yansendao@126.com
Subject: [PATCH v2] [gdb/symtab] Add canonical-name aliases for C++ template static members
Date: Sat, 21 Nov 2026 22:59:30 +0800	[thread overview]
Message-ID: <20261121145930.1048662-1-yansendao@126.com> (raw)
In-Reply-To: <20261120091803.89033-1-yansendao@126.com>

The cooked index stores names as they appear in the DWARF.  For a static
data member of a class template, GCC's DW_AT_name uses the DWARF spelling
of the template arguments (e.g. S<1>::v), while the C++ expression
evaluator looks the member up using the canonical spelling from the
demangled linkage name (S<1l>::v).  Since commit 86ac8c546235
lookup_symbol_in_objfile only searches the cooked index, the lookup
misses and GDB reports the member as optimized out.  This makes
libstdc++'s chrono pretty-printer fail (PR symtab/33533, GCC PR 128817).

Fix this in the index: for a C++ static data member (DW_TAG_variable or
DW_TAG_member with a linkage name) whose canonical name contains template
arguments, add a synthesized cooked-index entry using the demangled
linkage name.  The normal quick lookup then finds the symbol, so no
symtab-expansion fallback is needed.

Add gdb.cp/static-const-template.exp, which prints a static constexpr
member of a class template with a long non-type template parameter.  With
the unpatched gdb it fails with <optimized out>; with the fix it passes.

Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc
(print hms/hms_nano, also with -flto), the new test, and the existing
gdb.dwarf2/static-const-member.exp, gdb.cp/m-static.exp,
gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533
---
 gdb/dwarf2/cooked-index-shard.c               | 38 +++++++++++++++++-
 gdb/testsuite/gdb.cp/static-const-template.cc | 33 +++++++++++++++
 .../gdb.cp/static-const-template.exp          | 40 +++++++++++++++++++
 3 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc
 create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp

diff --git a/gdb/dwarf2/cooked-index-shard.c b/gdb/dwarf2/cooked-index-shard.c
index 206fe4483a4..ad4a0cb6c87 100644
--- a/gdb/dwarf2/cooked-index-shard.c
+++ b/gdb/dwarf2/cooked-index-shard.c
@@ -23,6 +23,7 @@
 #include "dwarf2/read.h"
 #include "dwarf2/error.h"
 #include "cp-support.h"
+#include "demangle.h"
 #include "c-lang.h"
 #include "ada-lang.h"
 
@@ -304,6 +305,7 @@ cooked_index_shard::canonicalize_names ()
   htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry,
 					   nullptr, xcalloc, xfree));
   std::vector<cooked_index_entry *> new_gnat_entries;
+  std::vector<cooked_index_entry *> new_cxx_entries;
 
   for (cooked_index_entry *entry : m_entries)
     {
@@ -319,7 +321,39 @@ cooked_index_shard::canonicalize_names ()
 	 here, then a check might be required there.  */
       gdb_assert (entry->canonical == nullptr);
       if ((entry->flags & IS_LINKAGE) != 0)
-	entry->canonical = entry->name ();
+	{
+	  entry->canonical = entry->name ();
+
+	  /* For a C++ static data member, the DWARF name of the
+	     enclosing template can use a non-canonical spelling of the
+	     template arguments, while lookups use the canonical
+	     spelling from the demangled linkage name.  For example,
+	     GCC emits S<1>::v while the demangled linkage name is
+	     S<1l>::v.  Add an alias entry using the canonical
+	     demangled name so that the cooked-index lookup succeeds.
+	     PR symtab/33533.  */
+	  if (entry->lang == language_cplus
+	      && (entry->tag == DW_TAG_variable
+		  || entry->tag == DW_TAG_member))
+	    {
+	      gdb::unique_xmalloc_ptr<char> demangled
+		= gdb_demangle (entry->name (),
+				DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
+	      if (demangled != nullptr
+		  && strchr (demangled.get (), ':') != nullptr
+		  && strchr (demangled.get (), '<') != nullptr
+		  && strcmp (demangled.get (), entry->name ()) != 0)
+		{
+		  const char *canon = m_names.insert (std::move (demangled));
+		  cooked_index_entry *alias
+		    = create (entry->die_offset, entry->tag,
+			      entry->flags | IS_SYNTHESIZED, entry->lang,
+			      canon, nullptr, entry->per_cu);
+		  alias->canonical = canon;
+		  new_cxx_entries.push_back (alias);
+		}
+	    }
+	}
       else if (entry->lang == language_ada)
 	{
 	  /* Newer versions of GNAT emit DW_TAG_module and use a
@@ -398,6 +432,8 @@ cooked_index_shard::canonicalize_names ()
      m_entries iterator used in the foreach above.  */
   m_entries.insert (m_entries.end (), new_gnat_entries.begin (),
 		    new_gnat_entries.end ());
+  m_entries.insert (m_entries.end (), new_cxx_entries.begin (),
+		    new_cxx_entries.end ());
 
   m_entries.shrink_to_fit ();
   std::sort (m_entries.begin (), m_entries.end (),
diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc
new file mode 100644
index 00000000000..036e6b3b593
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/static-const-template.cc
@@ -0,0 +1,33 @@
+/* Copyright 2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* A static constexpr member of a class template with a non-type template
+   parameter.  The reference from the member typedef forces GCC to emit the
+   declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5.  */
+
+template<long N>
+struct S
+{
+  static constexpr unsigned v = N;
+  using P = int[v];
+};
+
+S<1> s;
+
+int
+main ()
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp
new file mode 100644
index 00000000000..b68d646cd09
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/static-const-template.exp
@@ -0,0 +1,40 @@
+# Copyright 2026 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test printing a static constexpr member of a class template with a
+# non-type template parameter.
+#
+# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression
+# evaluator looks the member up using the canonical spelling S<1l>.  The
+# cooked index stores the DWARF spelling, so before the fix for PR
+# symtab/33533 the lookup missed and gdb printed <optimized out>.
+
+standard_testfile .cc
+
+set opts {debug c++ dwarf5}
+lappend opts additional_flags=-std=c++11
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} {
+    return
+}
+
+if {![runto_main]} {
+    return
+}
+
+gdb_test "print s.v" "= 1" \
+    "print static constexpr member of a template"
+
+gdb_test "print 'S<1l>::v'" "= 1" \
+    "print static constexpr member with canonical template arguments"
-- 
2.34.1


  parent reply	other threads:[~2026-11-21 15:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  9:18 [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses Sendao Yan
2026-11-20 18:26 ` Andrew Burgess
2026-11-20 19:46   ` Keith Seitz
2026-11-21  0:50   ` Tom Tromey
2026-11-21  3:53     ` Simon Marchi
2026-11-21 16:31       ` Tom Tromey
2026-11-22 12:03         ` [PATCH v3 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members Sendao Yan
2026-11-22 12:03           ` [PATCH v3 1/1] " Sendao Yan
2026-11-23  2:09           ` [PATCH v4 0/1] " Sendao Yan
2026-11-23  2:09             ` [PATCH v4 1/1] " Sendao Yan
2026-11-21 14:59 ` Sendao Yan [this message]
2026-11-21 15:07   ` [PATCH v2] " Sendao Yan

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=20261121145930.1048662-1-yansendao@126.com \
    --to=yansendao@126.com \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=keiths@redhat.com \
    --cc=tom@tromey.com \
    /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