[1/4] Add named-types-ids to use name ids after the type name instead of numbers.

Message ID 20200421122821.13769-2-mark@klomp.org
State Changes Requested
Headers
Series [1/4] Add named-types-ids to use name ids after the type name instead of numbers. |

Commit Message

Mark Wielaard April 21, 2020, 12:28 p.m. UTC
  From: Mark Wielaard <mark@klomp.org>

To make the XML output more readable and a bit more stable generate
type ids based on the underlying type name. When types are inserted,
removed or rearranged the type ids will (mostly) be the same so that
references can stay the same. This also makes it easier to read and
compare the XML corpus representation.

	* doc/manuals/abidw.rst: Document --named-type-ids.
	* include/abg-ir.h (is_interned_string): New method.
	* include/abg-libxml-utils.h (replace_xml_type_string): Likewise.
	* include/abg-writer.h (set_named_type_ids): New function.
	(set_common_options): Call it.
	* src/abg-ir.cc (environment::is_interned_string): New method.
	* src/abg-libxml-utils.cc (replace_xml_type_string): New function.
	* src/abg-writer.cc (id_manager): Add get_id_for_type.
	(write_context): Add m_named_type_ids bool, get_named_type_ids
	and set_named_type_ids functions.
	(write_context::get_id_for_type): Check get_named_type_ids,
	use get_id_for_type.
	(set_named_type_ids): New function.
	* tools/abidw.cc (options): Add named_type_ids.
	(display_usage): Describe --named_type-ids.
	(parse_command_line): Parse --named-type-ids.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 doc/manuals/abidw.rst      |  8 ++++
 include/abg-ir.h           |  3 ++
 include/abg-libxml-utils.h |  2 +
 include/abg-writer.h       |  4 ++
 src/abg-ir.cc              |  7 ++++
 src/abg-libxml-utils.cc    | 58 ++++++++++++++++++++++++++
 src/abg-writer.cc          | 85 ++++++++++++++++++++++++++++++++++++--
 tools/abidw.cc             |  7 +++-
 8 files changed, 170 insertions(+), 4 deletions(-)
  

Comments

Dodji Seketeli April 23, 2020, 5:48 p.m. UTC | #1
Hello Mark,

First of all, thanks for working on this, it's really appreciated.

"Mark J. Wielaard" <mark@klomp.org> a ?crit:

> To make the XML output more readable and a bit more stable generate
> type ids based on the underlying type name. When types are inserted,
> removed or rearranged the type ids will (mostly) be the same so that
> references can stay the same. This also makes it easier to read and
> compare the XML corpus representation.

Note that abidw has the option --annotate that makes it easy to read the
abixml file, for cases where people want to read it.

Nevertheless, this new --named-type-ids is definitely a welcomed improvement for the
stability of abixml files.

I made some light changes to this patch and so I have attached the
resulting patch to this one, so that you can test it in your environment
and see if it still suits you.

I am thinking that we should add some basic testing for this option as
well, I am about to modify test-read-dwarf.cc to make it support this.
I haven't done it yet as I didn't want to delay this review any further.

[...]

> diff --git a/src/abg-ir.cc b/src/abg-ir.cc
> index 27831352..f4fee60a 100644
> --- a/src/abg-ir.cc
> +++ b/src/abg-ir.cc
> @@ -2884,6 +2884,13 @@ interned_string
>  environment::intern(const string& s) const
>  {return const_cast<environment*>(this)->priv_->string_pool_.create_string(s);}
>  
> +bool
> +environment::is_interned_string(const string& s) const
> +{

I have added a comment for this function.

> +  const char *c = s.c_str();
> +  return const_cast<environment*>(this)->priv_->string_pool_.has_string(c);
> +}
> +
>  // </environment stuff>
>  
>  // <type_or_decl_base stuff>
> diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
> index 2c46aad8..e8c1c1b8 100644
> --- a/src/abg-libxml-utils.cc
> +++ b/src/abg-libxml-utils.cc
> @@ -249,6 +249,64 @@ escape_xml_string(const std::string& str)
>    return result;
>  }
>  
> +/// Replace the various special characters in a type string as used in
> +/// a type-id attribute.
> +///
> +/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
> +/// The characters '&' and '*' at the end of a string are simply dropped,
> +/// otherwise they are also replaced by an '_'.
> +///
> +/// The result is not reversible.
> +///
> +//// @param str the input string to read to search for the characters
> +//// to replace.
> +////
> +//// @param replaced the output string where to write the resulting
> +//// string that contains the replaced characters,
> +void
> +replace_xml_type_string(const std::string& str, std::string& replaced)
> +{

I thinking you could have just used the xml::escape_xml_string function
that is in this abg-libxml-utils.cc file.  I understand that this new
one that you wrote makes the result more pleasant to read, though.  So I
am not arguing against your choice.

I have however changed the name of this function to
escape_xml_string_as_named_type_id to make the name a bit more
"self-documented" and more in-line with escape_xml_string that exists
already.

[...]

> --- a/src/abg-writer.cc
> +++ b/src/abg-writer.cc
> @@ -127,6 +127,54 @@ public:
>      ABG_ASSERT(env);
>      return env->intern(o.str());
>    }
> +
> +  /// Return a unique string representing a name, prefixed by a type
> +  /// string. The returned string will be made unique by postfixing
> +  /// underscores if necessary.
> +  ///
> +  /// @param type to create an unique id string for
> +  interned_string
> +  get_id_for_type(const type_base* type) const
> +  {
> +    ostringstream o;
> +    /* Try to find an appropriate prefix. */
> +    if (is_type_decl(type))
> +      o << "type-";
> +    else if (is_class_type(type))
> +      o << "class-";
> +    else if (is_union_type(type))
> +      o << "union-";
> +    else if (is_enum_type(type))
> +      o << "enum-";
> +    else if (is_typedef(type))
> +      o << "typedef-";
> +    else if (is_qualified_type(type))
> +      o << "qual-";
> +    else if (is_pointer_type(type))
> +      o << "ptr-";
> +    else if (is_reference_type(type))
> +      o << "ref-";
> +    else if (is_array_type(type))
> +      o << "array-";
> +    else if (is_subrange_type(type))
> +      o << "subrng-";
> +    else if (is_function_type(type))
> +      o << "func-";
> +    else
> +      ABG_ASSERT_NOT_REACHED;

Here, have replaced the above by a call to
abigail::ir::get_pretty_representation().  It does what you think it
does for all the types supported in the internal representation (IR).
Whenever a new type representation is added to the IR, this should
hopefully just works as well.  I have also renamed this function as
get_named_type_id() to make it a bit more self-documented.

[...]

> +
> +    string name = xml::replace_xml_type_string(get_type_name(type));
> +    o << name;
> +
> +    /* We want to make sure the id is unique. See if it is already
> +       interned in this environment, if it is, it isn't unique and we
> +       add some underscores to it till it is.  */
> +    const environment* env = get_environment();
> +    ABG_ASSERT(env);
> +    while (env->is_interned_string(o.str()))

Btw, I like this neat little trick you added (the
environment::is_interned_string function) to ensure the uniqueness of
the type id.  Well done.

> +      o << "_";
> +    return env->intern(o.str());
> +  }
>  };
>

[...]

So please, find my amended patch below and I hope I haven't botched it
too much.  Please let me know if it works for you.

