public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
@ 2024-02-16  9:14 Jakub Jelinek
  2024-02-16 15:48 ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Jakub Jelinek @ 2024-02-16  9:14 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

DWARF5 added DW_AT_export_symbols both for use on inline namespaces (where
we emit it), but also on anonymous unions/structs (and we didn't emit that
attribute there).
The following patch fixes it.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-02-16  Jakub Jelinek  <jakub@redhat.com>

	PR debug/113918
	* dwarf2out.cc (gen_field_die): Emit DW_AT_export_symbols
	on anonymous unions or structs for -gdwarf-5 or -gno-strict-dwarf.

	* c-c++-common/dwarf2/pr113918.c: New test.

--- gcc/dwarf2out.cc.jj	2024-02-10 11:25:09.836476263 +0100
+++ gcc/dwarf2out.cc	2024-02-15 13:38:14.485675460 +0100
@@ -25153,6 +25153,20 @@ gen_field_die (tree decl, struct vlr_con
 
   add_accessibility_attribute (decl_die, decl);
 
+  /* Add DW_AT_export_symbols to anonymous unions or structs.  */
+  if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
+    if (tree type = member_declared_type (decl))
+      {
+	tree type_id = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
+	if (RECORD_OR_UNION_TYPE_P (type)
+	    && (type_id == NULL_TREE || IDENTIFIER_ANON_P (type_id)))
+	{
+	  dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
+	  if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
+	    add_AT_flag (type_die, DW_AT_export_symbols, 1);
+	}
+    }
+
   /* Equate decl number to die, so that we can look up this decl later on.  */
   equate_decl_number_to_die (decl, decl_die);
 }
--- gcc/testsuite/c-c++-common/dwarf2/pr113918.c.jj	2024-02-15 13:26:53.935984554 +0100
+++ gcc/testsuite/c-c++-common/dwarf2/pr113918.c	2024-02-15 13:26:48.773055181 +0100
@@ -0,0 +1,33 @@
+/* PR debug/113918 */
+/* { dg-do compile } */
+/* { dg-options "-gdwarf-5 -dA -fno-merge-debug-strings" } */
+
+struct S {
+  union {
+    int i;
+    long long j;
+  };
+  struct {
+    int k;
+    long long l;
+  };
+  union {
+    int m;
+    long long n;
+  } u;
+  struct {
+    int o;
+    long long p;
+  } v;
+} s;
+
+int
+main ()
+{
+  s.i = 1;
+  s.k = 2;
+  s.u.m = 3;
+  s.v.o = 4;
+}
+
+/* { dg-final { scan-assembler-times "DW_AT_export_symbols" 4 } } */

	Jakub


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
  2024-02-16  9:14 [PATCH] dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918] Jakub Jelinek
@ 2024-02-16 15:48 ` Jason Merrill
  2024-02-16 15:52   ` Jakub Jelinek
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Merrill @ 2024-02-16 15:48 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 2/16/24 04:14, Jakub Jelinek wrote:
> Hi!
> 
> DWARF5 added DW_AT_export_symbols both for use on inline namespaces (where
> we emit it), but also on anonymous unions/structs (and we didn't emit that
> attribute there).
> The following patch fixes it.

Should this involve cp_decl_dwarf_attribute like the namespace handling?

> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2024-02-16  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR debug/113918
> 	* dwarf2out.cc (gen_field_die): Emit DW_AT_export_symbols
> 	on anonymous unions or structs for -gdwarf-5 or -gno-strict-dwarf.
> 
> 	* c-c++-common/dwarf2/pr113918.c: New test.
> 
> --- gcc/dwarf2out.cc.jj	2024-02-10 11:25:09.836476263 +0100
> +++ gcc/dwarf2out.cc	2024-02-15 13:38:14.485675460 +0100
> @@ -25153,6 +25153,20 @@ gen_field_die (tree decl, struct vlr_con
>   
>     add_accessibility_attribute (decl_die, decl);
>   
> +  /* Add DW_AT_export_symbols to anonymous unions or structs.  */
> +  if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
> +    if (tree type = member_declared_type (decl))
> +      {
> +	tree type_id = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
> +	if (RECORD_OR_UNION_TYPE_P (type)
> +	    && (type_id == NULL_TREE || IDENTIFIER_ANON_P (type_id)))
> +	{
> +	  dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
> +	  if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
> +	    add_AT_flag (type_die, DW_AT_export_symbols, 1);
> +	}
> +    }
> +
>     /* Equate decl number to die, so that we can look up this decl later on.  */
>     equate_decl_number_to_die (decl, decl_die);
>   }
> --- gcc/testsuite/c-c++-common/dwarf2/pr113918.c.jj	2024-02-15 13:26:53.935984554 +0100
> +++ gcc/testsuite/c-c++-common/dwarf2/pr113918.c	2024-02-15 13:26:48.773055181 +0100
> @@ -0,0 +1,33 @@
> +/* PR debug/113918 */
> +/* { dg-do compile } */
> +/* { dg-options "-gdwarf-5 -dA -fno-merge-debug-strings" } */
> +
> +struct S {
> +  union {
> +    int i;
> +    long long j;
> +  };
> +  struct {
> +    int k;
> +    long long l;
> +  };
> +  union {
> +    int m;
> +    long long n;
> +  } u;
> +  struct {
> +    int o;
> +    long long p;
> +  } v;
> +} s;
> +
> +int
> +main ()
> +{
> +  s.i = 1;
> +  s.k = 2;
> +  s.u.m = 3;
> +  s.v.o = 4;
> +}
> +
> +/* { dg-final { scan-assembler-times "DW_AT_export_symbols" 4 } } */
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
  2024-02-16 15:48 ` Jason Merrill
@ 2024-02-16 15:52   ` Jakub Jelinek
  2024-02-16 19:52     ` [PATCH] dwarf2out, v2: " Jakub Jelinek
  0 siblings, 1 reply; 7+ messages in thread
From: Jakub Jelinek @ 2024-02-16 15:52 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Fri, Feb 16, 2024 at 10:48:28AM -0500, Jason Merrill wrote:
> On 2/16/24 04:14, Jakub Jelinek wrote:
> > DWARF5 added DW_AT_export_symbols both for use on inline namespaces (where
> > we emit it), but also on anonymous unions/structs (and we didn't emit that
> > attribute there).
> > The following patch fixes it.
> 
> Should this involve cp_decl_dwarf_attribute like the namespace handling?

I wrote it in dwarf2out.cc because the same thing needs to be done for C and
C++ (admittedly dunno if other languages have something similar).

Sure, it could be done in cp_decl_dwarf_attribute too but then it has to be
done in c_decl_dwarf_attribute which doesn't exist.  Though, it is slightly
complicated by DW_AT_export_symbols not actually going on the DW_TAG_member
die but on the DW_TAG_{structure,class,union}_type which DW_TAG_member uses
as its DW_AT_type.  But in order to ask the langhook, we likely need to pass
the FIELD_DECL and not the type...

	Jakub


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] dwarf2out, v2: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
  2024-02-16 15:52   ` Jakub Jelinek
