From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 4eBvLqSgA2sAES4AWB0awg (envelope-from ) for ; Sun, 22 Nov 2026 21:10:44 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=E5/+3wjp; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B89131E167; Sun, 22 Nov 2026 21:10:44 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id B55431E023 for ; Sun, 22 Nov 2026 21:10:41 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D4AC3480B355 for ; Mon, 23 Nov 2026 02:10:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D4AC3480B355 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=E5/+3wjp Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.7]) by sourceware.org (Postfix) with ESMTPS id 4A11643C2044 for ; Mon, 23 Nov 2026 02:09:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A11643C2044 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=126.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4A11643C2044 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=117.135.210.7 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795399773; cv=none; b=gjwtTDtwtIqCDUhgXRK2WSLjyv7XT0GgLNJ2F9Mq22wBEO+D09X4EnLtEONPw11632YXXm7L9honlvIYX/wC5Pr6luZn5AqDCknb9p9sEzpmt0QRZ8kvJ/djRqWJ4I6fnIrXQjtOsYPLL12QYFUJcGwl/cyMDSTYL15+IjaAfPo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795399773; c=relaxed/simple; bh=RkpLvrRhOrPjAeDSnOJjoEnhaYpKUccMvePCN0A06/c=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=lJtYOx0SjgbjUF0v9icdRQqE/uYDQvIJBnXmXivgkO71vdf6xKQTLYd2a5xDh3dyrlW/NueVzVmy171u951iwCGw/h/etBNJlNvCgu+WBZl3Rses7E5bmKUqts62rjl5GbscxS5WVItG8E6P33CU4+QvKaT0MrQ/MjL1UWZiZr0= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=E5/+3wjp DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A11643C2044 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=z9 0C70cfypw2nnCRvtIWn3uwZfC2Mqrz9wBZAfFn2e8=; b=E5/+3wjpaRo1CNbsZD 2hha7jVU9HshXPA53NaWt3DrJKujiGdFYEIsgA5TUto+fLYxwE1RQC4D/TRXMxR7 PCCLSE6aZOEsI0HugumknalvJR8DpCaKw0ASJ2GGwRJTMgkl165YQCWmNLH9twQA 0xJ8C1FAuJN0VHFl8g/9PlrQQ= Received: from ubuntu-2204.. (unknown []) by gzga-smtp-mtada-g1-0 (Coremail) with SMTP id _____wD3t14+oANrE5tHBA--.60677S3; Mon, 23 Nov 2026 10:09:04 +0800 (CST) From: Sendao Yan To: gdb-patches@sourceware.org Cc: Tom Tromey , Andrew Burgess , Keith Seitz , Simon Marchi , yansendao@126.com Subject: [PATCH v4 1/1] [gdb/symtab] Add canonical-name aliases for C++ template static members Date: Mon, 23 Nov 2026 10:09:01 +0800 Message-Id: <20261123020901.2794477-2-yansendao@126.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261123020901.2794477-1-yansendao@126.com> References: <20261122120312.52699-1-yansendao@126.com> <20261123020901.2794477-1-yansendao@126.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wD3t14+oANrE5tHBA--.60677S3 X-Coremail-Antispam: 1Uf129KBjvAXoWfArWkXrW3Ar4fXryrGF17KFg_yoW5JrW7Ao Wfur43Wa12kr4UuayDCrnrGry5ZFZ5ur98JFn3KrW5ZFnYv343Cry09w1xXanxXr4rGasx X3s7X345tFW7XFyfn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvj4R9zV1UUUUU X-CM-SenderInfo: 51dq2vpqgd0qqrswhudrp/xtbBngPT5WsDoEMK3QAA33 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 omits the integer type suffix (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, so 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). Static member functions have the same problem: their quick lookup uses S<1l>::f while the index only has S<1>::f. Add a synthesized cooked-index alias for C++ static data members and static member functions using the demangled linkage name. The alias is derived from the linkage name of this particular symbol, which encodes the exact template argument type, so T<1> (int) and T<1L> (long) get distinct spellings and are not conflated. The alias is skipped when the entry built from DW_AT_name already has the canonical spelling, so a clang-built binary (whose DWARF name already has the suffix) is unaffected. Only entries whose parent is a type or namespace are considered. Only Itanium-mangled (_Z) names with an I...E argument block are considered. The alias name is produced with the v3 demangler directly (still through gdb_demangle's crash handler), so index construction does not depend on the user's current demangle-style. Functions are demangled without DMGL_PARAMS, since index lookup ignores parameter lists, so the alias is S<1l>::f rather than S<1l>::f(). This keeps the quick lookup authoritative; no symtab-expansion fallback is needed. The DWARF-spelling comparison uses a new FOR_DWARF_NAME flag instead of the default canonical spelling. canonicalize_names() runs per shard concurrently, and a deferred parent may belong to a shard whose canonical field has not been set yet; FOR_DWARF_NAME builds the name from the immutable DWARF names and avoids that race. The comparison allocates its name on a temporary obstack. If the DW_AT_name entry is not adjacent (the index was not built by the DIE scanner, e.g. it came from .debug_names), skip the alias rather than bypassing the checks. The alias does not carry IS_LINKAGE or IS_SYNTHESIZED, so it is written to a saved .gdb_index and .debug_names; C++ linkage names are otherwise omitted from .gdb_index, and synthesized entries are otherwise omitted from .debug_names. For the same reason, synthesize an entry for the canonical spelling of each enclosing template scope (S<1l> for S<1l>::v), because the .gdb_index reader rebuilds qualified names from their components and needs each prefix to be present. Add gdb.cp/static-const-template.exp, which prints a static constexpr member of a class template with a long non-type template parameter, checks that int and long instantiations of the same template remain distinct, covers a member of a template nested in a namespace, checks that a static member function is found with its canonical template arguments, checks that the canonical spellings survive saving and reloading a .gdb_index, and checks that the aliases are emitted in a DWARF-5 .debug_names index. With the unpatched gdb the long lookups fail with ; with the fix they pass. Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc (54/54 PASS, including the -flto variants), the new test, and the existing gdb.dwarf2/static-const-member.exp, gdb.dwarf2/forward-spec-inter-cu.exp, gdb.cp/m-static.exp, gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp (248 PASS); gdb.dwarf2/debug-names*.exp: 27 PASS. Full gdb.cp: 8783 PASS, with only the pre-existing environmental failures in gdb.cp/step-and-next-inline.exp. Also verified by hand that save gdb-index -dwarf-5 / reload finds S<1l>::v and reports its type as const long. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533 --- gdb/cp-support.c | 41 +++++- gdb/cp-support.h | 12 +- gdb/dwarf2/cooked-index-entry.c | 14 +- gdb/dwarf2/cooked-index-entry.h | 9 ++ gdb/dwarf2/cooked-index-shard.c | 125 +++++++++++++++++ gdb/testsuite/gdb.cp/static-const-template.cc | 69 ++++++++++ .../gdb.cp/static-const-template.exp | 129 ++++++++++++++++++ 7 files changed, 389 insertions(+), 10 deletions(-) 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/cp-support.c b/gdb/cp-support.c index 51c836a06d0..d5f6b643797 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -665,6 +665,38 @@ cp_canonicalize_string (const char *string) return us; } +/* See cp-support.h. */ + +gdb::unique_xmalloc_ptr +cp_demangle_template_linkage_name (const char *linkage_name) +{ + /* Only an Itanium-mangled (_Z) C++ name can be a template + instantiation. An Itanium template instantiation always has an + I...E argument block, so avoid demangling names that cannot have + one. The _Z check also keeps plain identifiers that happen to be + valid type encodings out of the index. */ + if (strncmp (linkage_name, "_Z", 2) != 0 + || strchr (linkage_name, 'I') == nullptr) + return nullptr; + + /* Force the v3 demangler: cplus_demangle() returns the mangled name + unchanged when the user has set demangle-style to none, even if + DMGL_GNU_V3 is requested, and the cooked index must not depend on the + user's display preference. This still goes through the demangler + crash handler in gdb_demangle. Do not pass DMGL_PARAMS: index + lookup ignores parameter lists, so a function alias should be + S<1l>::f rather than S<1l>::f(). */ + gdb::unique_xmalloc_ptr demangled + = gdb_demangle (linkage_name, DMGL_ANSI | DMGL_TYPES, true); + + if (demangled == nullptr + || strchr (demangled.get (), ':') == nullptr + || strchr (demangled.get (), '<') == nullptr) + return nullptr; + + return demangled; +} + /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the block of used memory that should be freed when finished with the tree. DEMANGLED_P is set to the char * that should be @@ -1586,7 +1618,7 @@ report_failed_demangle (const char *name, bool core_dump_allowed, /* A wrapper for bfd_demangle. */ gdb::unique_xmalloc_ptr -gdb_demangle (const char *name, int options) +gdb_demangle (const char *name, int options, bool force_v3) { gdb::unique_xmalloc_ptr result; int crash_signal = 0; @@ -1617,7 +1649,12 @@ gdb_demangle (const char *name, int options) #endif if (crash_signal == 0) - result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); + { + if (force_v3) + result.reset (cplus_demangle_v3 (name, options | DMGL_VERBOSE)); + else + result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); + } #ifdef HAVE_WORKING_FORK if (catch_demangler_crashes) diff --git a/gdb/cp-support.h b/gdb/cp-support.h index b3040506c8a..7bc1f5566a2 100644 --- a/gdb/cp-support.h +++ b/gdb/cp-support.h @@ -82,6 +82,12 @@ struct demangle_parse_info extern gdb::unique_xmalloc_ptr cp_canonicalize_string (const char *string); +/* Return the demangled spelling of LINKAGE_NAME when it names a scoped + template instantiation, or NULL otherwise. */ + +extern gdb::unique_xmalloc_ptr cp_demangle_template_linkage_name + (const char *linkage_name); + extern gdb::unique_xmalloc_ptr cp_canonicalize_string_no_typedefs (const char *string); @@ -194,9 +200,11 @@ extern struct cmd_list_element *maint_cplus_cmd_list; libiberty's demangler to expand standard substitutions to their full template name. */ -/* A wrapper for bfd_demangle. */ +/* A wrapper for bfd_demangle. When FORCE_V3 is true, use the v3 + demangler directly, ignoring the user's demangle-style setting. */ -gdb::unique_xmalloc_ptr gdb_demangle (const char *name, int options); +gdb::unique_xmalloc_ptr gdb_demangle (const char *name, int options, + bool force_v3 = false); /* A wrapper for cplus_demangle_print. */ diff --git a/gdb/dwarf2/cooked-index-entry.c b/gdb/dwarf2/cooked-index-entry.c index 78a74273616..0a688ed453c 100644 --- a/gdb/dwarf2/cooked-index-entry.c +++ b/gdb/dwarf2/cooked-index-entry.c @@ -175,7 +175,8 @@ cooked_index_entry::full_name (struct obstack *storage, cooked_index_full_name_flag name_flags, const char *default_sep) const { - const char *local_name = ((name_flags & FOR_MAIN) != 0) ? name () : canonical; + const char *local_name + = ((name_flags & (FOR_MAIN | FOR_DWARF_NAME)) != 0) ? name () : canonical; if ((flags & IS_LINKAGE) != 0 || get_parent () == nullptr) return local_name; @@ -238,11 +239,12 @@ cooked_index_entry::write_scope (struct obstack *storage, cooked_index_shard::prune_nameless_entries. */ gdb_assert (!name_is_deferred ()); - /* When computing the Ada linkage name, the entry might not have - been canonicalized yet, so use the 'name'. */ - const char *local_name = ((flags & (FOR_MAIN | FOR_ADA_LINKAGE_NAME)) != 0 - ? name () - : canonical); + /* When computing the Ada linkage name or the DWARF spelling, the + entry might not have been canonicalized yet, so use the 'name'. */ + const char *local_name + = ((flags & (FOR_MAIN | FOR_ADA_LINKAGE_NAME | FOR_DWARF_NAME)) != 0 + ? name () + : canonical); obstack_grow (storage, local_name, strlen (local_name)); obstack_grow (storage, sep, strlen (sep)); } diff --git a/gdb/dwarf2/cooked-index-entry.h b/gdb/dwarf2/cooked-index-entry.h index 462ec0640a9..d9a8f001e55 100644 --- a/gdb/dwarf2/cooked-index-entry.h +++ b/gdb/dwarf2/cooked-index-entry.h @@ -60,6 +60,10 @@ enum cooked_index_full_name_enum : unsigned char FOR_MAIN = 1, /* Set when requesting the linkage name for an Ada entry. */ FOR_ADA_LINKAGE_NAME = 2, + /* Set when requesting the name as spelled in the DWARF, rather than + the canonical name. This does not read the canonical field, so it + is safe to use before all shards have been canonicalized. */ + FOR_DWARF_NAME = 4, }; DEF_ENUM_FLAGS_TYPE (enum cooked_index_full_name_enum, cooked_index_full_name_flag); @@ -168,6 +172,11 @@ struct cooked_index_entry : public allocate_on_obstack symbols will have their "linkage-style" name computed. The default is source-style. + If the FOR_DWARF_NAME flag is set, the name is built from the + entries' names as spelled in the DWARF; the canonical field is not + consulted. This is safe to use while shards are still being + canonicalized concurrently. + If the language doesn't prescribe a separator, one can be specified using DEFAULT_SEP. */ const char *full_name (struct obstack *storage, diff --git a/gdb/dwarf2/cooked-index-shard.c b/gdb/dwarf2/cooked-index-shard.c index 206fe4483a4..b966b88c7f3 100644 --- a/gdb/dwarf2/cooked-index-shard.c +++ b/gdb/dwarf2/cooked-index-shard.c @@ -23,8 +23,10 @@ #include "dwarf2/read.h" #include "dwarf2/error.h" #include "cp-support.h" +#include "split-name.h" #include "c-lang.h" #include "ada-lang.h" +#include /* Return true if a plain "main" could be the main program for this language. Languages that are known to use some other mechanism are @@ -304,6 +306,7 @@ cooked_index_shard::canonicalize_names () htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, nullptr, xcalloc, xfree)); std::vector new_gnat_entries; + std::vector new_cxx_entries; for (cooked_index_entry *entry : m_entries) { @@ -393,11 +396,133 @@ cooked_index_shard::canonicalize_names () entry->canonical = entry->name (); } + /* For a C++ static data member or member function, 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. The cooked index therefore + needs an entry for the canonical spelling as well. + + The alias is derived from the linkage name of this particular + symbol, so S<1> (int) and S<1L> (long) stay distinct: each symbol + gets the spelling encoded by its own linkage name. Skip the alias + when the entry built from DW_AT_name already uses the canonical + spelling (for instance a clang-built binary whose DWARF name + already has the integer suffix). PR symtab/33533. */ + + /* Names of the enclosing scopes whose canonical spelling has already + been added, to avoid duplicates within this shard. */ + std::unordered_set enclosing_names; + + for (size_t i = 0; i < m_entries.size (); ++i) + { + cooked_index_entry *entry = m_entries[i]; + + if ((entry->flags & IS_LINKAGE) == 0 + || entry->lang != language_cplus + || (entry->tag != DW_TAG_variable + && entry->tag != DW_TAG_member + && entry->tag != DW_TAG_subprogram)) + continue; + + gdb::unique_xmalloc_ptr demangled + = cp_demangle_template_linkage_name (entry->name ()); + if (demangled == nullptr) + continue; + + /* The index entry built from DW_AT_name is added directly before + the linkage-name entry for the same DIE. */ + const cooked_index_entry *dwarf_entry = nullptr; + if (i > 0) + { + const cooked_index_entry *prev = m_entries[i - 1]; + if (prev->die_offset == entry->die_offset + && prev->per_cu == entry->per_cu + && (prev->flags & (IS_LINKAGE | IS_SYNTHESIZED)) == 0) + dwarf_entry = prev; + } + + /* If the DW_AT_name entry is not adjacent, the index was not + built by the DIE scanner (for example, it came from + .debug_names), so the checks below cannot be applied. Skip + the alias rather than adding one without them. */ + if (dwarf_entry == nullptr) + continue; + + /* Use a temporary obstack for this probe. Use the DWARF + spelling: this shard may run before the shard owning an + ancestor has canonicalized its entries. */ + auto_obstack temp; + const char *full = dwarf_entry->full_name (&temp, FOR_DWARF_NAME); + + /* If the full name already has the canonical spelling, there is + nothing to add; this is the case for clang, which includes the + integer suffix in DW_AT_name. */ + if (strcmp (demangled.get (), full) == 0) + continue; + + /* Only add an alias when the parent is a type or namespace. + This excludes function-local statics, whose parent is a + subprogram. */ + const cooked_index_entry *parent = dwarf_entry->get_parent (); + if (parent != nullptr && !tag_is_type (parent->tag)) + continue; + + /* Clear IS_LINKAGE so that the alias is written to a saved + .gdb_index; C++ linkage names are otherwise omitted from the + cache. The alias has no parent, so full_name() still returns + its canonical name. Do not mark it IS_SYNTHESIZED: unlike the + Ada package entries, this alias names a real DIE and should be + written to .debug_names as well. */ + const char *canon = m_names.insert (std::move (demangled)); + cooked_index_entry *alias + = create (entry->die_offset, entry->tag, + entry->flags & ~IS_LINKAGE, + entry->lang, canon, nullptr, entry->per_cu); + alias->canonical = canon; + new_cxx_entries.push_back (alias); + + /* Also add the canonical spelling of each enclosing template + scope. A .gdb_index reader splits a symbol name into its + components and re-links them, but it can only do that if each + prefix has an entry too. These entries are deliberately + parentless and store the whole prefix: the reader re-splits the + name itself, and they are not intended for direct scope lookup. + A lookup for a scope-only spelling can still match one of them + and expand its CU before missing; that is the cost of making + saved indexes round-trip. */ + std::vector components + = split_name (canon, split_style::CXX); + std::string prefix; + for (size_t j = 0; j + 1 < components.size (); ++j) + { + if (!prefix.empty ()) + prefix += "::"; + prefix.append (components[j].data (), components[j].size ()); + + if (prefix.find ('<') == std::string::npos) + continue; + if (!enclosing_names.insert (prefix).second) + continue; + + const char *enclosing_name + = m_names.insert (std::string_view (prefix)); + cooked_index_entry *enclosing + = create (entry->die_offset, DW_TAG_structure_type, + IS_SYNTHESIZED, entry->lang, enclosing_name, nullptr, + entry->per_cu); + enclosing->canonical = enclosing_name; + new_cxx_entries.push_back (enclosing); + } + } + /* Make sure any new Ada entries end up in the results. This isn't done when creating these new entries to avoid invalidating the 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..1b1b8679c82 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.cc @@ -0,0 +1,69 @@ +/* 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 . */ + +/* 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 +struct S +{ + static constexpr unsigned v = N; + using P = int[v]; +}; + +S<1> s; + +/* The same template name instantiated with both int and long non-type + arguments. A canonical-name alias must not conflate the two. */ +template +struct T +{ + static constexpr auto v = N; +}; + +T<1> t_int; +T<1L> t_long; + +/* A member of a template in a namespace: the canonical spelling has + multiple scope components. */ +namespace N +{ + template + struct U + { + static constexpr unsigned v = M; + using P = int[v]; + }; +} + +N::U<1> u; + +/* A class template with only a static member function. */ +template +struct F +{ + static long f () { return N; } + using P = int[N]; +}; + +F<1> f; +long fv = F<1>::f (); + +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..55ef14aefe6 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.exp @@ -0,0 +1,129 @@ +# 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 . + +# 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 . + +standard_testfile .cc + +# Can't save an index with readnow. +require !readnow + +set opts {debug c++ dwarf5} +lappend opts additional_flags=-std=c++17 + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} { + return +} + +# Save a .gdb_index now, before the inferior is started, so that only +# the main objfile is indexed. The aliases must survive this round +# trip; the reload is checked below. +set filename [file tail $binfile] +set host_program [gdb_remote_download host $binfile] +set index_file [host_standard_output_file $filename.gdb-index] +remote_file host delete $index_file +gdb_test_no_output "save gdb-index [file dirname $index_file]" \ + "save gdb-index for $filename" + +if {![remote_file host exists $index_file]} { + unsupported "gdb-index file not created" + return +} +pass "gdb-index file created" + +# Also check that the alias is written to a DWARF-5 .debug_names index. +# Names that are not already in .debug_str are emitted in the .debug_str +# addition file, so check that file. +set debug_str_file [host_standard_output_file $filename.debug_str] +remote_file host delete $debug_str_file +gdb_test_no_output "save gdb-index -dwarf-5 [file dirname $index_file]" \ + "save .debug_names index for $filename" + +if {[remote_file host exists $debug_str_file]} { + pass ".debug_str addition created" + set fp [open $debug_str_file rb] + set contents [read $fp] + close $fp + gdb_assert {[string first "S<1l>::v" $contents] != -1} \ + "canonical alias present in .debug_names" + gdb_assert {[string first "F<1l>::f" $contents] != -1} \ + "canonical function alias present in .debug_names" +} else { + unsupported ".debug_str addition not created" +} + +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" + +# Both T<1> and T<1L> are instantiated. Their DWARF names are identical +# (T<1>), but the canonical spellings differ. Check that the long +# instantiation is not conflated with the int one. +gdb_test "print t_int.v" "= 1" +gdb_test "print t_long.v" "= 1" +gdb_test "whatis 'T<1>::v'" "type = (const )?int" +gdb_test "whatis 'T<1l>::v'" "type = (const )?long( int)?" + +gdb_test "print 'N::U<1l>::v'" "= 1" \ + "print member of a template in a namespace" +gdb_test "whatis 'N::U<1l>::v'" "type = (const )?unsigned int" + +# A static member function of a class template has the same spelling +# mismatch. +gdb_test "print 'F<1l>::f'()" "= 1" \ + "call member function with canonical template arguments" +gdb_test "whatis 'F<1l>::f'" "type = long \\(void\\)" \ + "member function type with canonical template arguments" + +# The synthesized aliases must also survive saving and reloading a +# .gdb_index. Embed the saved index in a copy of the program; the +# index reader rebuilds qualified names from their components, so this +# also checks that the enclosing scope aliases were emitted. +set host_program_with_index $host_program.with-index +set program_with_index $binfile.with-index +remote_file host delete $host_program_with_index +if {[run_on_host "objcopy" [gdb_find_objcopy] \ + [join [list \ + "--remove-section .gdb_index" \ + "--add-section .gdb_index=$index_file" \ + "--set-section-flags .gdb_index=readonly" \ + "$host_program" "$host_program_with_index"]]]} { + return +} + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir +gdb_test "file $program_with_index" "Reading symbols from .*" \ + "load program with embedded .gdb_index" +gdb_test "print 'S<1l>::v'" "= 1" \ + "canonical member after .gdb_index reload" +gdb_test "print 'T<1l>::v'" "= 1" \ + "canonical auto member after .gdb_index reload" +gdb_test "print 'N::U<1l>::v'" "= 1" \ + "canonical namespaced member after .gdb_index reload" +gdb_test "whatis 'F<1l>::f'" "type = long \\(void\\)" \ + "canonical member function after .gdb_index reload" -- 2.34.1