Cheers,

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Add-named-types-ids-to-use-name-ids-after-the-type-n.patch
Type: text/x-patch
Size: 12653 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libabigail/attachments/20200423/6c282573/attachment.bin>
-------------- next part --------------
  
Matthias Männich April 24, 2020, 12:57 p.m. UTC | #2
Hi!

Thanks both for working on that!

On Thu, Apr 23, 2020 at 07:48:12PM +0200, Dodji Seketeli wrote:
>Hello Mark,
>
>First of all, thanks for working on this, it's really appreciated.
>
>"Mark J. Wielaard" <mark@klomp.org> a ?crit:
>
>> To make the XML output more readable and a bit more stable generate
>> type ids based on the underlying type name. When types are inserted,
>> removed or rearranged the type ids will (mostly) be the same so that
>> references can stay the same. This also makes it easier to read and
>> compare the XML corpus representation.
>
>Note that abidw has the option --annotate that makes it easy to read the
>abixml file, for cases where people want to read it.
>
>Nevertheless, this new --named-type-ids is definitely a welcomed improvement for the
>stability of abixml files.
>
>I made some light changes to this patch and so I have attached the
>resulting patch to this one, so that you can test it in your environment
>and see if it still suits you.
>
>I am thinking that we should add some basic testing for this option as
>well, I am about to modify test-read-dwarf.cc to make it support this.
>I haven't done it yet as I didn't want to delay this review any further.
>
>[...]
>
>> diff --git a/src/abg-ir.cc b/src/abg-ir.cc
>> index 27831352..f4fee60a 100644
>> --- a/src/abg-ir.cc
>> +++ b/src/abg-ir.cc
>> @@ -2884,6 +2884,13 @@ interned_string
>>  environment::intern(const string& s) const
>>  {return const_cast<environment*>(this)->priv_->string_pool_.create_string(s);}
>>
>> +bool
>> +environment::is_interned_string(const string& s) const
>> +{
>
>I have added a comment for this function.
>
>> +  const char *c = s.c_str();
>> +  return const_cast<environment*>(this)->priv_->string_pool_.has_string(c);
>> +}
>> +
>>  // </environment stuff>
>>
>>  // <type_or_decl_base stuff>
>> diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
>> index 2c46aad8..e8c1c1b8 100644
>> --- a/src/abg-libxml-utils.cc
>> +++ b/src/abg-libxml-utils.cc
>> @@ -249,6 +249,64 @@ escape_xml_string(const std::string& str)
>>    return result;
>>  }
>>
>> +/// Replace the various special characters in a type string as used in
>> +/// a type-id attribute.
>> +///
>> +/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
>> +/// The characters '&' and '*' at the end of a string are simply dropped,
>> +/// otherwise they are also replaced by an '_'.
>> +///
>> +/// The result is not reversible.
>> +///
>> +//// @param str the input string to read to search for the characters
>> +//// to replace.
>> +////
>> +//// @param replaced the output string where to write the resulting
>> +//// string that contains the replaced characters,
>> +void
>> +replace_xml_type_string(const std::string& str, std::string& replaced)
>> +{
>
>I thinking you could have just used the xml::escape_xml_string function
>that is in this abg-libxml-utils.cc file.  I understand that this new
>one that you wrote makes the result more pleasant to read, though.  So I
>am not arguing against your choice.
>
>I have however changed the name of this function to
>escape_xml_string_as_named_type_id to make the name a bit more
>"self-documented" and more in-line with escape_xml_string that exists
>already.
>
>[...]
>
>> --- a/src/abg-writer.cc
>> +++ b/src/abg-writer.cc
>> @@ -127,6 +127,54 @@ public:
>>      ABG_ASSERT(env);
>>      return env->intern(o.str());
>>    }
>> +
>> +  /// Return a unique string representing a name, prefixed by a type
>> +  /// string. The returned string will be made unique by postfixing
>> +  /// underscores if necessary.
>> +  ///
>> +  /// @param type to create an unique id string for
>> +  interned_string
>> +  get_id_for_type(const type_base* type) const
>> +  {
>> +    ostringstream o;
>> +    /* Try to find an appropriate prefix. */
>> +    if (is_type_decl(type))
>> +      o << "type-";
>> +    else if (is_class_type(type))
>> +      o << "class-";
>> +    else if (is_union_type(type))
>> +      o << "union-";
>> +    else if (is_enum_type(type))
>> +      o << "enum-";
>> +    else if (is_typedef(type))
>> +      o << "typedef-";
>> +    else if (is_qualified_type(type))
>> +      o << "qual-";
>> +    else if (is_pointer_type(type))
>> +      o << "ptr-";
>> +    else if (is_reference_type(type))
>> +      o << "ref-";
>> +    else if (is_array_type(type))
>> +      o << "array-";
>> +    else if (is_subrange_type(type))
>> +      o << "subrng-";
>> +    else if (is_function_type(type))
>> +      o << "func-";
>> +    else
>> +      ABG_ASSERT_NOT_REACHED;
>
>Here, have replaced the above by a call to
>abigail::ir::get_pretty_representation().  It does what you think it
>does for all the types supported in the internal representation (IR).
>Whenever a new type representation is added to the IR, this should
>hopefully just works as well.  I have also renamed this function as
>get_named_type_id() to make it a bit more self-documented.
>
>[...]
>
>> +
>> +    string name = xml::replace_xml_type_string(get_type_name(type));
>> +    o << name;
>> +
>> +    /* We want to make sure the id is unique. See if it is already
>> +       interned in this environment, if it is, it isn't unique and we
>> +       add some underscores to it till it is.  */
>> +    const environment* env = get_environment();
>> +    ABG_ASSERT(env);
>> +    while (env->is_interned_string(o.str()))
>
>Btw, I like this neat little trick you added (the
>environment::is_interned_string function) to ensure the uniqueness of
>the type id.  Well done.
>
>> +      o << "_";
>> +    return env->intern(o.str());
>> +  }
>>  };
>>
>
>[...]
>
>So please, find my amended patch below and I hope I haven't botched it
>too much.  Please let me know if it works for you.
>
>Cheers,
>