@ 2024-02-16 19:52     ` Jakub Jelinek
  2024-02-16 20:40       ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Jakub Jelinek @ 2024-02-16 19:52 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Fri, Feb 16, 2024 at 04:52:20PM +0100, Jakub Jelinek wrote:
> On Fri, Feb 16, 2024 at 10:48:28AM -0500, Jason Merrill wrote:
> > On 2/16/24 04:14, Jakub Jelinek wrote:
> > > DWARF5 added DW_AT_export_symbols both for use on inline namespaces (where
> > > we emit it), but also on anonymous unions/structs (and we didn't emit that
> > > attribute there).
> > > The following patch fixes it.
> > 
> > Should this involve cp_decl_dwarf_attribute like the namespace handling?
> 
> I wrote it in dwarf2out.cc because the same thing needs to be done for C and
> C++ (admittedly dunno if other languages have something similar).
> 
> Sure, it could be done in cp_decl_dwarf_attribute too but then it has to be
> done in c_decl_dwarf_attribute which doesn't exist.  Though, it is slightly
> complicated by DW_AT_export_symbols not actually going on the DW_TAG_member
> die but on the DW_TAG_{structure,class,union}_type which DW_TAG_member uses
> as its DW_AT_type.  But in order to ask the langhook, we likely need to pass
> the FIELD_DECL and not the type...

Anyway, here is a version of the patch that uses the langhook for it.
So that the langhook isn't totally abused and for performance reasons too
I've kept the DECL_NAME == NULL check on the dwarf2out.cc side and am
calling it on the type rather than decl, because type is where the attribute
should go.

2024-02-16  Jakub Jelinek  <jakub@redhat.com>

	PR debug/113918
gcc/
	* dwarf2out.cc (gen_field_die): Emit DW_AT_export_symbols
	on anonymous unions or structs for -gdwarf-5 or -gno-strict-dwarf.
gcc/c/
	* c-tree.h (c_type_dwarf_attribute): Declare.
	* c-objc-common.h (LANG_HOOKS_TYPE_DWARF_ATTRIBUTE): Redefine.
	* c-objc-common.cc: Include dwarf2.h.
	(c_type_dwarf_attribute): New function.
gcc/cp/
	* cp-objcp-common.cc (cp_type_dwarf_attribute): Return 1
	for DW_AT_export_symbols on anonymous structs or unions.
gcc/testsuite/
	* c-c++-common/dwarf2/pr113918.c: New test.

--- gcc/dwarf2out.cc.jj	2024-02-15 13:54:29.284358101 +0100
+++ gcc/dwarf2out.cc	2024-02-16 20:38:19.718841259 +0100
@@ -25153,6 +25153,17 @@ gen_field_die (tree decl, struct vlr_con
 
   add_accessibility_attribute (decl_die, decl);
 
+  /* Add DW_AT_export_symbols to anonymous unions or structs.  */
+  if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
+    if (tree type = member_declared_type (decl))
+      if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
+						 DW_AT_export_symbols) != -1)
+	{
+	  dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
+	  if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
+	    add_AT_flag (type_die, DW_AT_export_symbols, 1);
+	}
+
   /* Equate decl number to die, so that we can look up this decl later on.  */
   equate_decl_number_to_die (decl, decl_die);
 }
--- gcc/c/c-tree.h.jj	2024-01-31 10:46:35.164761720 +0100
+++ gcc/c/c-tree.h	2024-02-16 20:43:45.993372908 +0100
@@ -731,6 +731,7 @@ extern bool c_warn_unused_global_decl (c
 extern void c_initialize_diagnostics (diagnostic_context *);
 extern bool c_var_mod_p (tree x, tree fn);
 extern alias_set_type c_get_alias_set (tree);
+extern int c_type_dwarf_attribute (const_tree, int);
 
 /* in c-typeck.cc */
 extern int in_alignof;
--- gcc/c/c-objc-common.h.jj	2024-01-03 12:06:52.973862999 +0100
+++ gcc/c/c-objc-common.h	2024-02-16 20:42:21.073535465 +0100
@@ -119,6 +119,9 @@ static const scoped_attribute_specs *con
 #undef LANG_HOOKS_GIMPLIFY_EXPR
 #define LANG_HOOKS_GIMPLIFY_EXPR c_gimplify_expr
 
+#undef LANG_HOOKS_TYPE_DWARF_ATTRIBUTE
+#define LANG_HOOKS_TYPE_DWARF_ATTRIBUTE c_type_dwarf_attribute
+
 #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING
 #define LANG_HOOKS_OMP_PREDETERMINED_SHARING c_omp_predetermined_sharing
 
--- gcc/c/c-objc-common.cc.jj	2024-01-03 12:06:53.213859637 +0100
+++ gcc/c/c-objc-common.cc	2024-02-16 20:45:24.649022305 +0100
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.
 #include "gcc-rich-location.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "dwarf2.h"
 
 static bool c_tree_printer (pretty_printer *, text_info *, const char *,
 			    int, bool, bool, bool, bool *, const char **);
@@ -446,3 +447,25 @@ instantiation_dependent_expression_p (tr
 {
   return false;
 }
+
+/* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
+   value otherwise.  */
+int
+c_type_dwarf_attribute (const_tree type, int attr)
+{
+  if (type == NULL_TREE)
+    return -1;
+
+  switch (attr)
+    {
+    case DW_AT_export_symbols:
+      if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
+	return 1;
+      break;
+
+    default:
+      break;
+    }
+
+  return -1;
+}
--- gcc/cp/cp-objcp-common.cc.jj	2024-02-13 12:50:21.666846296 +0100
+++ gcc/cp/cp-objcp-common.cc	2024-02-16 20:40:51.374763528 +0100
@@ -410,6 +410,15 @@ cp_type_dwarf_attribute (const_tree type
 	return 1;
       break;
 
+    case DW_AT_export_symbols:
+      if (RECORD_OR_UNION_TYPE_P (type))
+	{
+	  tree type_id = TYPE_IDENTIFIER (type);
+	  if (type_id == NULL_TREE || IDENTIFIER_ANON_P (type_id))
+	    return 1;
+	}
+      break;
+
     default:
       break;
     }
--- gcc/testsuite/c-c++-common/dwarf2/pr113918.c.jj	2024-02-16 20:27:13.996961811 +0100
+++ gcc/testsuite/c-c++-common/dwarf2/pr113918.c	2024-02-16 20:27:13.996961811 +0100
@@ -0,0 +1,33 @@
+/* PR debug/113918 */
+/* { dg-do compile } */
+/* { dg-options "-gdwarf-5 -dA -fno-merge-debug-strings" } */
+
+struct S {
+  union {
+    int i;
+    long long j;
+  };
+  struct {
+    int k;
+    long long l;
+  };
+  union {
+    int m;
+    long long n;
+  } u;
+  struct {
+    int o;
+    long long p;
+  } v;
+} s;
+
+int
+main ()
+{
+  s.i = 1;
+  s.k = 2;
+  s.u.m = 3;
+  s.v.o = 4;
+}
+
+/* { dg-final { scan-assembler-times "DW_AT_export_symbols" 4 } } */


	Jakub


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] dwarf2out, v2: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
  2024-02-16 19:52     ` [PATCH] dwarf2out, v2: " Jakub Jelinek
