public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
From: "Guillermo E. Martinez" <guillermo.e.martinez@oracle.com>
To: libabigail@sourceware.org
Cc: "Guillermo E. Martinez" <guillermo.e.martinez@oracle.com>
Subject: [PATCH 3/5] ctf-front-end: Strip qualification from a qualified array type
Date: Wed, 16 Nov 2022 21:43:03 -0600	[thread overview]
Message-ID: <20221117034305.184864-4-guillermo.e.martinez@oracle.com> (raw)
In-Reply-To: <20221117034305.184864-1-guillermo.e.martinez@oracle.com>

Redundant qualifier is emitted by CTF frond-end building IR for array
definitions:

   const char a[32];

   <qualified-type-def type-id='type-id-1' const='yes' id='type-id-2'/>
   <qualified-type-def type-id='type-id-3' const='yes' id='type-id-6'/>

Thus the diff reports:
   [C] 'const char[32] const a'

So, It strips the qualifiers from the array types.

	* src/abg-ctf-reader.cc (maybe_strip_qualification): New definition.
	(process_ctf_qualified_type): Strip redundant qualifiers.
	* tests/data/test-read-ctf/test-const-array.abi: New test.
	* tests/data/test-read-ctf/test-const-array.c: Likewise.
	* tests/data/test-read-ctf/test-const-array.o: Likewise.

Signed-off-by: Guillermo E. Martinez <guillermo.e.martinez@oracle.com>
---
 src/abg-ctf-reader.cc                         |  57 +++++++++++++++++-
 tests/data/Makefile.am                        |   3 +
 tests/data/test-read-ctf/test-const-array.abi |  14 +++++
 tests/data/test-read-ctf/test-const-array.c   |   2 +
 tests/data/test-read-ctf/test-const-array.o   | Bin 0 -> 1416 bytes
 tests/test-read-ctf.cc                        |   9 +++
 6 files changed, 83 insertions(+), 2 deletions(-)
 create mode 100644 tests/data/test-read-ctf/test-const-array.abi
 create mode 100644 tests/data/test-read-ctf/test-const-array.c
 create mode 100644 tests/data/test-read-ctf/test-const-array.o

diff --git a/src/abg-ctf-reader.cc b/src/abg-ctf-reader.cc
index feafc91d..7c267bb1 100644
--- a/src/abg-ctf-reader.cc
+++ b/src/abg-ctf-reader.cc
@@ -1261,6 +1261,51 @@ process_ctf_array_type(reader *rdr,
   return result;
 }
 
+/// Strip qualification from a qualified type, when it makes sense.
+///
+/// CTF constructs "const const char [N]" for "const char foo[N]"
+/// definition; This redundant types then causes bad diagnostics
+/// when it is compared with an DWARF IR.
+///
+/// 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.
+///
+/// @param t the type to strip const qualification from.
+///
+/// @param rdr the @ref reader to use.
+///
+/// @return the stripped type or just return @p t.
+static decl_base_sptr
+maybe_strip_qualification(const qualified_type_def_sptr t,
+                          reader *rdr)
+{
+  if (!t)
+    return t;
+
+  decl_base_sptr result = t;
+  type_base_sptr u = t->get_underlying_type();
+
+  if (is_array_type(u) || is_typedef_of_array(u))
+    {
+      array_type_def_sptr array = is_array_type(u);
+      ABG_ASSERT(array);
+      // We should not be editing types that are already canonicalized.
+      ABG_ASSERT(!array->get_canonical_type());
+      type_base_sptr element_type = array->get_element_type();
+
+      if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
+        {
+          qualified_type_def::CV quals = qualified->get_cv_quals();
+          quals |= t->get_cv_quals();
+          qualified->set_cv_quals(quals);
+          result = is_decl(u);
+        }
+    }
+
+  return result;
+}
+
 /// Build and return a qualified type libabigail IR.
 ///
 /// @param rdr the read context.