>From ecde139657bc8bde2aa4e98f4542be38f576c77d Mon Sep 17 00:00:00 2001
>From: Mark Wielaard <mark@klomp.org>
>Date: Tue, 21 Apr 2020 14:28:18 +0200
>Subject: [PATCH] Add named-types-ids to use name ids after the type name
> instead of numbers.
>
>To make the XML output more readable and a bit more stable generate
>type ids based on the textual representation of the type. When types
>are inserted, removed or rearranged the type ids will (mostly) be the
>same so that references can stay the same. This also makes it easier
>to read and compare the XML corpus representation.
>
>	* doc/manuals/abidw.rst: Document --named-type-ids.
>	* include/abg-ir.h (is_interned_string): New method.
>	* include/abg-libxml-utils.h (replace_xml_type_string): Likewise.
>	* include/abg-writer.h (set_named_type_ids): New function.
>	(set_common_options): Call it.
>	* src/abg-ir.cc (environment::is_interned_string): New method.
>	* src/abg-libxml-utils.cc (escape_xml_string_as_named_type_id):
>	New function.
>	* src/abg-writer.cc (id_manager::get_named_type_id): Add new
>	method.
>	(write_context::m_named_type_ids): New data member.
>	(write_context::write_context): Initialize it.
>	(write_context::{g,s}et_named_type_ids}): Add accessors.
>	(write_context::get_id_for_type): Use the new
>	id_manager::get_named_type_id if get_named_type_ids().
>	* tools/abidw.cc (options): Add named_type_ids.
>	(display_usage): Describe --named-type-ids.
>	(parse_command_line): Parse --named-type-ids.
>
>Signed-off-by: Mark Wielaard <mark@klomp.org>
>Signed-off-by: Dodji Seketeli <dodji@redhat.com>
>---
> doc/manuals/abidw.rst      |  8 +++++
> include/abg-ir.h           |  3 ++
> include/abg-libxml-utils.h |  2 ++
> include/abg-writer.h       |  4 +++
> src/abg-ir.cc              | 14 ++++++++
> src/abg-libxml-utils.cc    | 59 +++++++++++++++++++++++++++++++++
> src/abg-writer.cc          | 68 ++++++++++++++++++++++++++++++++++++--
> tools/abidw.cc             |  7 +++-
> 8 files changed, 161 insertions(+), 4 deletions(-)
>
>diff --git a/doc/manuals/abidw.rst b/doc/manuals/abidw.rst
>index 6cc4693c..1e427d32 100644
>--- a/doc/manuals/abidw.rst
>+++ b/doc/manuals/abidw.rst
>@@ -178,6 +178,14 @@ Options
>    In the emitted ABI representation, do not show file, line or column
>    where ABI artifacts are defined.
> 
>+  * ``--named-type-ids``
>+
>+    Without this option ids used to reference types in the XML file
>+    use simple numbers.  With this option the ids used are derived
>+    from the type name to make it easier to see which type is
>+    referenced and make the XML file more stable in case new types are
>+    added (without this option that might mean all id numbers change).
>+
>   * ``--check-alternate-debug-info-base-name`` <*elf-path*>
> 
> 
>diff --git a/include/abg-ir.h b/include/abg-ir.h
>index fda10de5..406a1719 100644
>--- a/include/abg-ir.h
>+++ b/include/abg-ir.h
>@@ -209,6 +209,9 @@ public:
>   interned_string
>   intern(const string&) const;
> 
>+  bool
>+  is_interned_string(const string&) const;
>+
>   friend class class_or_union;
>   friend class class_decl;
>   friend class function_type;
>diff --git a/include/abg-libxml-utils.h b/include/abg-libxml-utils.h
>index 6331bde5..7f85d5cb 100644
>--- a/include/abg-libxml-utils.h
>+++ b/include/abg-libxml-utils.h
>@@ -120,6 +120,8 @@ unescape_xml_comment(const std::string& str,
> std::string
> unescape_xml_comment(const std::string& str);
> 
>+std::string
>+escape_xml_string_as_named_type_id(const std::string& str);
> }//end namespace xml
> }//end namespace abigail
> #endif //__ABG_LIBXML_UTILS_H__
>diff --git a/include/abg-writer.h b/include/abg-writer.h
>index ed739ef1..f1598a15 100644
>--- a/include/abg-writer.h
>+++ b/include/abg-writer.h
>@@ -65,6 +65,9 @@ set_write_comp_dir(write_context& ctxt, bool flag);
> void
> set_short_locs(write_context& ctxt, bool flag);
> 
>+void
>+set_named_type_ids(write_context& ctxt, bool flag);
>+
> /// A convenience generic function to set common options (usually used
> /// by Libabigail tools) from a generic options carrying-object, into
> /// a given @ref write_context.
>@@ -84,6 +87,7 @@ set_common_options(write_context& ctxt, const OPTS& opts)
>   set_write_corpus_path(ctxt, opts.write_corpus_path);
>   set_write_comp_dir(ctxt, opts.write_comp_dir);
>   set_short_locs(ctxt, opts.short_locs);
>+  set_named_type_ids(ctxt, opts.named_type_ids);
> }
> 
> void
>diff --git a/src/abg-ir.cc b/src/abg-ir.cc
>index 27831352..e68c59cc 100644
>--- a/src/abg-ir.cc
>+++ b/src/abg-ir.cc
>@@ -2884,6 +2884,20 @@ interned_string
> environment::intern(const string& s) const
> {return const_cast<environment*>(this)->priv_->string_pool_.create_string(s);}
> 
>+/// Test if a given string has already been interned in the current
>+/// environment.
>+///
>+/// @param s the string to consider.
>+///
>+/// @return true iff @p s has already been intered in the current
>+/// environment.
>+bool
>+environment::is_interned_string(const string& s) const
>+{
>+  const char *c = s.c_str();
>+  return const_cast<environment*>(this)->priv_->string_pool_.has_string(c);
>+}
>+
> // </environment stuff>
> 
> // <type_or_decl_base stuff>
>diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
>index 2c46aad8..7370afd5 100644
>--- a/src/abg-libxml-utils.cc
>+++ b/src/abg-libxml-utils.cc
>@@ -249,6 +249,65 @@ escape_xml_string(const std::string& str)
>   return result;
> }
> 
>+/// Replace the various special characters in a type string as used in
>+/// a named type-id attribute.
>+///
>+/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
>+/// The characters '&' and '*' at the end of a string are simply dropped,
>+/// otherwise they are also replaced by an '_'.
>+///
>+/// The result is not reversible.
>+///
>+//// @param str the input string to read to search for the characters
>+//// to replace.
>+////
>+//// @param replaced the output string where to write the resulting
>+//// string that contains the replaced characters,
>+void
>+escape_xml_string_as_named_type_id(const std::string& str,
>+				   std::string& replaced)
>+{
>+  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
>+    switch (*i)
>+      {
>+      case ' ':
>+      case '<':
>+      case '>':
>+      case '"':
>+      case '\'':
>+	replaced += '_';
>+	break;
>+      case '&':
>+      case '*':

Would this not make references types and pointer types
indistinguishable?

Consider

   | template<typename T>
   | class Test{};
   |
   | Test<int&> a;
   | Test<int*> b;

While we add underscores until we have a unique match, that will later 

Also, why would we need to replace those characters at all? Except the
quote itself, we should be fine?! What am I missing? Can we reuse the
mangled name? Can replace less of those characters?


In the textual representation those are replaced differently:

   name='Test&lt;int&amp;&gt;'
   name='Test&lt;int*&gt;'

>+	if (i + 1 != str.end())
>+	  replaced += '_';
>+	break;
>+      default:
>+	replaced += *i;
>+      }
>+}
>+
>+/// Replace the various special characters in a type string as used in
>+/// a type-id attribute.
>+///
>+/// The characters '<', '>', ''', '"' are all replaced by '_'.
>+/// The character '&' is replaced by the string "-ref".

Is this really what is happening? I seem to miss the part where & is
transformed into '-ref'.

