From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 06/20] Remove DW_STRING and DW_STRING_IS_CANONICAL
Date: Sat, 28 Mar 2020 13:21:54 -0600 [thread overview]
Message-ID: <20200328192208.11324-7-tom@tromey.com> (raw)
In-Reply-To: <20200328192208.11324-1-tom@tromey.com>
This removes DW_STRING and DW_STRING_IS_CANONICAL, replacing them with
accessor methods on struct attribute. The new code ensures that a
string value will only ever be used when the form allows it.
2020-03-28 Tom Tromey <tom@tromey.com>
* dwarf2/read.c (read_cutu_die_from_dwo)
(read_attribute_reprocess, read_attribute_value, read_attribute)
(dwarf2_const_value_attr, dwarf2_name, dump_die_shallow)
(dwarf2_fetch_constant_bytes): Update.
* dwarf2/attribute.h (struct attribute) <form_is_string>: Declare.
<string_init, string_set>: New methods.
<string_is_canonical>: Update comment.
(DW_STRING, DW_STRING_IS_CANONICAL): Remove.
* dwarf2/attribute.c (attribute::form_is_string): New method.
(attribute::string): Use it.
---
gdb/ChangeLog | 13 +++++++++++
gdb/dwarf2/attribute.c | 26 ++++++++++++++-------
gdb/dwarf2/attribute.h | 27 +++++++++++++++++----
gdb/dwarf2/read.c | 53 ++++++++++++++++--------------------------
4 files changed, 72 insertions(+), 47 deletions(-)
diff --git a/gdb/dwarf2/attribute.c b/gdb/dwarf2/attribute.c
index 634b7979143..06b3245e4b2 100644
--- a/gdb/dwarf2/attribute.c
+++ b/gdb/dwarf2/attribute.c
@@ -61,18 +61,26 @@ attribute::address () const
/* See attribute.h. */
+bool
+attribute::form_is_string () const
+{
+ return (form == DW_FORM_strp || form == DW_FORM_line_strp
+ || form == DW_FORM_string
+ || form == DW_FORM_strx
+ || form == DW_FORM_strx1
+ || form == DW_FORM_strx2
+ || form == DW_FORM_strx3
+ || form == DW_FORM_strx4
+ || form == DW_FORM_GNU_str_index
+ || form == DW_FORM_GNU_strp_alt);
+}
+
+/* See attribute.h. */
+
const char *
attribute::string () const
{
- if (form == DW_FORM_strp || form == DW_FORM_line_strp
- || form == DW_FORM_string
- || form == DW_FORM_strx
- || form == DW_FORM_strx1
- || form == DW_FORM_strx2
- || form == DW_FORM_strx3
- || form == DW_FORM_strx4
- || form == DW_FORM_GNU_str_index
- || form == DW_FORM_GNU_strp_alt)
+ if (form_is_string ())
return u.str;
return nullptr;
}
diff --git a/gdb/dwarf2/attribute.h b/gdb/dwarf2/attribute.h
index f20540559aa..49989211018 100644
--- a/gdb/dwarf2/attribute.h
+++ b/gdb/dwarf2/attribute.h
@@ -89,6 +89,9 @@ struct attribute
bool form_is_block () const;
+ /* Check if the attribute's form is a string form. */
+ bool form_is_string () const;
+
/* Return DIE offset of this attribute. Return 0 with complaint if
the attribute is not of the required kind. */
@@ -106,13 +109,29 @@ struct attribute
return string_is_canonical;
}
+ /* Initialize this attribute to hold a string value. */
+ void string_init (const char *str)
+ {
+ gdb_assert (form_is_string ());
+ u.str = str;
+ string_is_canonical = 0;
+ }
+
+ /* Set the canonical string value for this attribute. */
+ void string_set (const char *str)
+ {
+ gdb_assert (form_is_string ());
+ u.str = str;
+ string_is_canonical = 1;
+ }
+
ENUM_BITFIELD(dwarf_attribute) name : 16;
ENUM_BITFIELD(dwarf_form) form : 15;
- /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
- field should be in u.str (existing only for DW_STRING) but it is kept
- here for better struct attribute alignment. */
+ /* Has u.str already been updated by dwarf2_canonicalize_name? This
+ field should be in u.str but it is kept here for better struct
+ attribute alignment. */
unsigned int string_is_canonical : 1;
union
@@ -129,8 +148,6 @@ struct attribute
/* Get at parts of an attribute structure. */
-#define DW_STRING(attr) ((attr)->u.str)
-#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
#define DW_UNSND(attr) ((attr)->u.unsnd)
#define DW_BLOCK(attr) ((attr)->u.blk)
#define DW_SND(attr) ((attr)->u.snd)
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 4b102e52e88..e3223e92d43 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -6474,8 +6474,7 @@ read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
comp_dir->name = DW_AT_comp_dir;
comp_dir->form = DW_FORM_string;
- DW_STRING_IS_CANONICAL (comp_dir) = 0;
- DW_STRING (comp_dir) = stub_comp_dir;
+ comp_dir->string_init (stub_comp_dir);
}
/* Set up for reading the DWO CU/TU. */
@@ -18321,16 +18320,11 @@ read_attribute_reprocess (const struct die_reader_specs *reader,
case DW_FORM_GNU_str_index:
{
unsigned int str_index = DW_UNSND (attr);
+ gdb_assert (!attr->canonical_p ());
if (reader->dwo_file != NULL)
- {
- DW_STRING (attr) = read_dwo_str_index (reader, str_index);
- DW_STRING_IS_CANONICAL (attr) = 0;
- }
+ attr->string_init (read_dwo_str_index (reader, str_index));
else
- {
- DW_STRING (attr) = read_stub_str_index (cu, str_index);
- DW_STRING_IS_CANONICAL (attr) = 0;
- }
+ attr->string_init (read_stub_str_index (cu, str_index));
break;
}
default:
@@ -18418,17 +18412,15 @@ read_attribute_value (const struct die_reader_specs *reader,
info_ptr += bytes_read;
break;
case DW_FORM_string:
- DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
- DW_STRING_IS_CANONICAL (attr) = 0;
+ attr->string_init (read_direct_string (abfd, info_ptr, &bytes_read));
info_ptr += bytes_read;
break;
case DW_FORM_strp:
if (!cu->per_cu->is_dwz)
{
- DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
+ attr->string_init (read_indirect_string (dwarf2_per_objfile,
abfd, info_ptr, cu_header,
- &bytes_read);
- DW_STRING_IS_CANONICAL (attr) = 0;
+ &bytes_read));
info_ptr += bytes_read;
break;
}
@@ -18436,10 +18428,9 @@ read_attribute_value (const struct die_reader_specs *reader,
case DW_FORM_line_strp:
if (!cu->per_cu->is_dwz)
{
- DW_STRING (attr)
- = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
- &bytes_read);
- DW_STRING_IS_CANONICAL (attr) = 0;
+ attr->string_init
+ (dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
+ &bytes_read));
info_ptr += bytes_read;
break;
}
@@ -18450,8 +18441,7 @@ read_attribute_value (const struct die_reader_specs *reader,
LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
&bytes_read);
- DW_STRING (attr) = dwz->read_string (objfile, str_offset);
- DW_STRING_IS_CANONICAL (attr) = 0;
+ attr->string_init (dwz->read_string (objfile, str_offset));
info_ptr += bytes_read;
}
break;
@@ -18615,6 +18605,7 @@ read_attribute (const struct die_reader_specs *reader,
const gdb_byte *info_ptr, bool *need_reprocess)
{
attr->name = abbrev->name;
+ attr->string_is_canonical = 0;
return read_attribute_value (reader, attr, abbrev->form,
abbrev->implicit_const, info_ptr,
need_reprocess);
@@ -20480,7 +20471,7 @@ dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
case DW_FORM_strx:
case DW_FORM_GNU_str_index:
case DW_FORM_GNU_strp_alt:
- /* DW_STRING is already allocated on the objfile obstack, point
+ /* The string is already allocated on the objfile obstack, point
directly to it. */
*bytes = (const gdb_byte *) attr->string ();
break;
@@ -21269,8 +21260,7 @@ dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
if (demangled == nullptr)
return nullptr;
- DW_STRING (attr) = objfile->intern (demangled.get ());
- DW_STRING_IS_CANONICAL (attr) = 1;
+ attr->string_set (objfile->intern (demangled.get ()));
}
/* Strip any leading namespaces/classes, keep only the base name.
@@ -21288,11 +21278,8 @@ dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
}
if (!attr->canonical_p ())
- {
- DW_STRING (attr) = dwarf2_canonicalize_name (attr->string (), cu,
- objfile);
- DW_STRING_IS_CANONICAL (attr) = 1;
- }
+ attr->string_set (dwarf2_canonicalize_name (attr->string (), cu,
+ objfile));
return attr->string ();
}
@@ -21405,9 +21392,9 @@ dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
case DW_FORM_GNU_str_index:
case DW_FORM_GNU_strp_alt:
fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
- DW_STRING (&die->attrs[i])
- ? DW_STRING (&die->attrs[i]) : "",
- die->attrs[i].canonical_p () ? "is" : "not");
+ die->attrs[i].string ()
+ ? die->attrs[i].string () : "",
+ die->attrs[i].canonical_p () ? "is" : "not");
break;
case DW_FORM_flag:
if (DW_UNSND (&die->attrs[i]))
@@ -21799,7 +21786,7 @@ dwarf2_fetch_constant_bytes (sect_offset sect_off,
case DW_FORM_strx:
case DW_FORM_GNU_str_index:
case DW_FORM_GNU_strp_alt:
- /* DW_STRING is already allocated on the objfile obstack, point
+ /* The string is already allocated on the objfile obstack, point
directly to it. */
result = (const gdb_byte *) attr->string ();
*len = strlen (attr->string ());
--
2.17.2
next prev parent reply other threads:[~2020-03-28 19:22 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-28 19:21 [PATCH 00/20] Make DWARF attribute references safe Tom Tromey
2020-03-28 19:21 ` [PATCH 01/20] Add attribute::value_as_string method Tom Tromey
2020-03-28 19:21 ` [PATCH 02/20] Rename struct attribute accessors Tom Tromey
2020-03-30 8:58 ` Aktemur, Tankut Baris
2020-03-30 23:39 ` Tom Tromey
2020-03-30 14:45 ` Simon Marchi
2020-03-30 23:39 ` Tom Tromey
2020-03-28 19:21 ` [PATCH 03/20] Avoid using DW_* macros in dwarf2/attribute.c Tom Tromey
2020-03-28 19:21 ` [PATCH 04/20] Change some uses of DW_STRING to string method Tom Tromey
2020-03-30 14:56 ` Simon Marchi
2020-03-30 23:53 ` Tom Tromey
2020-03-28 19:21 ` [PATCH 05/20] Remove some uses of DW_STRING_IS_CANONICAL Tom Tromey
2020-03-30 15:02 ` Simon Marchi
2020-03-31 0:01 ` Tom Tromey
2020-03-28 19:21 ` Tom Tromey [this message]
2020-03-30 15:10 ` [PATCH 06/20] Remove DW_STRING and DW_STRING_IS_CANONICAL Simon Marchi
2020-03-31 0:23 ` Tom Tromey
2020-03-28 19:21 ` [PATCH 07/20] Remove DW_BLOCK Tom Tromey
2020-03-30 15:13 ` Simon Marchi
2020-03-28 19:21 ` [PATCH 08/20] Remove DW_SIGNATURE Tom Tromey
2020-03-28 19:21 ` [PATCH 09/20] Remove DW_SND Tom Tromey
2020-03-28 19:21 ` [PATCH 10/20] Use setter for attribute's unsigned value Tom Tromey
2020-03-28 19:21 ` [PATCH 11/20] Add reprocessing flag to struct attribute Tom Tromey
2020-03-30 15:32 ` Simon Marchi
2020-04-04 14:02 ` Tom Tromey
2020-03-28 19:22 ` [PATCH 12/20] Remove DW_ADDR Tom Tromey
2020-03-30 15:40 ` Simon Marchi
2020-04-04 14:05 ` Tom Tromey
2020-03-28 19:22 ` [PATCH 13/20] Change how reprocessing is done Tom Tromey
2020-03-30 15:46 ` Simon Marchi
2020-04-04 14:14 ` Tom Tromey
2020-03-28 19:22 ` [PATCH 14/20] Change how accessibility is handled in dwarf2/read.c Tom Tromey
2020-03-30 15:50 ` Simon Marchi
2020-03-28 19:22 ` [PATCH 15/20] Add attribute::get_unsigned method Tom Tromey
2020-03-30 15:57 ` Simon Marchi
2020-04-04 14:17 ` Tom Tromey
2020-03-28 19:22 ` [PATCH 16/20] Change is_valid_DW_AT_defaulted to a method on attribute Tom Tromey
2020-03-30 16:00 ` Simon Marchi
2020-04-04 14:23 ` Tom Tromey
2020-03-28 19:22 ` [PATCH 17/20] Change die_info methods to check the attribute's form Tom Tromey
2020-03-30 16:02 ` Simon Marchi
2020-03-30 19:04 ` Tom Tromey
2020-03-30 20:18 ` Simon Marchi
2020-03-30 20:26 ` Tom Tromey
2020-03-28 19:22 ` [PATCH 18/20] Add attribute::virtuality method Tom Tromey
2020-03-28 19:22 ` [PATCH 19/20] Add attribute::boolean method Tom Tromey
2020-03-28 19:22 ` [PATCH 20/20] Remove DW_UNSND Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200328192208.11324-7-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).