@@ -1304,8 +1349,16 @@ process_ctf_qualified_type(reader *rdr,
   result.reset(new qualified_type_def(utype, qualifiers, location()));
   if (result)
     {
-      decl_base_sptr qualified_type_decl = get_type_declaration(result);
-      add_decl_to_scope(qualified_type_decl, tunit->get_global_scope());
+      // Strip some potentially redundant type qualifiers from
+      // the qualified type we just built.
+      decl_base_sptr d = maybe_strip_qualification(is_qualified_type(result),
+                                                   rdr);
+      if (!d)
+        d = get_type_declaration(result);
+      ABG_ASSERT(d);
+
+      add_decl_to_scope(d, tunit->get_global_scope());
+      result = is_type(d);
       rdr->add_type(ctf_dictionary, ctf_type, result);
     }
 
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
index b89a4dd8..8d4a2b8f 100644
--- a/tests/data/Makefile.am
+++ b/tests/data/Makefile.am
@@ -710,6 +710,9 @@ test-read-ctf/test-bitfield.o		\
 test-read-ctf/test-bitfield-enum.abi	\
 test-read-ctf/test-bitfield-enum.c	\
 test-read-ctf/test-bitfield-enum.o	\
+test-read-ctf/test-const-array.abi	\
+test-read-ctf/test-const-array.c	\
+test-read-ctf/test-const-array.o	\
 \
 test-annotate/test0.abi			\
 test-annotate/test1.abi			\
diff --git a/tests/data/test-read-ctf/test-const-array.abi b/tests/data/test-read-ctf/test-const-array.abi
new file mode 100644
index 00000000..bd60b098
--- /dev/null
+++ b/tests/data/test-read-ctf/test-const-array.abi
@@ -0,0 +1,14 @@
+<abi-corpus version='2.1' path='data/test-read-ctf/test-const-array.o'>
+  <elf-variable-symbols>
+    <elf-symbol name='a' size='32' type='object-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
+  </elf-variable-symbols>
+  <abi-instr address-size='64' language='LANG_C'>
+    <type-decl name='char' size-in-bits='8' id='type-id-1'/>
+    <array-type-def dimensions='1' type-id='type-id-2' size-in-bits='256' id='type-id-3'>
+      <subrange length='32' type-id='type-id-4' id='type-id-5'/>
+    </array-type-def>
+    <type-decl name='unsigned long int' size-in-bits='64' id='type-id-4'/>
+    <qualified-type-def type-id='type-id-1' const='yes' id='type-id-2'/>
+    <var-decl name='a' type-id='type-id-3' mangled-name='a' visibility='default' elf-symbol-id='a'/>
+  </abi-instr>
+</abi-corpus>
diff --git a/tests/data/test-read-ctf/test-const-array.c b/tests/data/test-read-ctf/test-const-array.c
new file mode 100644
index 00000000..4ffecf87
--- /dev/null
+++ b/tests/data/test-read-ctf/test-const-array.c
@@ -0,0 +1,2 @@
+/* gcc -gctf -c test-const-array.c -o test-const-array.o */
+const char a[32];
diff --git a/tests/data/test-read-ctf/test-const-array.o b/tests/data/test-read-ctf/test-const-array.o
new file mode 100644
index 0000000000000000000000000000000000000000..b50e2fe5a7f7c86b3220c08782ee1d72035cc4d3
GIT binary patch
literal 1416
zcmbtU&2H2%5T3NlQhpA!LRAG4GACL=oNPFi1B$4i91sU2?sc+eH)__YY*#cVu1LM`
zNPQEY0bT%R5__|?Rz1K-<N5o(vB&Z5x3?eOZA1}xiQqL1k<S8r-x&P7b91;4_u%)>
z(X#SrfWQcS(k-VC(4L^4qTWK?b4tG3Z%h{Pn`lTfipQM`W9pOlKCL^n(8dI2s7^B4
z6Uw>JP&aDE&XuWVN={j&EC@*E%|a$cTeL}|MOn+l=rXCRLKM|ZRCTQ6*ThO=O)?c$
zbZ)Grn8v03(nn>ZjE#lXqUELE_6B==bo7etAAkIOz$Pzw%2Sr5SvHxZhiqS7oJn0R
zq_Uzu=zGxl87kcil5oc8s+$R8lE}q$H#Z5@NAT-Uh<S*K?XG)YVIDwJ$Jd7w0Nk_-
zD+;t$_jc|*D=#c?x+dU-F~ISPfa|8ul+A+X0<Ib>IZhHAE6P(B&(t|T(+y5<TYrH6
zvyYKZGrRY)Y?d#*>rQxoPq9on|M)yQ_fB9#{xhu88~s=Q3*?49Y0I1zl<-4_*a>1N
zxBM{@2_<(x)<we-#)nuUXgWYT@5J$+2YHw7I8=Ir>T|2m%XP^$*iV?djT)Z+hvSFm
zqPt!df`weFUpfAJ2fpiYo=^WnBzI85Z%6qp*L0VH6$!`M#@agm6UUb;TyjJHU(J<n
AdH?_b

literal 0
HcmV?d00001

diff --git a/tests/test-read-ctf.cc b/tests/test-read-ctf.cc
index 5d0e7435..61a5682c 100644
--- a/tests/test-read-ctf.cc
+++ b/tests/test-read-ctf.cc
@@ -363,6 +363,15 @@ static InOutSpec in_out_specs[] =
     "output/test-read-ctf/test-bitfield-enum.abi",
     "--ctf",
    },