>+///
>+/// The result is not reversible.
>+///
>+//// @param str the input string to read to search for the characters
>+//// to replace.
>+////
>+//// @return the resulting string that contains the string with the
>+//// replaced characters.
>+std::string
>+escape_xml_string_as_named_type_id(const std::string& str)
>+{
>+  std::string result;
>+  escape_xml_string_as_named_type_id(str, result);
>+  return result;
>+}
>+
> /// Escape the '-' character, to avoid having a '--' in a comment.
> ///
> /// The resulting entity for '-' is '&#45;'.
>diff --git a/src/abg-writer.cc b/src/abg-writer.cc
>index 74fa1a8c..5f85810b 100644
>--- a/src/abg-writer.cc
>+++ b/src/abg-writer.cc
>@@ -127,6 +127,29 @@ public:
>     ABG_ASSERT(env);
>     return env->intern(o.str());
>   }
>+
>+  /// Return a unique string representing a type representation.  The
>+  /// returned string will be made unique by postfixing underscores if
>+  /// necessary.
>+  ///
>+  /// @param type to create an unique id string for
>+  interned_string
>+  get_named_type_id(const type_base* type) const
>+  {
>+    string named_type_id =
>+      xml::escape_xml_string_as_named_type_id(get_pretty_representation(type,
>+									/*internal=*/true));
>+
>+    /* We want to make sure the id is unique. See if it is already
>+       interned in this environment, if it is, it isn't unique and we
>+       add some underscores to it till it is.  */
>+    const environment* env = get_environment();
>+    ABG_ASSERT(env);
>+    while (env->is_interned_string(named_type_id))
>+      named_type_id = named_type_id + "_";

Postfixing the underscore has the advantage that this is not
reproducibly producing the same output for the same input. That would
defeat (a bit) the purpose of that patch to stabilize the output.

Again, thanks for working on that! :-)

Cheers,
Matthias

