From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8ZzZB+uV12r5DzsAWB0awg (envelope-from ) for ; Tue, 20 Oct 2026 12:25:15 -0400 Received: by simark.ca (Postfix, from userid 112) id 1BE3B1E04D; Tue, 20 Oct 2026 12:25:15 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham 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 17A271E04D for ; Tue, 20 Oct 2026 12:25:14 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B07154A9C025 for ; Tue, 20 Oct 2026 16:25:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B07154A9C025 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id C18694A9A899 for ; Tue, 20 Oct 2026 16:23:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C18694A9A899 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C18694A9A899 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792513383; cv=none; b=cWhuFHVUmXUhZEEkb4gwfqCeQ2rAx4SdTbAnLlQMc1HATz//VGvvT8ZRbXWxXr8WDwAQ2gXuPdxTR94CRo0oDA2J+/RfvPKCr/hxdAW+/B/dRFz0Tv04pAREtE+M13a+cHmCVc8gYO30Mr1dTZ88vB1hnOCto2B/VlWS+jPkwSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792513383; c=relaxed/simple; bh=K/wQ66LjSe2uFHusZWVQBOJLUFgdBrUFK77kmDJcNVc=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=DuftlE317ldJflm3mvHQyRg8Uo+3Rd0cB09uYhidMXaGVZBirrZlDC0WFU+rgTnTnKcne30a6vQi6wBOafEWnNkXWvOLIORC+V0RnYGVUETN++BVPPWqV6xetAWGciZy2eZWlGa0nTU6QJ0Mx9P2Q7h+yb2vl0O6uazIwt8/wQ8= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C18694A9A899 Received: by simark.ca (Postfix) id 8BC3A1E091; Tue, 20 Oct 2026 12:23:01 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 13/13] gdb: handle universal character names in C++ name parser Date: Tue, 20 Oct 2026 12:17:35 -0400 Message-ID: <20261020162054.2140671-14-simon.marchi@efficios.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261020162054.2140671-1-simon.marchi@efficios.com> References: <20261020162054.2140671-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 From: Simon Marchi clang uses UCN, or "universal character names" (\u followed by exactly 4 hex digits or \U followed by exactly 8), in the names of templates with character arguments, for instance: C16 WChar The demangler spells them: C16<(char16_t)338> WChar<(wchar_t)128512> Handle them in cp_parse_escape, the same way as hexadecimal escapes, but with the exact number of digits. If there are fewer or more digits than expected, the escape sequence is invalid. In C++, extra digits would instead be separate characters, making it a multicharacter literal, but it doesn't matter here, since we reject those anyway. The value of a universal character name can be 2^31 or more. For instance, clang names a type with a char32_t template argument of value 0xffffffff: C The values of character literals are currently returned as an int and formatted with "%d", which results in "-1". This creates a demangle_component of type DEMANGLE_COMPONENT_LITERAL with a negative value, which is unexpected. The demangler normally represents a negative value with DEMANGLE_COMPONENT_LITERAL_NEG and a string holding the absolute value. To avoid this, change cp_parse_escape to return an unsigned int and format the value with "%u". This makes subsequent patches simpler, as we don't have to handle the case of DEMANGLE_COMPONENT_LITERAL with a value string holding a negative value. Add some tests: - canonicalization selftests - parser selftests checking that malformed UCNs don't parse - cases to gdb.dwarf2/nontype-template-lookup.exp - cases to gdb.cp/nontype-template-rtti.exp Change-Id: I374333f2b629f921542ae03eec25c4624e8bb11f --- gdb/cp-name-parser.c | 39 ++++++++++++++----- gdb/testsuite/gdb.cp/nontype-template-rtti.cc | 2 + .../gdb.cp/nontype-template-rtti.exp | 5 ++- .../gdb.dwarf2/nontype-template-lookup.exp | 24 ++++++++++++ 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/gdb/cp-name-parser.c b/gdb/cp-name-parser.c index abc2e2f19eed..c8d96c9ace33 100644 --- a/gdb/cp-name-parser.c +++ b/gdb/cp-name-parser.c @@ -482,11 +482,11 @@ c_parse_backslash (int host_char, int *target_char) If the escape sequence is invalid, return an empty optional. */ -static std::optional +static std::optional cp_parse_escape (const char **string_ptr) { int target_char; - int c = *(*string_ptr)++; + unsigned int c = static_cast (*(*string_ptr)++); if (c == '\0') { @@ -500,11 +500,15 @@ cp_parse_escape (const char **string_ptr) switch (c) { case 'x': + case 'u': + case 'U': { - /* Hexadecimal escape (\x followed by one or more hex digits). + /* Hexadecimal escape (\x followed by one or more hex digits) or + universal character name (\u followed by exactly 4 hex digits, + \U followed by exactly 8). - An escape with more than 8 digits will silently wrap around, - that's ok. */ + An hex escape with more than 8 digits will silently wrap around, + that's ok.*/ unsigned int value = 0; int count = 0; @@ -514,7 +518,9 @@ cp_parse_escape (const char **string_ptr) count++; } - if (count == 0) + if (count == 0 + || (c == 'u' && count != 4) + || (c == 'U' && count != 8)) return {}; return value; @@ -577,10 +583,10 @@ lex_char_literal (cp_name_parser_YYSTYPE *lvalp, /* Skip opening quote. */ state->lexptr++; - int c = *state->lexptr++; + unsigned int c = static_cast (*state->lexptr++); if (c == '\\') { - std::optional escaped = cp_parse_escape (&state->lexptr); + std::optional escaped = cp_parse_escape (&state->lexptr); if (!escaped.has_value ()) { cpname_yyerror (state, _("invalid escape sequence")); @@ -602,7 +608,7 @@ lex_char_literal (cp_name_parser_YYSTYPE *lvalp, /* We over-allocate here, but it doesn't really matter. */ char *copy = (char *) obstack_alloc (&state->demangle_info->obstack, 30); - xsnprintf (copy, 30, "%d", c); + xsnprintf (copy, 30, "%u", c); c = *state->lexptr++; if (c == '\0') @@ -1168,6 +1174,14 @@ canonicalize_tests () should_canonicalize_to ("C", "C<(char32_t)128512>"); should_canonicalize_to ("C<(char32_t) 128512>", "C<(char32_t)128512>"); + /* Universal character names. */ + should_canonicalize_to ("C", "C<(char16_t)338>"); + should_canonicalize_to ("C<(char16_t) 338>", "C<(char16_t)338>"); + should_canonicalize_to ("C", "C<(char32_t)128512>"); + should_canonicalize_to ("C", "C<(char32_t)4294967295>"); + should_canonicalize_to ("C<(char32_t) 4294967295>", + "C<(char32_t)4294967295>"); + /* Identifiers that start like an encoding prefix. */ should_canonicalize_to ("C", "C"); @@ -1204,6 +1218,13 @@ parser_tests () should_not_parse ("C<'\\x", "invalid escape sequence"); should_not_parse ("C<'\\xg'>", "invalid escape sequence"); should_not_parse ("C<'\\xabg'>", "invalid character constant"); + + should_not_parse ("C", "invalid escape sequence"); + should_not_parse ("C", "invalid escape sequence"); + should_not_parse ("C", "invalid escape sequence"); + should_not_parse ("C", "invalid escape sequence"); + should_not_parse ("C", "invalid escape sequence"); + should_not_parse ("C", "invalid escape sequence"); } #endif diff --git a/gdb/testsuite/gdb.cp/nontype-template-rtti.cc b/gdb/testsuite/gdb.cp/nontype-template-rtti.cc index e19c7e4371ba..ebd6f3b6aede 100644 --- a/gdb/testsuite/gdb.cp/nontype-template-rtti.cc +++ b/gdb/testsuite/gdb.cp/nontype-template-rtti.cc @@ -34,6 +34,8 @@ base *char32_a = new S; #ifdef __cpp_char8_t base *char8_a = new S; #endif +base *char16_0x152 = new S; +base *char32_0xffffffff = new S; int main () diff --git a/gdb/testsuite/gdb.cp/nontype-template-rtti.exp b/gdb/testsuite/gdb.cp/nontype-template-rtti.exp index f3de351b648a..5ea53a1bdc65 100644 --- a/gdb/testsuite/gdb.cp/nontype-template-rtti.exp +++ b/gdb/testsuite/gdb.cp/nontype-template-rtti.exp @@ -55,6 +55,8 @@ set objects { {wchar_a wchar_t 0x61} {char16_a char16_t 0x61} {char32_a char32_t 0x61} + {char16_0x152 char16_t 0x152} + {char32_0xffffffff char32_t 0xffffffff} } if {[cxx_compiler_supports_cxx20]} { @@ -67,7 +69,8 @@ foreach obj $objects { # gcc spells the template arguments of these objects in a way GDB # doesn't match yet. if {[test_compiler_info gcc-* c++] - && $var in {uchar_200 schar_5 wchar_a char16_a char32_a char8_a}} { + && $var in {uchar_200 schar_5 wchar_a char16_a char32_a char8_a + char16_0x152 char32_0xffffffff}} { setup_kfail "gdb/19320" *-*-* } diff --git a/gdb/testsuite/gdb.dwarf2/nontype-template-lookup.exp b/gdb/testsuite/gdb.dwarf2/nontype-template-lookup.exp index 537d5cb1e9e1..42f0a8f1609c 100644 --- a/gdb/testsuite/gdb.dwarf2/nontype-template-lookup.exp +++ b/gdb/testsuite/gdb.dwarf2/nontype-template-lookup.exp @@ -65,6 +65,27 @@ Dwarf::assemble $asm_file { DW_AT_name {Char8Prefix} DW_AT_byte_size 1 DW_FORM_sdata } + + # This is how clang encodes a char16_t value, using a universal + # character name. + structure_type { + DW_AT_name {Char16UCN} + DW_AT_byte_size 1 DW_FORM_sdata + } + + # This is how clang encodes a wchar_t value, using a universal + # character name. + structure_type { + DW_AT_name {WCharUCN} + DW_AT_byte_size 1 DW_FORM_sdata + } + + # This is how clang encodes a char32_t value, using a universal + # character name. + structure_type { + DW_AT_name {Char32Max} + DW_AT_byte_size 1 DW_FORM_sdata + } } } } @@ -83,6 +104,9 @@ foreach name [list \ {Char16Prefix<(char16_t)97>} \ {Char32Prefix<(char32_t)97>} \ {Char8Prefix<(char8_t)97>} \ + {Char16UCN<(char16_t)338>} \ + {WCharUCN<(wchar_t)128512>} \ + {Char32Max<(char32_t)4294967295>} \ ] { gdb_test "ptype $name" \ "type = struct [string_to_regexp $name] \{.*\}" -- 2.56.0