fixes for type-punning warnings in GCC 4.1

Alexandre Oliva
2005-12-19T19:21:54Z

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

Hi,

This patch fixes the type-punning warnings that the GCC 4.1 branch
issues when compiling the GDB code base.

The fixes for the .y files are safe, since the casts only perform a
non-identity conversion when code paths that are never taken. The two
other cases are situations in which the use of opaque types forces us
to violate the ISO C string aliasing rules; the one in tui/tui-data.c
is safe in that we never access the affected data structures with the
wrong type. In varobj.c that's much trickier to prove, but since it's
in a separate translation unit from the call, what we have now is no
safer than what we get after my patch.

Build-tested on amd64-linux-gnu. Ok to install?

[-- Attachment #2: gdb-type-punning.patch --]
[-- Type: text/x-patch, Size: 5001 bytes --]

Index: gdb/ChangeLog
2005-12-19 Alexandre Oliva <aoliva@redhat.com>

* c-exp.y (parse_number): Silence type-punning warnings.
* jv-exp.y (parse_number): Likewise.
* objc-exp.y (parse_number): Likewise.
* p-exp.y (parse_number): Likewise.
* tui/tui-data.c (source_windows): Likewise.
* varobj.c (free_variable): Likewise.

Index: gdb/c-exp.y
===================================================================
--- gdb/c-exp.y.orig 2005-12-19 15:07:39.000000000 -0200
+++ gdb/c-exp.y 2005-12-19 15:25:35.000000000 -0200
@@ -1082,9 +1082,9 @@
p[len] = 0; /* null-terminate the token */

if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
- num = sscanf (p, "%g%s", (float *) &putithere->typed_val_float.dval,s);
+ num = sscanf (p, "%g%s", (float *) (void *) &putithere->typed_val_float.dval,s);
else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
- num = sscanf (p, "%lg%s", (double *) &putithere->typed_val_float.dval,s);
+ num = sscanf (p, "%lg%s", (double *) (void *) &putithere->typed_val_float.dval,s);
else
{
#ifdef SCANF_HAS_LONG_DOUBLE
Index: gdb/jv-exp.y
===================================================================
--- gdb/jv-exp.y.orig 2005-12-19 15:07:41.000000000 -0200
+++ gdb/jv-exp.y 2005-12-19 15:24:53.000000000 -0200
@@ -714,9 +714,9 @@

p[len] = 0; /* null-terminate the token */
if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
- num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval, &c);
+ num = sscanf (p, "%g%c", (float *) (void *) &putithere->typed_val_float.dval, &c);
else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
- num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval, &c);
+ num = sscanf (p, "%lg%c", (double *) (void *) &putithere->typed_val_float.dval, &c);
else
{
#ifdef SCANF_HAS_LONG_DOUBLE
Index: gdb/objc-exp.y
===================================================================
--- gdb/objc-exp.y.orig 2005-12-19 15:07:45.000000000 -0200
+++ gdb/objc-exp.y 2005-12-19 15:24:53.000000000 -0200
@@ -1026,9 +1026,9 @@
/* It's a float since it contains a point or an exponent. */

if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
- sscanf (p, "%g", (float *)&putithere->typed_val_float.dval);
+ sscanf (p, "%g", (float *) (void *) &putithere->typed_val_float.dval);
else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
- sscanf (p, "%lg", (double *)&putithere->typed_val_float.dval);
+ sscanf (p, "%lg", (double *) (void *) &putithere->typed_val_float.dval);
else
{
#ifdef PRINTF_HAS_LONG_DOUBLE
Index: gdb/p-exp.y
===================================================================
--- gdb/p-exp.y.orig 2005-12-19 15:07:45.000000000 -0200
+++ gdb/p-exp.y 2005-12-19 15:24:53.000000000 -0200
@@ -800,9 +800,9 @@

p[len] = 0; /* null-terminate the token */
if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
- num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
+ num = sscanf (p, "%g%c", (float *) (void *) &putithere->typed_val_float.dval,&c);
else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
- num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
+ num = sscanf (p, "%lg%c", (double *) (void *) &putithere->typed_val_float.dval,&c);
else
{
#ifdef SCANF_HAS_LONG_DOUBLE
Index: gdb/tui/tui-data.c
===================================================================
--- gdb/tui/tui-data.c.orig 2005-12-19 15:07:52.000000000 -0200
+++ gdb/tui/tui-data.c 2005-12-19 15:24:53.000000000 -0200
@@ -44,7 +44,13 @@
static struct tui_gen_win_info _locator;
static struct tui_gen_win_info exec_info[2];
static struct tui_win_info * src_win_list[2];
-static struct tui_list source_windows = {(void **) src_win_list, 0};
+/* The intermediate cast to void* silences a type-punning warning
+ issued by GCC. The use appears to be safe, since we always access
+ source_windows.list with type void**, and whenever we access one of
+ the list members, we cast it to struct tui_win_info*. The
+ interface of struct tui_list should probably be redesigned with
+ less type opacity to avoid type punning. -aoliva */
+static struct tui_list source_windows = {(void **) (void *) src_win_list, 0};
static int default_tab_len = DEFAULT_TAB_LEN;
static struct tui_win_info * win_with_focus = (struct tui_win_info *) NULL;
static struct tui_layout_def layout_def =
Index: gdb/varobj.c
===================================================================
--- gdb/varobj.c.orig 2005-12-19 15:07:50.000000000 -0200
+++ gdb/varobj.c 2005-12-19 15:24:53.000000000 -0200
@@ -1374,7 +1374,7 @@
/* Free the expression if this is a root variable. */
if (var->root->rootvar == var)
{
- free_current_contents ((char **) &var->root->exp);
+ free_current_contents (&var->root->exp);
xfree (var->root);
}

[-- Attachment #3: Type: text/plain, Size: 188 bytes --]

--
Alexandre Oliva http://www.lsd.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist oliva@{lsd.ic.unicamp.br, gnu.org}