>+
>+    return env->intern(named_type_id);
>+  }
> };
> 
> /// A hashing functor that should be as fast as possible.
>@@ -174,6 +197,7 @@ class write_context
>   bool					m_write_corpus_path;
>   bool					m_write_comp_dir;
>   bool					m_short_locs;
>+  bool					m_named_type_ids;
>   mutable type_ptr_map			m_type_id_map;
>   mutable type_ptr_set_type		m_emitted_type_set;
>   type_ptr_set_type			m_emitted_decl_only_set;
>@@ -208,7 +232,8 @@ public:
>       m_write_architecture(true),
>       m_write_corpus_path(true),
>       m_write_comp_dir(true),
>-      m_short_locs(false)
>+      m_short_locs(false),
>+      m_named_type_ids(false)
>   {}
> 
>   /// Getter of the environment we are operating from.
>@@ -306,6 +331,20 @@ public:
>   set_short_locs(bool f)
>   {m_short_locs = f;}
> 
>+  /// Getter of the named-type-ids option.
>+  ///
>+  /// @return true iff named type ids shall be emitted
>+  bool
>+  get_named_type_ids() const
>+  {return m_named_type_ids;}
>+
>+  /// Setter of the named-type-ids option
>+  ///
>+  /// @param f the new value of the flag.
>+  void
>+  set_named_type_ids(bool f)
>+  {m_named_type_ids = f;}
>+
>   /// Getter of the "show-locs" option.
>   ///
>   /// When this option is true then the XML writer emits location
>@@ -357,6 +396,10 @@ public:
>   /// in a hash table, hashing the type.  So if the type has no id
>   /// associated to it, create a new one and return it.  Otherwise,
>   /// return the existing id for that type.
>+  ///
>+  /// @param t the type to get the id for.
>+  ///
>+  /// @return an interned string representing the id of the type.
>   interned_string
>   get_id_for_type(const type_base_sptr& t)
>   {return get_id_for_type(t.get());}
>@@ -365,6 +408,10 @@ public:
>   /// in a hash table, hashing the type.  So if the type has no id
>   /// associated to it, create a new one and return it.  Otherwise,
>   /// return the existing id for that type.
>+  ///
>+  /// @param t the type to get the id for.
>+  ///
>+  /// @return an interned string representing the id of the type.
>   interned_string
>   get_id_for_type(const type_base* t) const
>   {
>@@ -375,8 +422,11 @@ public:
>     type_ptr_map::const_iterator it = m_type_id_map.find(c);
>     if (it == m_type_id_map.end())
>       {
>-	interned_string id =
>-	  get_id_manager().get_id_with_prefix("type-id-");
>+	interned_string id;
>+	if (get_named_type_ids())
>+	  id = get_id_manager().get_named_type_id(c);
>+	else
>+	  id = get_id_manager().get_id_with_prefix("type-id-");
> 	m_type_id_map[c] = id;
> 	return id;
>       }
>@@ -2033,6 +2083,18 @@ void
> set_short_locs(write_context& ctxt, bool flag)
> {ctxt.set_short_locs(flag);}
> 
>+/// Set the 'named-type-ids' flag.
>+///
>+/// When this flag is set then the XML writer will emit type ids
>+/// based on the name of types, instead of numbered ids.
>+///
>+/// @param ctxt the context to set this flag on to.
>+///
>+/// @param flag the new value of the 'named-type_ids' flag.
>+void
>+set_named_type_ids(write_context& ctxt, bool flag)
>+{ctxt.set_named_type_ids(flag);}
>+
> /// Serialize the canonical types of a given scope.
> ///
> /// @param scope the scope to consider.
>diff --git a/tools/abidw.cc b/tools/abidw.cc
>index 72a8b0f1..2190e8aa 100644
>--- a/tools/abidw.cc
>+++ b/tools/abidw.cc
>@@ -111,6 +111,7 @@ struct options
>   bool			do_log;
>   bool			drop_private_types;
>   bool			drop_undefined_syms;
>+  bool			named_type_ids;
> 
>   options()
>     : display_version(),
>@@ -130,7 +131,8 @@ struct options
>       annotate(),
>       do_log(),
>       drop_private_types(false),
>-      drop_undefined_syms(false)
>+      drop_undefined_syms(false),
>+      named_type_ids(false)
>   {}
> 
>   ~options()
>@@ -164,6 +166,7 @@ display_usage(const string& prog_name, ostream& out)
>     << "  --short-locs  only print filenames rather than paths\n"
>     << "  --drop-private-types  drop private types from representation\n"
>     << "  --drop-undefined-syms  drop undefined symbols from representation\n"
>+    << "  --named-type-ids  use id attributes based on type names in XML file\n"
>     << "  --no-comp-dir-path  do not show compilation path information\n"
>     << "  --check-alternate-debug-info <elf-path>  check alternate debug info "
>     "of <elf-path>\n"
>@@ -304,6 +307,8 @@ parse_command_line(int argc, char* argv[], options& opts)
> 	opts.drop_private_types = true;
>       else if (!strcmp(argv[i], "--drop-undefined-syms"))
> 	opts.drop_undefined_syms = true;
>+      else if (!strcmp(argv[i], "--named-type-ids"))
>+	opts.named_type_ids = true;
>       else if (!strcmp(argv[i], "--no-linux-kernel-mode"))
> 	opts.linux_kernel_mode = false;
>       else if (!strcmp(argv[i], "--abidiff"))
>-- 
>2.26.1
>

>
>-- 
>		Dodji
  
Mark Wielaard April 24, 2020, 2:26 p.m. UTC | #3
Hi Dodji,

On Thu, 2020-04-23 at 19:48 +0200, Dodji Seketeli wrote:
> "Mark J. Wielaard" <mark@klomp.org> a ?crit:
> 
> > To make the XML output more readable and a bit more stable generate
> > type ids based on the underlying type name. When types are inserted,
> > removed or rearranged the type ids will (mostly) be the same so that
> > references can stay the same. This also makes it easier to read and
> > compare the XML corpus representation.
> 
> Note that abidw has the option --annotate that makes it easy to read the
> abixml file, for cases where people want to read it.

Yes, but that does make the file bigger and I found the xml-escaped
names in the annotation a bit harder to read (see also below).

> Nevertheless, this new --named-type-ids is definitely a welcomed
> improvement for the stability of abixml files.
> 
> I made some light changes to this patch and so I have attached the
> resulting patch to this one, so that you can test it in your environment
> and see if it still suits you.

The get_pretty_representation change makes things less ideal for my use
case, see below.

> I am thinking that we should add some basic testing for this option as
> well, I am about to modify test-read-dwarf.cc to make it support this.
> I haven't done it yet as I didn't want to delay this review any further.

Yes, sorry. I wanted to see first if the representation was acceptable.
But I guess we should at least add some "round-tripping" tests with
abidw --named-types-ids --abidiff to make sure things are at least
internally consistent.

> [...]
> 
> > diff --git a/src/abg-ir.cc b/src/abg-ir.cc
> > index 27831352..f4fee60a 100644
> > --- a/src/abg-ir.cc
> > +++ b/src/abg-ir.cc
> > @@ -2884,6 +2884,13 @@ interned_string
> >  environment::intern(const string& s) const
> >  {return const_cast<environment*>(this)->priv_-
> > >string_pool_.create_string(s);}
> >  
> > +bool
> > +environment::is_interned_string(const string& s) const
> > +{
> 
> I have added a comment for this function.

Thanks, looks good.

> > diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
> > index 2c46aad8..e8c1c1b8 100644
> > --- a/src/abg-libxml-utils.cc
> > +++ b/src/abg-libxml-utils.cc
> > @@ -249,6 +249,64 @@ escape_xml_string(const std::string& str)
> >    return result;
> >  }
> >  
> > +/// Replace the various special characters in a type string as used in
> > +/// a type-id attribute.
> > +///
> > +/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
> > +/// The characters '&' and '*' at the end of a string are simply dropped,
> > +/// otherwise they are also replaced by an '_'.
> > +///
> > +/// The result is not reversible.
> > +///
> > +//// @param str the input string to read to search for the characters
> > +//// to replace.
> > +////
> > +//// @param replaced the output string where to write the resulting
> > +//// string that contains the replaced characters,
> > +void
> > +replace_xml_type_string(const std::string& str, std::string& replaced)
> > +{
> 
> I thinking you could have just used the xml::escape_xml_string function
> that is in this abg-libxml-utils.cc file.  I understand that this new
> one that you wrote makes the result more pleasant to read, though.  So I
> am not arguing against your choice.

I indeed found the replacements using &[a-z]+;... not very readable.
It is a bit of a shame that we have to escape '<' inside attributes, so
it seemed right to also replace '>' (which doesn't need escaping inside
an attribute text). And some chars, like '&' and '*' at the end of an
identifier were redundant and so could be removed (but see below).

> I have however changed the name of this function to
> escape_xml_string_as_named_type_id to make the name a bit more
> "self-documented" and more in-line with escape_xml_string that exists
> already.

Yes, good idea.

> --- a/src/abg-writer.cc
> > +++ b/src/abg-writer.cc
> > @@ -127,6 +127,54 @@ public:
> >      ABG_ASSERT(env);
> >      return env->intern(o.str());
> >    }
> > +
> > +  /// Return a unique string representing a name, prefixed by a type
> > +  /// string. The returned string will be made unique by postfixing
> > +  /// underscores if necessary.
> > +  ///
> > +  /// @param type to create an unique id string for
> > +  interned_string
> > +  get_id_for_type(const type_base* type) const
> > +  {
> > +    ostringstream o;
> > +    /* Try to find an appropriate prefix. */
> > +    if (is_type_decl(type))
> > +      o << "type-";
> > +    else if (is_class_type(type))
> > +      o << "class-";
> > +    else if (is_union_type(type))
> > +      o << "union-";
> > +    else if (is_enum_type(type))
> > +      o << "enum-";
> > +    else if (is_typedef(type))
> > +      o << "typedef-";
> > +    else if (is_qualified_type(type))
> > +      o << "qual-";
> > +    else if (is_pointer_type(type))
> > +      o << "ptr-";
> > +    else if (is_reference_type(type))
> > +      o << "ref-";
> > +    else if (is_array_type(type))
> > +      o << "array-";
> > +    else if (is_subrange_type(type))
> > +      o << "subrng-";
> > +    else if (is_function_type(type))
> > +      o << "func-";
> > +    else
> > +      ABG_ASSERT_NOT_REACHED;
> 
> Here, have replaced the above by a call to
> abigail::ir::get_pretty_representation().  It does what you think it
> does for all the types supported in the internal representation (IR).
> Whenever a new type representation is added to the IR, this should
> hopefully just works as well.  I have also renamed this function as
> get_named_type_id() to make it a bit more self-documented.

I am not sure using get_pretty_representation() gives the same
guarantees as the above code. In particular it seems to accept both
types and decls and it will accept a "NULL" type (as "void"). does that
really make sure we are only handling real types at this point?

It might be a good idea to use get_pretty_representation() instead of
get_type_name() here, because it makes the type representation
"richer". But it seems it is not enough to make them "unique".

The idea of creating an unique type-prefix was to make clear what type
we are dealing with. e.g. both a base type, struct, union, enum,
typedef, qualified type, etc. can have the same "name". Which is
especially confusing with typedefs, pointers, references and functions.
That is why this function prefixes each type with "type-" for base
types, "class-" for structs, "typedef-" for typedefs. That way it is
easy to see if you are dealing with an enum, typedef or reference, etc.

So even if we replace get_type_name(), with
get_pretty_representation(), I would like to keep the prefixing.

> So please, find my amended patch below and I hope I haven't botched
> it too much.  Please let me know if it works for you.

I'll look at Matthias review next and will sent a V2 based on your
version.

Cheers,

Mark
  
Mark Wielaard April 24, 2020, 3:26 p.m. UTC | #4
Hi Matthias,

On Fri, 2020-04-24 at 14:57 +0200, Matthias Maennich wrote:
> On Thu, Apr 23, 2020 at 07:48:12PM +0200, Dodji Seketeli wrote:
> > "Mark J. Wielaard" <mark@klomp.org> a ?crit:
> > [...]
> > diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
> > index 2c46aad8..7370afd5 100644
> > --- a/src/abg-libxml-utils.cc
> > +++ b/src/abg-libxml-utils.cc
> > @@ -249,6 +249,65 @@ escape_xml_string(const std::string& str)
> >   return result;
> > }
> > 
> > +/// Replace the various special characters in a type string as used in
> > +/// a named type-id attribute.
> > +///
> > +/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
> > +/// The characters '&' and '*' at the end of a string are simply dropped,
> > +/// otherwise they are also replaced by an '_'.
> > +///
> > +/// The result is not reversible.
> > +///
> > +//// @param str the input string to read to search for the characters
> > +//// to replace.
> > +////
> > +//// @param replaced the output string where to write the resulting
> > +//// string that contains the replaced characters,
> > +void
> > +escape_xml_string_as_named_type_id(const std::string& str,
> > +				   std::string& replaced)
> > +{
> > +  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
> > +    switch (*i)
> > +      {
> > +      case ' ':
> > +      case '<':
> > +      case '>':
> > +      case '"':
> > +      case '\'':
> > +	replaced += '_';
> > +	break;
> > +      case '&':
> > +      case '*':
> 
> Would this not make references types and pointer types
> indistinguishable?

Yes, which is why in the original patch prefix the named-type-id with
the type-kind.

> Consider
> 
>    | template<typename T>
>    | class Test{};
>    |
>    | Test<int&> a;
>    | Test<int*> b;

So "int&" would come out as "ref-int" and "int*" would come out as
"ptr-int".

> While we add underscores until we have a unique match, that will later 
> 
> Also, why would we need to replace those characters at all? Except the
> quote itself, we should be fine?! What am I missing? Can we reuse the
> mangled name? Can replace less of those characters?

As far as I understand '<' and '&' always need to be escaped in XML
attributes. '>' doesn't, but it is really confusing to replace only '<'
and not '>', so to "balance it out" I replaced them both with the same
char. 

> In the textual representation those are replaced differently:
> 
>    name='Test&lt;int&amp;&gt;'
>    name='Test&lt;int*&gt;'

I found that to be really hard to read.
I admit that my tests have all be plain C, for which the replacement
turns out very nicely. I'll try some C++ code to see if I can make the
named-type-ids easier to read. Suggestions welcome. But maybe --named-
type-ids only really makes sense for plain C libraries...?

> > +	if (i + 1 != str.end())
> > +	  replaced += '_';
> > +	break;
> > +      default:
> > +	replaced += *i;
> > +      }
> > +}
> > +
> > +/// Replace the various special characters in a type string as
> > used in
> > +/// a type-id attribute.
> > +///
> > +/// The characters '<', '>', ''', '"' are all replaced by '_'.
> > +/// The character '&' is replaced by the string "-ref".
> 
> Is this really what is happening? I seem to miss the part where & is
> transformed into '-ref'.

Bad comment, the same function right above has the correct comment.
Instead of prefixes I originally used postfix -ref and -ptr, but that
didn't distinguish other types, which don't have special characters in
their names (like typedefs, which are often named after the underlying
type). I'll fix up the comment.

> > diff --git a/src/abg-writer.cc b/src/abg-writer.cc
> > index 74fa1a8c..5f85810b 100644
> > --- a/src/abg-writer.cc
> > +++ b/src/abg-writer.cc
> > @@ -127,6 +127,29 @@ public:
> >     ABG_ASSERT(env);
> >     return env->intern(o.str());
> >   }
> > +
> > +  /// Return a unique string representing a type representation.  The
> > +  /// returned string will be made unique by postfixing underscores if
> > +  /// necessary.
> > +  ///
> > +  /// @param type to create an unique id string for
> > +  interned_string
> > +  get_named_type_id(const type_base* type) const
> > +  {
> > +    string named_type_id =
> > +      xml::escape_xml_string_as_named_type_id(get_pretty_representation(type,
> > +									/*internal=*/true));
> > +
> > +    /* We want to make sure the id is unique. See if it is already
> > +       interned in this environment, if it is, it isn't unique and we
> > +       add some underscores to it till it is.  */
> > +    const environment* env = get_environment();
> > +    ABG_ASSERT(env);
> > +    while (env->is_interned_string(named_type_id))
> > +      named_type_id = named_type_id + "_";
> 
> Postfixing the underscore has the advantage that this is not
> reproducibly producing the same output for the same input. That would
> defeat (a bit) the purpose of that patch to stabilize the output.

I am afraid I don't fully understand what you are suggesting here.

There is an issue with anonymous types. Since they have the same names.
I don't have a good solution for that, but think it might be possible
to disambiguate them in most cases.

Another issue is using the exact same type name defined in different
translation units that are not the same type. That issue violates ODR
for C++, but can happen for C code. If it happens, it does depend on
the order we see the translation unit. But in the cases I am interested
in you would use --named-type-ids together with --drop-private-types.
Which makes it more likely that such internal same name, but different
type, issues pop up.

Cheers,

Mark
  
Matthias Männich April 24, 2020, 5:11 p.m. UTC | #5
Hi Mark!

On Fri, Apr 24, 2020 at 05:26:24PM +0200, Mark Wielaard wrote:
>Hi Matthias,
>
>On Fri, 2020-04-24 at 14:57 +0200, Matthias Maennich wrote:
>> On Thu, Apr 23, 2020 at 07:48:12PM +0200, Dodji Seketeli wrote:
>> > "Mark J. Wielaard" <mark@klomp.org> a ?crit:
>> > [...]
>> > diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
>> > index 2c46aad8..7370afd5 100644
>> > --- a/src/abg-libxml-utils.cc
>> > +++ b/src/abg-libxml-utils.cc
>> > @@ -249,6 +249,65 @@ escape_xml_string(const std::string& str)
>> >   return result;
>> > }
>> >
>> > +/// Replace the various special characters in a type string as used in
>> > +/// a named type-id attribute.
>> > +///
>> > +/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
>> > +/// The characters '&' and '*' at the end of a string are simply dropped,
>> > +/// otherwise they are also replaced by an '_'.
>> > +///
>> > +/// The result is not reversible.
>> > +///
>> > +//// @param str the input string to read to search for the characters
>> > +//// to replace.
>> > +////
>> > +//// @param replaced the output string where to write the resulting
>> > +//// string that contains the replaced characters,
>> > +void
>> > +escape_xml_string_as_named_type_id(const std::string& str,
>> > +				   std::string& replaced)
>> > +{
>> > +  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
>> > +    switch (*i)
>> > +      {
>> > +      case ' ':
>> > +      case '<':
>> > +      case '>':
>> > +      case '"':
>> > +      case '\'':
>> > +	replaced += '_';
>> > +	break;
>> > +      case '&':
>> > +      case '*':
>>
>> Would this not make references types and pointer types
>> indistinguishable?
>
>Yes, which is why in the original patch prefix the named-type-id with
>the type-kind.
>
>> Consider
>>
>>    | template<typename T>
>>    | class Test{};
>>    |
>>    | Test<int&> a;
>>    | Test<int*> b;
>
>So "int&" would come out as "ref-int" and "int*" would come out as
>"ptr-int".
>
>> While we add underscores until we have a unique match, that will later
>>
>> Also, why would we need to replace those characters at all? Except the
>> quote itself, we should be fine?! What am I missing? Can we reuse the
>> mangled name? Can replace less of those characters?
>
>As far as I understand '<' and '&' always need to be escaped in XML
>attributes. '>' doesn't, but it is really confusing to replace only '<'
>and not '>', so to "balance it out" I replaced them both with the same
>char.
>
>> In the textual representation those are replaced differently:
>>
>>    name='Test&lt;int&amp;&gt;'
>>    name='Test&lt;int*&gt;'
>
>I found that to be really hard to read.
>I admit that my tests have all be plain C, for which the replacement
>turns out very nicely. I'll try some C++ code to see if I can make the
>named-type-ids easier to read. Suggestions welcome. But maybe --named-
>type-ids only really makes sense for plain C libraries...?

When I was thinking of addressing this myself, I had in mind to do this
based on the type name and the type dependency graph.

The typename can maybe be mangled like C++. That should give a nice
string suitable to put into the xml attribute. Even for C. (And c++filt
could maybe make it readable if it needs to be). The format is well
thought through, battle tested and we only need underscores if the
typenames are exactly the same.

The above types Test<int&> and Test<int*> become 4TestIRiE and
4TestIPiE and  `cat abi.xml | c++filt -t` makes them human readable in
the representation file. (https://godbolt.org/z/WyA4xi)

Sorting those types topological and alphabetical and merging translation
units (as you did in patch 4/4) should yield a stable xml even across
larger refactorings.

How do you like this approach. Not necessarily the sorting, but the
encoding?

>
>> > +	if (i + 1 != str.end())
>> > +	  replaced += '_';
>> > +	break;
>> > +      default:
>> > +	replaced += *i;
>> > +      }
>> > +}
>> > +
>> > +/// Replace the various special characters in a type string as
>> > used in
>> > +/// a type-id attribute.
>> > +///
>> > +/// The characters '<', '>', ''', '"' are all replaced by '_'.
>> > +/// The character '&' is replaced by the string "-ref".
>>
>> Is this really what is happening? I seem to miss the part where & is
>> transformed into '-ref'.
>
>Bad comment, the same function right above has the correct comment.
>Instead of prefixes I originally used postfix -ref and -ptr, but that
>didn't distinguish other types, which don't have special characters in
>their names (like typedefs, which are often named after the underlying
>type). I'll fix up the comment.
>
>> > diff --git a/src/abg-writer.cc b/src/abg-writer.cc
>> > index 74fa1a8c..5f85810b 100644
>> > --- a/src/abg-writer.cc
>> > +++ b/src/abg-writer.cc
>> > @@ -127,6 +127,29 @@ public:
>> >     ABG_ASSERT(env);
>> >     return env->intern(o.str());
>> >   }
>> > +
>> > +  /// Return a unique string representing a type representation.  The
>> > +  /// returned string will be made unique by postfixing underscores if
>> > +  /// necessary.
>> > +  ///
>> > +  /// @param type to create an unique id string for
>> > +  interned_string
>> > +  get_named_type_id(const type_base* type) const
>> > +  {
>> > +    string named_type_id =
>> > +      xml::escape_xml_string_as_named_type_id(get_pretty_representation(type,
>> > +									/*internal=*/true));
>> > +
>> > +    /* We want to make sure the id is unique. See if it is already
>> > +       interned in this environment, if it is, it isn't unique and we
>> > +       add some underscores to it till it is.  */
>> > +    const environment* env = get_environment();
>> > +    ABG_ASSERT(env);
>> > +    while (env->is_interned_string(named_type_id))
>> > +      named_type_id = named_type_id + "_";
>>
>> Postfixing the underscore has the advantage that this is not
>> reproducibly producing the same output for the same input. That would
>> defeat (a bit) the purpose of that patch to stabilize the output.
>
>I am afraid I don't fully understand what you are suggesting here.

Sorry, that was a very confusing sentence (not sure who wrote that :-)).
What I meant to say is: If we have similar types (like the c++ example
above) that will convert into the same underscore representation and we
need add underscores to disambiguate, then we depend on the order we see
the translation units for the output format. If think I would like to
eliminate the underscore suffixing down to only the cases where the type
name is exactly the same.