+   {
+     "data/test-read-ctf/test-const-array.o",
+     "",
+     "",
+     SEQUENCE_TYPE_ID_STYLE,
+     "data/test-read-ctf/test-const-array.abi",
+     "output/test-read-ctf/test-const-array.abi",
+     "--ctf",
+  },
   // This should be the last entry.
   {NULL, NULL, NULL, SEQUENCE_TYPE_ID_STYLE, NULL, NULL}
 };
-- 
2.35.1


  parent reply	other threads:[~2022-11-17  3:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17  3:43 [PATCH 0/5] CTF front-end Bug Fixing and improvement Guillermo E. Martinez
2022-11-17  3:43 ` [PATCH 1/5] ctf-front-end: Set alignment-in-bits property to 0 Guillermo E. Martinez
2022-11-29 11:46   ` Dodji Seketeli
2022-11-17  3:43 ` [PATCH 2/5] ctf-front-end: Fix size and name for underlying types Guillermo E. Martinez
2022-11-29 11:53   ` Dodji Seketeli
2022-11-29 13:59     ` Dodji Seketeli
2022-11-29 18:32       ` [PATCHv2 2/5]ctf-front-end: " Guillermo E. Martinez
2022-11-30  9:14         ` Dodji Seketeli
2022-11-29 18:53     ` [PATCH 2/5] ctf-front-end: " Guillermo E. Martinez
2022-11-17  3:43 ` Guillermo E. Martinez [this message]
2022-11-30  9:33   ` [PATCH 3/5] ctf-front-end: Strip qualification from a qualified array type Dodji Seketeli
2022-12-02  3:48     ` Guillermo E. Martinez
2022-11-17  3:43 ` [PATCH 4/5] ctf-front-end: Fix representation for multidimensional " Guillermo E. Martinez
2022-11-30  9:43   ` Dodji Seketeli
2022-11-17  3:43 ` [PATCH 5/5] ctf-front-end: Fix array size representation Guillermo E. Martinez
2022-11-30  9:58   ` Dodji Seketeli

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=20221117034305.184864-4-guillermo.e.martinez@oracle.com \
    --to=guillermo.e.martinez@oracle.com \
    --cc=libabigail@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).