Re: A couple of uses of xmalloc and xfree in a couple of .y files

Pedro Alves
2008-10-27T19:30:53Z

[-- Attachment #1: Type: text/plain, Size: 1259 bytes --]

On Monday 27 October 2008 11:48:00, Pedro Alves wrote:
> Ok, I think that makes for: one "funny we don't do it", one "got mildly
> tickled by the inconsistency, although doesn't care that much, but
> wrote the patch anyway", one "non-silent don't care", a bunch of
> silent don't care's, and one "it's useful".
>
> I believe that's a positive balance. :-)
>
> Checked in.
>

Grrrrr, things are never that simple...

Somehow, I missed rebuilding this file (.y.c doesn't depend on
Makefile.in), so I missed this breakage:

cp-name-parser.c.tmp: In function 'cpname_parse':
cp-name-parser.c.tmp:1990: warning: implicit declaration of function 'xfree'

The fix is to include "defs.h" instead of "config.h" directly, as the
other .y files do. While doing that, I hit the fact that there's an
external parse_escape function in utils.c, declared in defs.h that now
colides with the static cp-name-parse.y:parse_escape. They're mostly the
same, but this file it also buildable as a standalone program, so I just
renamed the one in cp-name-parser.y. The xfree issue is described in the
patch itself.

Phew, hope the attached (already commited) settles it. I made sure
that `make test-cp-name-parser' still links the standalone
test program.

--
Pedro Alves

[-- Attachment #2: fix_xfree.diff --]
[-- Type: text/x-diff, Size: 1798 bytes --]

2008-10-27 Pedro Alves <pedro@codesourcery.com>

* cp-name-parser.y: Include defs.h instead of config.h.
(parse_escape): Rename to ...
(cp_parse_escape): ... this.
(yylex): Update.
(xfree) [TEST_CPNAMES]: New.

---
gdb/cp-name-parser.y | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

Index: src/gdb/cp-name-parser.y
===================================================================
--- src.orig/gdb/cp-name-parser.y 2008-10-27 18:40:25.000000000 +0000
+++ src/gdb/cp-name-parser.y 2008-10-27 19:16:47.000000000 +0000
@@ -31,7 +31,7 @@ Boston, MA 02110-1301, USA. */

%{

-#include "config.h"
+#include "defs.h"

#include <stdio.h>
#include <stdlib.h>
@@ -1462,7 +1462,7 @@ c_parse_backslash (int host_char, int *t
after the zeros. A value of 0 does not mean end of string. */

static int
-parse_escape (const char **string_ptr)
+cp_parse_escape (const char **string_ptr)
{
int target_char;
int c = *(*string_ptr)++;
@@ -1483,7 +1483,7 @@ parse_escape (const char **string_ptr)
if (c == '?')
return 0177;
else if (c == '\\')
- target_char = parse_escape (string_ptr);
+ target_char = cp_parse_escape (string_ptr);
else
target_char = c;

@@ -1581,7 +1581,7 @@ yylex (void)
lexptr++;
c = *lexptr++;
if (c == '\\')
- c = parse_escape (&lexptr);
+ c = cp_parse_escape (&lexptr);
else if (c == '\'')
{
yyerror ("empty character constant");
@@ -2084,6 +2084,16 @@ trim_chars (char *lexptr, char **extra_c
return c;
}

+/* When this file is built as a standalone program, xmalloc comes from
+ libiberty --- in which case we have to provide xfree ourselves. */
+
+void
+xfree (void *ptr)
+{
+ if (ptr != NULL)
+ free (ptr);
+}
+
int
main (int argc, char **argv)
{