@ 2024-02-16 20:40       ` Jason Merrill
  2024-02-16 21:06         ` [PATCH] dwarf2out, v3: " Jakub Jelinek
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Merrill @ 2024-02-16 20:40 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 2/16/24 14:52, Jakub Jelinek wrote:
> On Fri, Feb 16, 2024 at 04:52:20PM +0100, Jakub Jelinek wrote:
>> On Fri, Feb 16, 2024 at 10:48:28AM -0500, Jason Merrill wrote:
>>> On 2/16/24 04:14, Jakub Jelinek wrote:
>>>> DWARF5 added DW_AT_export_symbols both for use on inline namespaces (where
>>>> we emit it), but also on anonymous unions/structs (and we didn't emit that
>>>> attribute there).
>>>> The following patch fixes it.
>>>
>>> Should this involve cp_decl_dwarf_attribute like the namespace handling?
>>
>> I wrote it in dwarf2out.cc because the same thing needs to be done for C and
>> C++ (admittedly dunno if other languages have something similar).
>>
>> Sure, it could be done in cp_decl_dwarf_attribute too but then it has to be
>> done in c_decl_dwarf_attribute which doesn't exist.  Though, it is slightly
>> complicated by DW_AT_export_symbols not actually going on the DW_TAG_member
>> die but on the DW_TAG_{structure,class,union}_type which DW_TAG_member uses
>> as its DW_AT_type.  But in order to ask the langhook, we likely need to pass
>> the FIELD_DECL and not the type...
> 
> Anyway, here is a version of the patch that uses the langhook for it.
> So that the langhook isn't totally abused and for performance reasons too
> I've kept the DECL_NAME == NULL check on the dwarf2out.cc side and am
> calling it on the type rather than decl, because type is where the attribute
> should go.
> 
> 2024-02-16  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR debug/113918
> gcc/
> 	* dwarf2out.cc (gen_field_die): Emit DW_AT_export_symbols
> 	on anonymous unions or structs for -gdwarf-5 or -gno-strict-dwarf.
> gcc/c/
> 	* c-tree.h (c_type_dwarf_attribute): Declare.
> 	* c-objc-common.h (LANG_HOOKS_TYPE_DWARF_ATTRIBUTE): Redefine.
> 	* c-objc-common.cc: Include dwarf2.h.
> 	(c_type_dwarf_attribute): New function.
> gcc/cp/
> 	* cp-objcp-common.cc (cp_type_dwarf_attribute): Return 1
> 	for DW_AT_export_symbols on anonymous structs or unions.
> gcc/testsuite/
> 	* c-c++-common/dwarf2/pr113918.c: New test.
> 
> --- gcc/dwarf2out.cc.jj	2024-02-15 13:54:29.284358101 +0100
> +++ gcc/dwarf2out.cc	2024-02-16 20:38:19.718841259 +0100
> @@ -25153,6 +25153,17 @@ gen_field_die (tree decl, struct vlr_con
>   
>     add_accessibility_attribute (decl_die, decl);
>   
> +  /* Add DW_AT_export_symbols to anonymous unions or structs.  */
> +  if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
> +    if (tree type = member_declared_type (decl))
> +      if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
> +						 DW_AT_export_symbols) != -1)
> +	{
> +	  dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
> +	  if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
> +	    add_AT_flag (type_die, DW_AT_export_symbols, 1);
> +	}
> +
>     /* Equate decl number to die, so that we can look up this decl later on.  */
>     equate_decl_number_to_die (decl, decl_die);
>   }
> --- gcc/c/c-tree.h.jj	2024-01-31 10:46:35.164761720 +0100
> +++ gcc/c/c-tree.h	2024-02-16 20:43:45.993372908 +0100
> @@ -731,6 +731,7 @@ extern bool c_warn_unused_global_decl (c
>   extern void c_initialize_diagnostics (diagnostic_context *);
>   extern bool c_var_mod_p (tree x, tree fn);
>   extern alias_set_type c_get_alias_set (tree);
> +extern int c_type_dwarf_attribute (const_tree, int);
>   
>   /* in c-typeck.cc */
>   extern int in_alignof;
> --- gcc/c/c-objc-common.h.jj	2024-01-03 12:06:52.973862999 +0100
> +++ gcc/c/c-objc-common.h	2024-02-16 20:42:21.073535465 +0100
> @@ -119,6 +119,9 @@ static const scoped_attribute_specs *con
>   #undef LANG_HOOKS_GIMPLIFY_EXPR
>   #define LANG_HOOKS_GIMPLIFY_EXPR c_gimplify_expr
>   
> +#undef LANG_HOOKS_TYPE_DWARF_ATTRIBUTE
> +#define LANG_HOOKS_TYPE_DWARF_ATTRIBUTE c_type_dwarf_attribute
> +
>   #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING
>   #define LANG_HOOKS_OMP_PREDETERMINED_SHARING c_omp_predetermined_sharing
>   
> --- gcc/c/c-objc-common.cc.jj	2024-01-03 12:06:53.213859637 +0100
> +++ gcc/c/c-objc-common.cc	2024-02-16 20:45:24.649022305 +0100
> @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.
>   #include "gcc-rich-location.h"
>   #include "stringpool.h"
>   #include "attribs.h"
> +#include "dwarf2.h"
>   
>   static bool c_tree_printer (pretty_printer *, text_info *, const char *,
>   			    int, bool, bool, bool, bool *, const char **);
> @@ -446,3 +447,25 @@ instantiation_dependent_expression_p (tr
>   {
>     return false;
>   }
> +
> +/* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
> +   value otherwise.  */
> +int
> +c_type_dwarf_attribute (const_tree type, int attr)
> +{
> +  if (type == NULL_TREE)
> +    return -1;
> +
> +  switch (attr)
> +    {
> +    case DW_AT_export_symbols:
> +      if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
> +	return 1;
> +      break;
> +
> +    default:
> +      break;
> +    }
> +
> +  return -1;
> +}
> --- gcc/cp/cp-objcp-common.cc.jj	2024-02-13 12:50:21.666846296 +0100
> +++ gcc/cp/cp-objcp-common.cc	2024-02-16 20:40:51.374763528 +0100
> @@ -410,6 +410,15 @@ cp_type_dwarf_attribute (const_tree type
>   	return 1;
>         break;
>   
> +    case DW_AT_export_symbols:

For C++ this can use ANON_AGGR_TYPE_P, so it doesn't need to involve the 
FIELD_DECL at all.  But I suppose the C front-end doesn't have a similar 
flag?

> +      if (RECORD_OR_UNION_TYPE_P (type))
> +	{
> +	  tree type_id = TYPE_IDENTIFIER (type);
> +	  if (type_id == NULL_TREE || IDENTIFIER_ANON_P (type_id))
> +	    return 1;
> +	}
> +      break;
> +
>       default:
>         break;
>       }
> --- gcc/testsuite/c-c++-common/dwarf2/pr113918.c.jj	2024-02-16 20:27:13.996961811 +0100
> +++ gcc/testsuite/c-c++-common/dwarf2/pr113918.c	2024-02-16 20:27:13.996961811 +0100
> @@ -0,0 +1,33 @@
> +/* PR debug/113918 */
> +/* { dg-do compile } */
> +/* { dg-options "-gdwarf-5 -dA -fno-merge-debug-strings" } */
> +
> +struct S {
> +  union {
> +    int i;
> +    long long j;
> +  };
> +  struct {
> +    int k;
> +    long long l;
> +  };
> +  union {
> +    int m;
> +    long long n;
> +  } u;
> +  struct {
> +    int o;
> +    long long p;
> +  } v;
> +} s;
> +
> +int
> +main ()
> +{
> +  s.i = 1;
> +  s.k = 2;
> +  s.u.m = 3;
> +  s.v.o = 4;
> +}
> +
> +/* { dg-final { scan-assembler-times "DW_AT_export_symbols" 4 } } */
> 
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] dwarf2out, v3: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
  2024-02-16 20:40       ` Jason Merrill
@ 2024-02-16 21:06         ` Jakub Jelinek
  2024-03-06 23:45           ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Jakub Jelinek @ 2024-02-16 21:06 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Fri, Feb 16, 2024 at 03:40:39PM -0500, Jason Merrill wrote:
> > --- gcc/cp/cp-objcp-common.cc.jj	2024-02-13 12:50:21.666846296 +0100
> > +++ gcc/cp/cp-objcp-common.cc	2024-02-16 20:40:51.374763528 +0100
> > @@ -410,6 +410,15 @@ cp_type_dwarf_attribute (const_tree type
> >   	return 1;
> >         break;
> > +    case DW_AT_export_symbols:
> 
> For C++ this can use ANON_AGGR_TYPE_P, so it doesn't need to involve the
> FIELD_DECL at all.  But I suppose the C front-end doesn't have a similar
> flag?

Yes, using ANON_AGGR_TYPE_P there works for C++, but C doesn't have anything
like that.  All it uses is DECL_NAME == NULL on FIELD_DECL +
RECORD_OR_UNION_TYPE_P on its type to determine anon struct/union.

The patch below has updated cp_type_dwarf_attribute, otherwise the same as
before.

2024-02-16  Jakub Jelinek  <jakub@redhat.com>

	PR debug/113918
gcc/
	* dwarf2out.cc (gen_field_die): Emit DW_AT_export_symbols
	on anonymous unions or structs for -gdwarf-5 or -gno-strict-dwarf.
gcc/c/
	* c-tree.h (c_type_dwarf_attribute): Declare.
	* c-objc-common.h (LANG_HOOKS_TYPE_DWARF_ATTRIBUTE): Redefine.
	* c-objc-common.cc: Include dwarf2.h.
	(c_type_dwarf_attribute): New function.
gcc/cp/
	* cp-objcp-common.cc (cp_type_dwarf_attribute): Return 1
	for DW_AT_export_symbols on anonymous structs or unions.
gcc/testsuite/
	* c-c++-common/dwarf2/pr113918.c: New test.

--- gcc/dwarf2out.cc.jj	2024-02-15 13:54:29.284358101 +0100
+++ gcc/dwarf2out.cc	2024-02-16 20:38:19.718841259 +0100
@@ -25153,6 +25153,17 @@ gen_field_die (tree decl, struct vlr_con
 
   add_accessibility_attribute (decl_die, decl);
 
+  /* Add DW_AT_export_symbols to anonymous unions or structs.  */
+  if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
+    if (tree type = member_declared_type (decl))
+      if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
+						 DW_AT_export_symbols) != -1)
+	{
+	  dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
+	  if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
+	    add_AT_flag (type_die, DW_AT_export_symbols, 1);
+	}
+
   /* Equate decl number to die, so that we can look up this decl later on.  */
   equate_decl_number_to_die (decl, decl_die);
 }
