public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 05/10] Fix C++ name canonicalizations of character literals
Date: Sun, 21 Apr 2024 11:00:05 -0600	[thread overview]
Message-ID: <20240421-canon-fixes-v1-5-4dc4791d270d@tromey.com> (raw)
In-Reply-To: <20240421-canon-fixes-v1-0-4dc4791d270d@tromey.com>

The names "void C<(char)1>::m()" and "void C<'\001'>::m()" should
canonicalize to the same string, but currently they do not -- the
former remains unchanged and the latter is transformed to
"void C<(char)'\001'>::m()".

This patch fixes the bug and also adds some unit tests.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16843
---
 gdb/cp-name-parser.y | 49 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 6 deletions(-)

diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y
index 6003ed0b01e..6590194545f 100644
--- a/gdb/cp-name-parser.y
+++ b/gdb/cp-name-parser.y
@@ -44,6 +44,7 @@
 #include "cp-support.h"
 #include "c-support.h"
 #include "parser-defs.h"
+#include "gdbsupport/selftest.h"
 
 #define GDB_YY_REMAP_PREFIX cpname
 #include "yy-remap.h"
@@ -1514,6 +1515,7 @@ yylex (YYSTYPE *lvalp, cpname_state *state)
   int c;
   int namelen;
   const char *tokstart;
+  char *copy;
 
  retry:
   state->prev_lexptr = state->lexptr;
@@ -1544,6 +1546,10 @@ yylex (YYSTYPE *lvalp, cpname_state *state)
 	  return ERROR;
 	}
 
+      /* We over-allocate here, but it doesn't really matter . */
+      copy = (char *) obstack_alloc (&state->demangle_info->obstack, 30);
+      xsnprintf (copy, 30, "%d", c);
+
       c = *state->lexptr++;
       if (c != '\'')
 	{
@@ -1551,15 +1557,10 @@ yylex (YYSTYPE *lvalp, cpname_state *state)
 	  return ERROR;
 	}
 
-      /* FIXME: We should refer to a canonical form of the character,
-	 presumably the same one that appears in manglings - the decimal
-	 representation.  But if that isn't in our input then we have to
-	 allocate memory for it somewhere.  */
       lvalp->comp
 	= state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
 			    state->make_builtin_type ("char"),
-			    state->make_name (tokstart,
-					      state->lexptr - tokstart));
+			    state->make_name (copy, strlen (copy)));
 
       return INT;
 
@@ -1970,3 +1971,39 @@ cp_demangled_name_to_comp (const char *demangled_name,
 
   return result;
 }
+
+#if GDB_SELF_TEST
+
+static void
+should_be_the_same (const char *one, const char *two)
+{
+  gdb::unique_xmalloc_ptr<char> cpone = cp_canonicalize_string (one);
+  gdb::unique_xmalloc_ptr<char> cptwo = cp_canonicalize_string (two);
+
+  if (cpone != nullptr)
+    one = cpone.get ();
+  if (cptwo != nullptr)
+    two = cptwo.get ();
+
+  SELF_CHECK (strcmp (one, two) == 0);
+}
+
+static void
+canonicalize_tests ()
+{
+  should_be_the_same ("short int", "short");
+  should_be_the_same ("int short", "short");
+
+  should_be_the_same ("C<(char) 1>::m()", "C<(char) '\\001'>::m()");
+}
+
+#endif
+
+void _initialize_cp_name_parser ();
+void
+_initialize_cp_name_parser ()
+{
+#if GDB_SELF_TEST
+  selftests::register_test ("canonicalize", canonicalize_tests);
+#endif
+}

-- 
2.44.0


  parent reply	other threads:[~2024-04-21 17:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-21 17:00 [PATCH 00/10] Fix some C++ name canonicalizer problems Tom Tromey
2024-04-21 17:00 ` [PATCH 01/10] Remove test code from cp-name-parser.y Tom Tromey
2024-04-22 17:11   ` John Baldwin
2024-04-21 17:00 ` [PATCH 02/10] Allow initialization functions in .y files Tom Tromey
2024-04-21 17:00 ` [PATCH 03/10] Clean up demangle_parse_info Tom Tromey
2024-04-22 17:12   ` John Baldwin
2024-04-21 17:00 ` [PATCH 04/10] Change storage of demangle_component Tom Tromey
2024-04-22 17:17   ` John Baldwin
2024-04-21 17:00 ` Tom Tromey [this message]
2024-04-22 17:19   ` [PATCH 05/10] Fix C++ name canonicalizations of character literals John Baldwin
2024-04-21 17:00 ` [PATCH 06/10] Remove some unnecessary allocations from cpname_state::parse_number Tom Tromey
2024-04-22 17:20   ` John Baldwin
2024-04-21 17:00 ` [PATCH 07/10] Fix C++ canonicalization of hex literals Tom Tromey
2024-04-22 17:22   ` John Baldwin
2024-04-21 17:00 ` [PATCH 08/10] Implement C++14 numeric separators Tom Tromey
2024-04-22 17:29   ` John Baldwin
2024-04-24 21:42     ` Tom Tromey
2024-04-30 16:33       ` John Baldwin
2024-04-21 17:00 ` [PATCH 09/10] Allow function types as template parameters in name canonicalizer Tom Tromey
2024-04-22 17:30   ` John Baldwin
2024-04-21 17:00 ` [PATCH 10/10] Add spaceship operator to cp-name-parser.y Tom Tromey
2024-04-22 17:31   ` John Baldwin
2024-05-14 19:29 ` [PATCH 00/10] Fix some C++ name canonicalizer problems Tom Tromey

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=20240421-canon-fixes-v1-5-4dc4791d270d@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.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).