From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 80C773851C17 for ; Wed, 1 Jul 2020 00:26:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 80C773851C17 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 0610QhPt031372 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Jun 2020 20:26:48 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 0610QhPt031372 Received: from [10.0.0.11] (173-246-6-90.qc.cable.ebox.net [173.246.6.90]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id CDFCA1E797; Tue, 30 Jun 2020 20:26:43 -0400 (EDT) Subject: Re: [PATCH 3/3] gdb: make macro_stringify return a gdb::unique_xmalloc_ptr To: Tom Tromey , Simon Marchi via Gdb-patches References: <20200628165628.96730-1-simon.marchi@polymtl.ca> <20200628165628.96730-4-simon.marchi@polymtl.ca> <87r1tw8e9x.fsf@tromey.com> From: Simon Marchi Message-ID: Date: Tue, 30 Jun 2020 20:26:43 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <87r1tw8e9x.fsf@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Language: fr Content-Transfer-Encoding: 7bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Wed, 1 Jul 2020 00:26:44 +0000 X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jul 2020 00:26:53 -0000 On 2020-06-30 4:55 p.m., Tom Tromey wrote: >>>>>> "Simon" == Simon Marchi via Gdb-patches writes: > > Simon> The change to macro_stringify is straightforward. This allows removing > Simon> the manual memory management in fixup_definition. > > Simon> @@ -882,25 +882,19 @@ macro_undef (struct macro_source_file *source, int line, > Simon> static struct macro_definition * > Simon> fixup_definition (const char *filename, int line, struct macro_definition *def) > Simon> { > Simon> - static char *saved_expansion; > [...] > Simon> + gdb::unique_xmalloc_ptr saved_expansion; > > This loses the "static", but that's necessary because this function > returns "def", which has a pointer to the contents. Oh damn, I fixed it last minute (it fails some tests) but probably forgot to git-add. Here's the fixed version: >From e0ce95d17288009301534846fcb8c278f5d58fd7 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sat, 27 Jun 2020 16:12:20 -0400 Subject: [PATCH] gdb: make macro_stringify return a gdb::unique_xmalloc_ptr The change to macro_stringify is straightforward. This allows removing the manual memory management in fixup_definition. gdb/ChangeLog: * macroexp.h (macro_stringify): Return gdb::unique_xmalloc_ptr. * macroexp.c (macro_stringify): Likewise. * macrotab.c (fixup_definition): Update. Change-Id: Id7db8988bdbd569dd51c4f4655b00eb26db277cb --- diff --git a/gdb/macroexp.c b/gdb/macroexp.c index e1d185d..5f749ff 100644 --- a/gdb/macroexp.c +++ b/gdb/macroexp.c @@ -698,7 +698,7 @@ /* See macroexp.h. */ -char * +gdb::unique_xmalloc_ptr macro_stringify (const char *str) { int len = strlen (str); @@ -707,7 +707,7 @@ stringify (&buffer, str, len); buffer.appendc ('\0'); - return buffer.release ().release (); + return buffer.release (); } diff --git a/gdb/macroexp.h b/gdb/macroexp.h index 511991c..2e29d02 100644 --- a/gdb/macroexp.h +++ b/gdb/macroexp.h @@ -78,9 +78,7 @@ int macro_is_digit (int c); -/* Stringify STR according to C rules and return an xmalloc'd pointer - to the result. */ - -char *macro_stringify (const char *str); +/* Stringify STR according to C rules and return a null-terminated string. */ +gdb::unique_xmalloc_ptr macro_stringify (const char *str); #endif /* MACROEXP_H */ diff --git a/gdb/macrotab.c b/gdb/macrotab.c index 63cd301..9ada436 100644 --- a/gdb/macrotab.c +++ b/gdb/macrotab.c @@ -882,25 +882,19 @@ static struct macro_definition * fixup_definition (const char *filename, int line, struct macro_definition *def) { - static char *saved_expansion; - - if (saved_expansion) - { - xfree (saved_expansion); - saved_expansion = NULL; - } + static gdb::unique_xmalloc_ptr saved_expansion; if (def->kind == macro_object_like) { if (def->argc == macro_FILE) { saved_expansion = macro_stringify (filename); - def->replacement = saved_expansion; + def->replacement = saved_expansion.get (); } else if (def->argc == macro_LINE) { - saved_expansion = xstrprintf ("%d", line); - def->replacement = saved_expansion; + saved_expansion.reset (xstrprintf ("%d", line)); + def->replacement = saved_expansion.get (); } }