--- gcc/c/c-tree.h.jj	2024-01-31 10:46:35.164761720 +0100
+++ gcc/c/c-tree.h	2024-02-16 20:43:45.993372908 +0100
@@ -731,6 +731,7 @@ extern bool c_warn_unused_global_decl (c
 extern void c_initialize_diagnostics (diagnostic_context *);
 extern bool c_var_mod_p (tree x, tree fn);
 extern alias_set_type c_get_alias_set (tree);
+extern int c_type_dwarf_attribute (const_tree, int);
 
 /* in c-typeck.cc */
 extern int in_alignof;
--- gcc/c/c-objc-common.h.jj	2024-01-03 12:06:52.973862999 +0100
+++ gcc/c/c-objc-common.h	2024-02-16 20:42:21.073535465 +0100
@@ -119,6 +119,9 @@ static const scoped_attribute_specs *con
 #undef LANG_HOOKS_GIMPLIFY_EXPR
 #define LANG_HOOKS_GIMPLIFY_EXPR c_gimplify_expr
 
+#undef LANG_HOOKS_TYPE_DWARF_ATTRIBUTE
+#define LANG_HOOKS_TYPE_DWARF_ATTRIBUTE c_type_dwarf_attribute
+
 #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING
 #define LANG_HOOKS_OMP_PREDETERMINED_SHARING c_omp_predetermined_sharing
 
--- gcc/c/c-objc-common.cc.jj	2024-01-03 12:06:53.213859637 +0100
+++ gcc/c/c-objc-common.cc	2024-02-16 20:45:24.649022305 +0100
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.
 #include "gcc-rich-location.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "dwarf2.h"
 
 static bool c_tree_printer (pretty_printer *, text_info *, const char *,
 			    int, bool, bool, bool, bool *, const char **);
@@ -446,3 +447,25 @@ instantiation_dependent_expression_p (tr
 {
   return false;
 }
+
+/* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
+   value otherwise.  */
+int
+c_type_dwarf_attribute (const_tree type, int attr)
+{
+  if (type == NULL_TREE)
+    return -1;
+
+  switch (attr)
+    {
+    case DW_AT_export_symbols:
+      if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
+	return 1;
+      break;
+
+    default:
+      break;
+    }
+
+  return -1;
+}
--- gcc/cp/cp-objcp-common.cc.jj	2024-02-13 12:50:21.666846296 +0100
+++ gcc/cp/cp-objcp-common.cc	2024-02-16 21:48:33.880458318 +0100
@@ -410,6 +410,11 @@ cp_type_dwarf_attribute (const_tree type
 	return 1;
       break;
 
+    case DW_AT_export_symbols:
+      if (ANON_AGGR_TYPE_P (type))
+	return 1;
+      break;
+
     default:
       break;
     }
--- gcc/testsuite/c-c++-common/dwarf2/pr113918.c.jj	2024-02-16 20:27:13.996961811 +0100
+++ gcc/testsuite/c-c++-common/dwarf2/pr113918.c	2024-02-16 20:27:13.996961811 +0100
@@ -0,0 +1,33 @@
+/* PR debug/113918 */
+/* { dg-do compile } */
+/* { dg-options "-gdwarf-5 -dA -fno-merge-debug-strings" } */
+
+struct S {
+  union {
+    int i;
+    long long j;
+  };
+  struct {
+    int k;
+    long long l;
+  };
+  union {
+    int m;
+    long long n;
+  } u;
+  struct {
+    int o;
+    long long p;
+  } v;
+} s;
+
+int
+main ()
+{
+  s.i = 1;
+  s.k = 2;
+  s.u.m = 3;
+  s.v.o = 4;
+}
+
+/* { dg-final { scan-assembler-times "DW_AT_export_symbols" 4 } } */


	Jakub


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] dwarf2out, v3: Emit DW_AT_export_symbols on anon unions/structs [PR113918]
  2024-02-16 21:06         ` [PATCH] dwarf2out, v3: " Jakub Jelinek
@ 2024-03-06 23:45           ` Jason Merrill
  0 siblings, 0 replies; 7+ messages in thread
From: Jason Merrill @ 2024-03-06 23:45 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 2/16/24 16:06, Jakub Jelinek wrote:
> On Fri, Feb 16, 2024 at 03:40:39PM -0500, Jason Merrill wrote:
>>> --- gcc/cp/cp-objcp-common.cc.jj	2024-02-13 12:50:21.666846296 +0100
>>> +++ gcc/cp/cp-objcp-common.cc	2024-02-16 20:40:51.374763528 +0100
>>> @@ -410,6 +410,15 @@ cp_type_dwarf_attribute (const_tree type
>>>    	return 1;
>>>          break;
>>> +    case DW_AT_export_symbols:
>>
>> For C++ this can use ANON_AGGR_TYPE_P, so it doesn't need to involve the
>> FIELD_DECL at all.  But I suppose the C front-end doesn't have a similar
>> flag?
> 
> Yes, using ANON_AGGR_TYPE_P there works for C++, but C doesn't have anything
> like that.  All it uses is DECL_NAME == NULL on FIELD_DECL +
> RECORD_OR_UNION_TYPE_P on its type to determine anon struct/union.
> 
> The patch below has updated cp_type_dwarf_attribute, otherwise the same as
> before.

OK.

> 2024-02-16  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR debug/113918
> gcc/
> 	* dwarf2out.cc (gen_field_die): Emit DW_AT_export_symbols
> 	on anonymous unions or structs for -gdwarf-5 or -gno-strict-dwarf.
> gcc/c/
> 	* c-tree.h (c_type_dwarf_attribute): Declare.
> 	* c-objc-common.h (LANG_HOOKS_TYPE_DWARF_ATTRIBUTE): Redefine.
> 	* c-objc-common.cc: Include dwarf2.h.
> 	(c_type_dwarf_attribute): New function.
> gcc/cp/
> 	* cp-objcp-common.cc (cp_type_dwarf_attribute): Return 1
> 	for DW_AT_export_symbols on anonymous structs or unions.
> gcc/testsuite/
> 	* c-c++-common/dwarf2/pr113918.c: New test.
> 
> --- gcc/dwarf2out.cc.jj	2024-02-15 13:54:29.284358101 +0100
> +++ gcc/dwarf2out.cc	2024-02-16 20:38:19.718841259 +0100
> @@ -25153,6 +25153,17 @@ gen_field_die (tree decl, struct vlr_con
>   
>     add_accessibility_attribute (decl_die, decl);
>   
> +  /* Add DW_AT_export_symbols to anonymous unions or structs.  */
> +  if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
> +    if (tree type = member_declared_type (decl))
> +      if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
> +						 DW_AT_export_symbols) != -1)
> +	{
> +	  dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
> +	  if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
> +	    add_AT_flag (type_die, DW_AT_export_symbols, 1);
> +	}
> +
>     /* Equate decl number to die, so that we can look up this decl later on.  */
>     equate_decl_number_to_die (decl, decl_die);
>   }
> --- gcc/c/c-tree.h.jj	2024-01-31 10:46:35.164761720 +0100
> +++ gcc/c/c-tree.h	2024-02-16 20:43:45.993372908 +0100
> @@ -731,6 +731,7 @@ extern bool c_warn_unused_global_decl (c
>   extern void c_initialize_diagnostics (diagnostic_context *);
>   extern bool c_var_mod_p (tree x, tree fn);
>   extern alias_set_type c_get_alias_set (tree);
> +extern int c_type_dwarf_attribute (const_tree, int);
>   
>   /* in c-typeck.cc */
>   extern int in_alignof;
> --- gcc/c/c-objc-common.h.jj	2024-01-03 12:06:52.973862999 +0100
> +++ gcc/c/c-objc-common.h	2024-02-16 20:42:21.073535465 +0100
> @@ -119,6 +119,9 @@ static const scoped_attribute_specs *con
>   #undef LANG_HOOKS_GIMPLIFY_EXPR
>   #define LANG_HOOKS_GIMPLIFY_EXPR c_gimplify_expr
>   
> +#undef LANG_HOOKS_TYPE_DWARF_ATTRIBUTE
> +#define LANG_HOOKS_TYPE_DWARF_ATTRIBUTE c_type_dwarf_attribute
> +
>   #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING
>   #define LANG_HOOKS_OMP_PREDETERMINED_SHARING c_omp_predetermined_sharing
>   
> --- gcc/c/c-objc-common.cc.jj	2024-01-03 12:06:53.213859637 +0100
> +++ gcc/c/c-objc-common.cc	2024-02-16 20:45:24.649022305 +0100
> @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.
>   #include "gcc-rich-location.h"
>   #include "stringpool.h"
>   #include "attribs.h"
> +#include "dwarf2.h"
>   
>   static bool c_tree_printer (pretty_printer *, text_info *, const char *,
>   			    int, bool, bool, bool, bool *, const char **);
> @@ -446,3 +447,25 @@ instantiation_dependent_expression_p (tr
>   {
>     return false;
>   }
> +
> +/* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
> +   value otherwise.  */
> +int
> +c_type_dwarf_attribute (const_tree type, int attr)
> +{
> +  if (type == NULL_TREE)
> +    return -1;
> +
> +  switch (attr)
> +    {
> +    case DW_AT_export_symbols:
> +      if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
> +	return 1;
> +      break;
> +
> +    default:
> +      break;
> +    }
> +
> +  return -1;
> +}
> --- gcc/cp/cp-objcp-common.cc.jj	2024-02-13 12:50:21.666846296 +0100
> +++ gcc/cp/cp-objcp-common.cc	2024-02-16 21:48:33.880458318 +0100
> @@ -410,6 +410,11 @@ cp_type_dwarf_attribute (const_tree type
>   	return 1;
>         break;
>   
> +    case DW_AT_export_symbols:
> +      if (ANON_AGGR_TYPE_P (type))
> +	return 1;
> +      break;
> +
>       default:
>         break;
>       }
> --- gcc/testsuite/c-c++-common/dwarf2/pr113918.c.jj	2024-02-16 20:27:13.996961811 +0100
> +++ gcc/testsuite/c-c++-common/dwarf2/pr113918.c	2024-02-16 20:27:13.996961811 +0100
> @@ -0,0 +1,33 @@
> +/* PR debug/113918 */
> +/* { dg-do compile } */
> +/* { dg-options "-gdwarf-5 -dA -fno-merge-debug-strings" } */
> +
> +struct S {
> +  union {
> +    int i;
> +    long long j;
> +  };
> +  struct {
> +    int k;
> +    long long l;
> +  };
> +  union {
> +    int m;
> +    long long n;
> +  } u;
> +  struct {
> +    int o;
> +    long long p;
> +  } v;
> +} s;
> +
> +int
> +main ()
> +{
> +  s.i = 1;
> +  s.k = 2;
> +  s.u.m = 3;
> +  s.v.o = 4;
> +}
> +
> +/* { dg-final { scan-assembler-times "DW_AT_export_symbols" 4 } } */
> 
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-03-06 23:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-16  9:14 [PATCH] dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918] Jakub Jelinek
2024-02-16 15:48 ` Jason Merrill
2024-02-16 15:52   ` Jakub Jelinek
2024-02-16 19:52     ` [PATCH] dwarf2out, v2: " Jakub Jelinek
2024-02-16 20:40       ` Jason Merrill
2024-02-16 21:06         ` [PATCH] dwarf2out, v3: " Jakub Jelinek
2024-03-06 23:45           ` Jason Merrill

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).