>
>There is an issue with anonymous types. Since they have the same names.
>I don't have a good solution for that, but think it might be possible
>to disambiguate them in most cases.
>
>Another issue is using the exact same type name defined in different
>translation units that are not the same type. That issue violates ODR
>for C++, but can happen for C code. If it happens, it does depend on
>the order we see the translation unit. But in the cases I am interested
>in you would use --named-type-ids together with --drop-private-types.
>Which makes it more likely that such internal same name, but different
>type, issues pop up.

I think --named-type-ids is useful for everyone that puts the xml
description in the sources, updates it regularly and wants small human
readable diffs when doing so. I would say this is independent of the
language in use, but I agree that this is much less of a problem for C.

Cheers,
Matthias

>
>Cheers,
>
>Mark
  

Patch

diff --git a/doc/manuals/abidw.rst b/doc/manuals/abidw.rst
index 6cc4693c..1e427d32 100644
--- a/doc/manuals/abidw.rst
+++ b/doc/manuals/abidw.rst
@@ -178,6 +178,14 @@  Options
    In the emitted ABI representation, do not show file, line or column
    where ABI artifacts are defined.
 
+  * ``--named-type-ids``
+
+    Without this option ids used to reference types in the XML file
+    use simple numbers.  With this option the ids used are derived
+    from the type name to make it easier to see which type is
+    referenced and make the XML file more stable in case new types are
+    added (without this option that might mean all id numbers change).
+
   * ``--check-alternate-debug-info-base-name`` <*elf-path*>
 
 
