[4/9] dwarf: generate annotation DIEs

Message ID 20220607214342.19463-5-david.faust@oracle.com
State New
Headers
Series Add debug_annotate attributes |

Commit Message

David Faust June 7, 2022, 9:43 p.m. UTC
  The "debug_annotate_decl" and "debug_annotate_type" attributes are
handled by constructing DW_TAG_GNU_annotation DIEs. These DIEs are
children of the declarations or types which they annotate, and convey
the information via a string constant.

gcc/

	* dwarf2out.cc (gen_decl_annotation_dies): New function.
	(gen_type_annotation_dies): Likewise.
	(modified_type_die): Call them here, if appropriate.
	(gen_formal_parameter_die): Likewise.
	(gen_typedef_die): Likewise.
	(gen_type_die): Likewise.
	(gen_decl_die): Likewise.
---
 gcc/dwarf2out.cc | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
  

Patch

diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index 9c61026bb34..aff9f72bd55 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -13611,6 +13611,78 @@  long_double_as_float128 (tree type)
   return NULL_TREE;
 }
 
+/* Given a tree T, which may be a decl or a type, process any
+   "debug_annotate_decl" attributes on T, provided in ATTR. Construct
+   DW_TAG_GNU_annotation DIEs appropriately as children of TARGET, usually
+   the DIE for T.  */
+
+static void
+gen_decl_annotation_dies (tree t, dw_die_ref target)
+{
+  dw_die_ref die;
+  tree attr;
+
+  if (t == NULL_TREE || !target)
+    return;
+
+  if (TYPE_P (t))
+    attr = lookup_attribute ("debug_annotate_decl", TYPE_ATTRIBUTES (t));
+  else if (DECL_P (t))
+    attr = lookup_attribute ("debug_annotate_decl", DECL_ATTRIBUTES (t));
+  else
+    /* This is an error.  */
+    gcc_unreachable ();
+
+  while (attr != NULL_TREE)
+    {
+      die = new_die (DW_TAG_GNU_annotation, target, t);
+      add_name_attribute (die, IDENTIFIER_POINTER (get_attribute_name (attr)));
+      add_AT_string (die, DW_AT_const_value,
+		     TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
+      attr = TREE_CHAIN (attr);
+    }
+
+  /* Strip the decl tag attribute to avoid creating multiple copies if we hit
+     this tree node again in some recursive call.  */
+  if (TYPE_P (t))
+    TYPE_ATTRIBUTES (t) =
+      remove_attribute ("debug_annotate_decl", TYPE_ATTRIBUTES (t));
+  else if (DECL_P (t))
+    DECL_ATTRIBUTES (t) =
+      remove_attribute ("debug_annotate_decl", DECL_ATTRIBUTES (t));
+}
+
+/* Given a tree TYPE, process any "debug_annotate_type" attributes on
+   TYPE. Construct DW_TAG_GNU_annotation DIEs appropriately as children of
+   TARGET, usually the DIE for TYPE.  */
+
+static void
+gen_type_annotation_dies (tree type, dw_die_ref target)
+{
+  dw_die_ref die;
+  tree attr;
+
+  if (type == NULL_TREE || !target)
+    return;
+
+  gcc_assert (TYPE_P (type));
+
+  attr = lookup_attribute ("debug_annotate_type", TYPE_ATTRIBUTES (type));
+  while (attr != NULL_TREE)
+    {
+      die = new_die (DW_TAG_GNU_annotation, target, type);
+      add_name_attribute (die, IDENTIFIER_POINTER (get_attribute_name (attr)));
+      add_AT_string (die, DW_AT_const_value,
+		     TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
+      attr = TREE_CHAIN (attr);
+    }
+
+  /* Strip the type tag attribute to avoid creating multiple copies if we hit
+     this type again in some recursive call.  */
+  TYPE_ATTRIBUTES (type) =
+    remove_attribute ("debug_annotate_type", TYPE_ATTRIBUTES (type));
+}
+
 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
    entry that chains the modifiers specified by CV_QUALS in front of the
    given type.  REVERSE is true if the type is to be interpreted in the
@@ -14009,6 +14081,9 @@  modified_type_die (tree type, int cv_quals, bool reverse,
   if (TYPE_ARTIFICIAL (type))
     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
 
+  /* Generate any annotation DIEs on this type.  */
+  gen_type_annotation_dies (type, mod_type_die);
+
   return mod_type_die;
 }
 
@@ -23002,6 +23077,9 @@  gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
       gcc_unreachable ();
     }
 
+  /* Generate any annotation DIEs for this decl.  */
+  gen_decl_annotation_dies (node, parm_die);
+
   return parm_die;
 }
 
@@ -26076,6 +26154,9 @@  gen_typedef_die (tree decl, dw_die_ref context_die)
 
   if (get_AT (type_die, DW_AT_name))
     add_pubtype (decl, type_die);
+
+  /* Generate any annotation DIEs for the typedef.  */
+  gen_decl_annotation_dies (decl, type_die);
 }
 
 /* Generate a DIE for a struct, class, enum or union type.  */
@@ -26389,6 +26470,16 @@  gen_type_die (tree type, dw_die_ref context_die)
 	  if (die)
 	    check_die (die);
 	}
+
+      /* Generate any annotation DIEs on the type.  */
+      dw_die_ref die = lookup_type_die (type);
+      if (die)
+	{
+	  gen_type_annotation_dies (type, die);
+
+	  /* "decl" annotations may also be attached to a type.  */
+	  gen_decl_annotation_dies (type, die);
+	}
     }
 }
 
@@ -27145,6 +27236,9 @@  gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
       break;
     }
 
+  /* Generate any annotation DIEs for the decl.  */
+  gen_decl_annotation_dies (decl, lookup_decl_die (decl_or_origin));
+
   return NULL;
 }