[3/3] dwarf-reader: Improve comments for maybe_strip_qualification

Message ID 20260929072248.1015420-4-dodji@seketeli.org
State New
Headers
Series [1/3] dwarf-reader: Fix an intermittent assert in maybe_strip_qualification |

Commit Message

Dodji Seketeli Sept. 29, 2026, 7:21 a.m. UTC
  maybe_strip_qualification is a weird one that can be touchy in
multi-threaded contexts so it needs better documentation.

The function edits the graph reachable from a given qualified type to
transform it into an equivalent normalized graph to avoid spurious
change reports later down the road.

In a multi-threaded context, doing this can introduce data races so
callers must be aware of what they are doing.

This patch adds further documentation to maybe_strip_qualification.

For more context, you can read the comments of the patch below that
provide more details about some of the transformation performed by
maybe_strip_qualification.

	commit 3c87247cb42a3d3d8f57b02ded34c35284aea51a
	Author: Dodji Seketeli <dodji@redhat.com>
	Date:   Tue Oct 27 17:12:58 2020 +0100

	    Bug PR26739 - Handle qualified typedef array types

	    CV-qualifiers of a typedef of an array type apply to the elements of
	    the array.  So this can be transformed into a typedef of array type
	    which element type is similarly CV-qualified.

	    That transformation helps avoiding spurious changes that might occur
	    when comparing the latter form against the former even though both are
	    equivalent.

	    This patch performs that transformation, just like we already do for
	    CV-qualified array types which are transformed into an array of
	    similarly CV-qualified elements.

	    Performing that transformation amounts to editing the type of the
	    array elements.  As those types might be used by other parts of the
	    type graph (type node sharing), the patch "clones" the type sub-tree
	    of interest and edits the cloned version.  That way, the shared type
	    nodes are not edited.  It appears that in the existing version of
	    maybe_strip_qualification, the transformation of CV-qualified arrays
	    into arrays of CV-qualified elements was unfortunately editing shared
	    type nodes.  The patch fixes that and re-works the logic of*
	    maybe_strip_qualification to make it handle this new case and the
	    previous one in a somewhat generic manner.

	* src/abg-dwarf-reader.cc (maybe_strip_qualification): Add more
	insightful comments for careful use in multi-threaded contexts.

Signed-off-by: Dodji Seketeli <dodji@seketeli.org>
---
 src/abg-dwarf-reader.cc | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
  

Patch

diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index 7963214a..43d57588 100644
--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -12444,8 +12444,14 @@  schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
 /// leak into the IR and make for bad diagnostics.
 ///
 /// This function thus strips the const qualifier from the type in
-/// that case.  It might contain code to strip other cases like this
-/// in the future.
+/// that case.  The function also applies CV-qualifiers of an array to
+/// the elements of the array.
+///
+/// Note that this functions edits the IR graph reachable from the
+/// qualified type, so callers must be aware of that.
+///
+/// Please note that this code might contain code to strip or massage
+/// other qualification-related cases like this in the future.
 ///
 /// @param t the type to strip const qualification from.
 ///
@@ -12469,6 +12475,13 @@  maybe_strip_qualification(const qualified_type_def_sptr t,
 
   if (is_array_type(u) || is_typedef_of_array(u))
     {
+      // Now, let's apply CV-qualifiers of an array (or of a typedef of an
+      // array) to the elements of the array, transforming a qualified
+      // array into an array of similarly qualified elements.
+      //
+      // Before editing the qualified array tree in this way, let's clone
+      // the tree first, because we'd be editing a share tree otherwise.
+
       array_type_def_sptr array;
       scope_decl_sptr scope = 0;
       if ((array = is_array_type(u)))
@@ -12516,6 +12529,10 @@  maybe_strip_qualification(const qualified_type_def_sptr t,
       ABG_ASSERT(!array->get_canonical_type());
       type_base_sptr element_type = array->get_element_type();
 
+      // Now that the array tree has been cloned, let's perform the
+      // "qualified array to array of similarly qualified elements"
+      // transformation.
+
       if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
 	{
 	  // We should not be editing types that are already canonicalized.