diff --git a/include/abg-ir.h b/include/abg-ir.h
index fda10de5..406a1719 100644
--- a/include/abg-ir.h
+++ b/include/abg-ir.h
@@ -209,6 +209,9 @@  public:
   interned_string
   intern(const string&) const;
 
+  bool
+  is_interned_string(const string&) const;
+
   friend class class_or_union;
   friend class class_decl;
   friend class function_type;
diff --git a/include/abg-libxml-utils.h b/include/abg-libxml-utils.h
index 6331bde5..bd677027 100644
--- a/include/abg-libxml-utils.h
+++ b/include/abg-libxml-utils.h
@@ -120,6 +120,8 @@  unescape_xml_comment(const std::string& str,
 std::string
 unescape_xml_comment(const std::string& str);
 
+std::string
+replace_xml_type_string(const std::string& str);
 }//end namespace xml
 }//end namespace abigail
 #endif //__ABG_LIBXML_UTILS_H__
diff --git a/include/abg-writer.h b/include/abg-writer.h
index ed739ef1..f1598a15 100644
--- a/include/abg-writer.h
+++ b/include/abg-writer.h
@@ -65,6 +65,9 @@  set_write_comp_dir(write_context& ctxt, bool flag);
 void
 set_short_locs(write_context& ctxt, bool flag);
 
+void
+set_named_type_ids(write_context& ctxt, bool flag);
+
 /// A convenience generic function to set common options (usually used
 /// by Libabigail tools) from a generic options carrying-object, into
 /// a given @ref write_context.
@@ -84,6 +87,7 @@  set_common_options(write_context& ctxt, const OPTS& opts)
   set_write_corpus_path(ctxt, opts.write_corpus_path);
   set_write_comp_dir(ctxt, opts.write_comp_dir);
   set_short_locs(ctxt, opts.short_locs);
+  set_named_type_ids(ctxt, opts.named_type_ids);
 }
 
 void
diff --git a/src/abg-ir.cc b/src/abg-ir.cc
index 27831352..f4fee60a 100644
--- a/src/abg-ir.cc
+++ b/src/abg-ir.cc
@@ -2884,6 +2884,13 @@  interned_string
 environment::intern(const string& s) const
 {return const_cast<environment*>(this)->priv_->string_pool_.create_string(s);}
 
+bool
+environment::is_interned_string(const string& s) const
+{
+  const char *c = s.c_str();
+  return const_cast<environment*>(this)->priv_->string_pool_.has_string(c);
+}
+
 // </environment stuff>
 
 // <type_or_decl_base stuff>
diff --git a/src/abg-libxml-utils.cc b/src/abg-libxml-utils.cc
index 2c46aad8..e8c1c1b8 100644
--- a/src/abg-libxml-utils.cc
+++ b/src/abg-libxml-utils.cc
@@ -249,6 +249,64 @@  escape_xml_string(const std::string& str)
   return result;
 }
 
