dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918]

Message ID Zc8naK+FA9aT8nqC@tucnak
State Unresolved
Headers
Series dwarf2out: Emit DW_AT_export_symbols on anon unions/structs [PR113918] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Feb. 16, 2024, 9:14 a.m. UTC
  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.


	Jakub
  

Comments

Jason Merrill Feb. 16, 2024, 3:48 p.m. UTC | #1
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
>
  
Jakub Jelinek Feb. 16, 2024, 3:52 p.m. UTC | #2
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
  

Patch

--- 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 } } */