[05/20] Remove some uses of DW_STRING_IS_CANONICAL

Message ID 20200328192208.11324-6-tom@tromey.com
State New
Headers show
Series
  • Make DWARF attribute references safe
Related show

Commit Message

Tom Tromey March 28, 2020, 7:21 p.m.
This removes the rvalue uses of DW_STRING_IS_CANONICAL, replacing them
with an accessor method.

2020-03-28  Tom Tromey  <tom@tromey.com>

	* dwarf2/read.c (anonymous_struct_prefix, dwarf2_name)
	(dump_die_shallow): Use canonical_p.
	* dwarf2/attribute.h (struct attribute) <canonical_p>: New
	method.
---
 gdb/ChangeLog          | 7 +++++++
 gdb/dwarf2/attribute.h | 6 ++++++
 gdb/dwarf2/read.c      | 8 ++++----
 3 files changed, 17 insertions(+), 4 deletions(-)

-- 
2.17.2

Comments

Simon Marchi March 30, 2020, 3:02 p.m. | #1
On 2020-03-28 3:21 p.m., Tom Tromey wrote:
> This removes the rvalue uses of DW_STRING_IS_CANONICAL, replacing them

> with an accessor method.

> 

> 2020-03-28  Tom Tromey  <tom@tromey.com>

> 

> 	* dwarf2/read.c (anonymous_struct_prefix, dwarf2_name)

> 	(dump_die_shallow): Use canonical_p.

> 	* dwarf2/attribute.h (struct attribute) <canonical_p>: New

> 	method.

> ---

>  gdb/ChangeLog          | 7 +++++++

>  gdb/dwarf2/attribute.h | 6 ++++++

>  gdb/dwarf2/read.c      | 8 ++++----

>  3 files changed, 17 insertions(+), 4 deletions(-)

> 

> diff --git a/gdb/dwarf2/attribute.h b/gdb/dwarf2/attribute.h

> index cefd3c5541e..f20540559aa 100644

> --- a/gdb/dwarf2/attribute.h

> +++ b/gdb/dwarf2/attribute.h

> @@ -100,6 +100,12 @@ struct attribute

>  

>    LONGEST constant_value (int default_value) const;

>  

> +  /* Return true if this attribute holds a canonical string.  */

> +  bool canonical_p () const

> +  {

> +    return string_is_canonical;

> +  }


Would you be able to improve the documentation to say what is
a "canonical string"?  If this is a DWARF concept and described
in the DWARF spec, you could just refer to the relevant section
of the standard too.

Is this method only relevant if the attribute is of a string
form?  If so, the method name should perhaps still have "string"
in its name, like "is_canonical_string" or "canonical_string_p".

I presume it doesn't make sense to call this method when the
attribute has a non-string form, so should there be a gdb_assert
that checks that the attribute has a string form?

Simon
Tom Tromey March 31, 2020, 12:01 a.m. | #2
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:


Simon> Would you be able to improve the documentation to say what is
Simon> a "canonical string"?

Yeah, I di dthis.

Simon> If this is a DWARF concept and described
Simon> in the DWARF spec, you could just refer to the relevant section
Simon> of the standard too.

Just FYI, it isn't; it is a gdb concept.

Simon> Is this method only relevant if the attribute is of a string
Simon> form?  If so, the method name should perhaps still have "string"
Simon> in its name, like "is_canonical_string" or "canonical_string_p".

Done.

Simon> I presume it doesn't make sense to call this method when the
Simon> attribute has a non-string form, so should there be a gdb_assert
Simon> that checks that the attribute has a string form?

I had to move this to the next patch, since form_is_string doesn't exist
yet.

Tom

Patch

diff --git a/gdb/dwarf2/attribute.h b/gdb/dwarf2/attribute.h
index cefd3c5541e..f20540559aa 100644
--- a/gdb/dwarf2/attribute.h
+++ b/gdb/dwarf2/attribute.h
@@ -100,6 +100,12 @@  struct attribute
 
   LONGEST constant_value (int default_value) const;
 
+  /* Return true if this attribute holds a canonical string.  */
+  bool canonical_p () const
+  {
+    return string_is_canonical;
+  }
+
 
   ENUM_BITFIELD(dwarf_attribute) name : 16;
   ENUM_BITFIELD(dwarf_form) form : 15;
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index eb5ee98de60..4b102e52e88 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -20934,7 +20934,7 @@  anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
     return NULL;
 
   /* dwarf2_name had to be already called.  */
-  gdb_assert (DW_STRING_IS_CANONICAL (attr));
+  gdb_assert (attr->canonical_p ());
 
   /* Strip the base name, keep any leading namespaces/classes.  */
   base = strrchr (attr->string (), ':');
@@ -21262,7 +21262,7 @@  dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
 
 	  /* Avoid demangling attr->string () the second time on a second
 	     call for the same DIE.  */
-	  if (!DW_STRING_IS_CANONICAL (attr))
+	  if (!attr->canonical_p ())
 	    {
 	      gdb::unique_xmalloc_ptr<char> demangled
 		(gdb_demangle (attr->string (), DMGL_TYPES));
@@ -21287,7 +21287,7 @@  dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
       break;
     }
 
-  if (!DW_STRING_IS_CANONICAL (attr))
+  if (!attr->canonical_p ())
     {
       DW_STRING (attr) = dwarf2_canonicalize_name (attr->string (), cu,
 						   objfile);
@@ -21407,7 +21407,7 @@  dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
 	  fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
 		   DW_STRING (&die->attrs[i])
 		   ? DW_STRING (&die->attrs[i]) : "",
-		   DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
+		   die->attrs[i].canonical_p () ? "is" : "not");
 	  break;
 	case DW_FORM_flag:
 	  if (DW_UNSND (&die->attrs[i]))