+/// Replace the various special characters in a type string as used in
+/// a type-id attribute.
+///
+/// The characters '<', '>', ''', '"' and ' ' are all replaced by '_'.
+/// The characters '&' and '*' at the end of a string are simply dropped,
+/// otherwise they are also replaced by an '_'.
+///
+/// The result is not reversible.
+///
+//// @param str the input string to read to search for the characters
+//// to replace.
+////
+//// @param replaced the output string where to write the resulting
+//// string that contains the replaced characters,
+void
+replace_xml_type_string(const std::string& str, std::string& replaced)
+{
+  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
+    switch (*i)
+      {
+      case ' ':
+      case '<':
+      case '>':
+      case '"':
+      case '\'':
+	replaced += '_';
+	break;
+      case '&':
+      case '*':
+	if (i + 1 != str.end())
+	  replaced += '_';
+	break;
+      default:
+	replaced += *i;
+      }
+}
+
+/// Replace the various special characters in a type string as used in
+/// a type-id attribute.
+///
+/// The characters '<', '>', ''', '"' are all replaced by '_'.
+/// The character '&' is replaced by the string "-ref".
+///
+/// The result is not reversible.
+///
+//// @param str the input string to read to search for the characters
+//// to replace.
+////
+//// @return the resulting string that contains the string with the
+//// replaced characters.
+std::string
+replace_xml_type_string(const std::string& str)
+{
+  std::string result;
+  replace_xml_type_string(str, result);
+  return result;
+}
+
 /// Escape the '-' character, to avoid having a '--' in a comment.
 ///
 /// The resulting entity for '-' is '&#45;'.
diff --git a/src/abg-writer.cc b/src/abg-writer.cc
index 74fa1a8c..c240443c 100644
--- a/src/abg-writer.cc
+++ b/src/abg-writer.cc
@@ -127,6 +127,54 @@  public:
     ABG_ASSERT(env);
     return env->intern(o.str());
   }
+
+  /// Return a unique string representing a name, prefixed by a type
+  /// string. The returned string will be made unique by postfixing
+  /// underscores if necessary.
+  ///
+  /// @param type to create an unique id string for
+  interned_string
+  get_id_for_type(const type_base* type) const
+  {
+    ostringstream o;
+    /* Try to find an appropriate prefix. */
+    if (is_type_decl(type))
+      o << "type-";
+    else if (is_class_type(type))
+      o << "class-";
+    else if (is_union_type(type))
+      o << "union-";
+    else if (is_enum_type(type))
+      o << "enum-";
+    else if (is_typedef(type))
+      o << "typedef-";
+    else if (is_qualified_type(type))
+      o << "qual-";
+    else if (is_pointer_type(type))
+      o << "ptr-";
+    else if (is_reference_type(type))
+      o << "ref-";
+    else if (is_array_type(type))
+      o << "array-";
+    else if (is_subrange_type(type))
+      o << "subrng-";
+    else if (is_function_type(type))
+      o << "func-";
+    else
+      ABG_ASSERT_NOT_REACHED;
+
+    string name = xml::replace_xml_type_string(get_type_name(type));
+    o << name;
+
+    /* We want to make sure the id is unique. See if it is already
+       interned in this environment, if it is, it isn't unique and we
+       add some underscores to it till it is.  */
+    const environment* env = get_environment();
+    ABG_ASSERT(env);
+    while (env->is_interned_string(o.str()))
+      o << "_";
+    return env->intern(o.str());
+  }
 };
 
 /// A hashing functor that should be as fast as possible.
@@ -174,6 +222,7 @@  class write_context
   bool					m_write_corpus_path;
   bool					m_write_comp_dir;
   bool					m_short_locs;
+  bool					m_named_type_ids;
   mutable type_ptr_map			m_type_id_map;
   mutable type_ptr_set_type		m_emitted_type_set;
   type_ptr_set_type			m_emitted_decl_only_set;
@@ -208,7 +257,8 @@  public:
       m_write_architecture(true),
       m_write_corpus_path(true),
       m_write_comp_dir(true),
-      m_short_locs(false)
+      m_short_locs(false),
+      m_named_type_ids(false)
   {}
 
   /// Getter of the environment we are operating from.
@@ -306,6 +356,20 @@  public:
   set_short_locs(bool f)
   {m_short_locs = f;}
 
+  /// Getter of the named-type-ids option.
+  ///
+  /// @return true iff named type ids shall be emitted
+  bool
+  get_named_type_ids() const
+  {return m_named_type_ids;}
+
+  /// Setter of the named-type-ids option
+  ///
+  /// @param f the new value of the flag.
+  void
+  set_named_type_ids(bool f)
+  {m_named_type_ids = f;}
+
   /// Getter of the "show-locs" option.
   ///
   /// When this option is true then the XML writer emits location
@@ -375,8 +439,11 @@  public:
     type_ptr_map::const_iterator it = m_type_id_map.find(c);
     if (it == m_type_id_map.end())
       {
-	interned_string id =
-	  get_id_manager().get_id_with_prefix("type-id-");
+	interned_string id;
+	if (get_named_type_ids())
+	  id = get_id_manager().get_id_for_type(c);
+	else
+	  id = get_id_manager().get_id_with_prefix("type-id-");
 	m_type_id_map[c] = id;
 	return id;
       }
@@ -2033,6 +2100,18 @@  void
 set_short_locs(write_context& ctxt, bool flag)
 {ctxt.set_short_locs(flag);}
 
+/// Set the 'named-type-ids' flag.
+///
+/// When this flag is set then the XML writer will emit type ids
+/// based on the name of types, instead of numbered ids.
+///
+/// @param ctxt the context to set this flag on to.
+///
+/// @param flag the new value of the 'named-type_ids' flag.
+void
+set_named_type_ids(write_context& ctxt, bool flag)
+{ctxt.set_named_type_ids(flag);}
+
 /// Serialize the canonical types of a given scope.
 ///
 /// @param scope the scope to consider.
diff --git a/tools/abidw.cc b/tools/abidw.cc
index 72a8b0f1..7251c98d 100644
--- a/tools/abidw.cc
+++ b/tools/abidw.cc
@@ -111,6 +111,7 @@  struct options
   bool			do_log;
   bool			drop_private_types;
   bool			drop_undefined_syms;
+  bool			named_type_ids;
 
   options()
     : display_version(),
@@ -130,7 +131,8 @@  struct options
       annotate(),
       do_log(),
       drop_private_types(false),
-      drop_undefined_syms(false)
+      drop_undefined_syms(false),
+      named_type_ids(false)
   {}
 
   ~options()
@@ -164,6 +166,7 @@  display_usage(const string& prog_name, ostream& out)
     << "  --short-locs  only print filenames rather than paths\n"
     << "  --drop-private-types  drop private types from representation\n"
     << "  --drop-undefined-syms  drop undefined symbols from representation\n"
+    << "  --named_type-ids  use id attributes based on type names in XML file\n"
     << "  --no-comp-dir-path  do not show compilation path information\n"
     << "  --check-alternate-debug-info <elf-path>  check alternate debug info "
     "of <elf-path>\n"
@@ -304,6 +307,8 @@  parse_command_line(int argc, char* argv[], options& opts)
 	opts.drop_private_types = true;
       else if (!strcmp(argv[i], "--drop-undefined-syms"))
 	opts.drop_undefined_syms = true;
+      else if (!strcmp(argv[i], "--named-type-ids"))
+	opts.named_type_ids = true;
       else if (!strcmp(argv[i], "--no-linux-kernel-mode"))
 	opts.linux_kernel_mode = false;
       else if (!strcmp(argv[i], "--abidiff"))