diff mbox series

ctf-reader: Add support to undefined forward declaration types

Message ID 20211212041452.380448-1-guillermo.e.martinez@oracle.com
State New
Headers show
Series ctf-reader: Add support to undefined forward declaration types | expand

Commit Message

Guillermo E. Martinez Dec. 12, 2021, 4:14 a.m. UTC
Hello libabigail team,

This patch add support to process undefined forward declarations
in ctf reader back-end.

Dependencies/limitations:

* It works on the top of the following patch:
  https://sourceware.org/pipermail/libabigail/2021q4/003985.html

I'll appreciate your comments.

Kind Regards,
Guillermo


Undefined struct/union forward declaration types are not serialized in
abixml representation using `class-decl' node.

For instance, consider:

    struct key_type;
    typedef void (*key_restrict_link_func_t)(struct key_type *type);

Expected node:

  <class-decl name='key_type' ... is-declaration-only='yes'.../>

	* src/abg-ctf-reader.cc (process_ctf_forward_type): New
	function.
	(process_ctf_type): New CTF_K_FORWARD case.
	* tests/data/test-read-ctf/test-forward-undefine-type-decl.c:
	New testcase.
	* tests/data/test-read-ctf/test-forward-undefine-type-decl.abi:
	New expected result.
	* tests/data/test-read-ctf/test-forward-undefine-type-decl.o
	New test input.
	* tests/test-read-ctf.cc: Add new testcase to test harness.

Signed-off-by: Guillermo E. Martinez <guillermo.e.martinez@oracle.com>
---
 src/abg-ctf-reader.cc                         |  53 ++++++++++++++++++
 .../test-forward-undefine-type-decl.abi       |  31 ++++++++++
 .../test-forward-undefine-type-decl.c         |  17 ++++++
 .../test-forward-undefine-type-decl.o         | Bin 0 -> 1488 bytes
 tests/test-read-ctf.cc                        |   8 +++
 5 files changed, 109 insertions(+)
 create mode 100644 tests/data/test-read-ctf/test-forward-undefine-type-decl.abi
 create mode 100644 tests/data/test-read-ctf/test-forward-undefine-type-decl.c
 create mode 100644 tests/data/test-read-ctf/test-forward-undefine-type-decl.o
diff mbox series

Patch

diff --git a/src/abg-ctf-reader.cc b/src/abg-ctf-reader.cc
index bc49f49c..99937a26 100644
--- a/src/abg-ctf-reader.cc
+++ b/src/abg-ctf-reader.cc
@@ -384,6 +384,53 @@  process_ctf_sou_members(read_context *ctxt,
     fprintf(stderr, "ERROR from ctf_member_next\n");
 }
 
+static type_base_sptr
+process_ctf_forward_type(read_context *ctxt,
+                         corpus_sptr corp,
+                         translation_unit_sptr tunit,
+                         ctf_dict_t *ctf_dictionary,
+                         ctf_id_t ctf_type)
+{
+  decl_base_sptr result;
+  std::string type_name = ctf_type_name_raw(ctf_dictionary,
+                                            ctf_type);
+  bool type_is_anonymous = (type_name == "");
+  uint32_t kind = ctf_type_kind_forwarded (ctf_dictionary, ctf_type);
+
+  if (kind == CTF_K_UNION)
+    {
+      union_decl_sptr
+       union_fwd(new union_decl(ctxt->ir_env,
+                                type_name,
+                                /*alignment=*/0,
+                                location(),
+                                decl_base::VISIBILITY_DEFAULT,
+                                type_is_anonymous));
+      union_fwd->set_is_declaration_only(true);
+      result = union_fwd;
+    }
+  else
+    {
+      class_decl_sptr
+       struct_fwd(new class_decl(ctxt->ir_env, type_name,
+                                 /*alignment=*/0, /*size=*/0,
+                                 true /* is_struct */,
+                                 location(),
+                                 decl_base::VISIBILITY_DEFAULT,
+                                 type_is_anonymous));
+      struct_fwd->set_is_declaration_only(true);
+      result = struct_fwd;
+    }
+
+  if (!result)
+    return is_type(result);
+
+  add_decl_to_scope(result, tunit->get_global_scope());
+  ctxt->add_type(ctf_type, is_type(result));
+
+  return is_type(result);
+}
+
 /// Build and return a struct type libabigail IR.
 ///
 /// @param ctxt the read context.
@@ -813,6 +860,12 @@  process_ctf_type(read_context *ctxt,
         result = is_type(struct_decl);
         break;
       }
+    case CTF_K_FORWARD:
+      {
+        result = process_ctf_forward_type(ctxt, corp, tunit, ctf_dictionary,
+                                          ctf_type);
+      }
+      break;
     case CTF_K_UNION:
       {
         union_decl_sptr union_decl
diff --git a/tests/data/test-read-ctf/test-forward-undefine-type-decl.abi b/tests/data/test-read-ctf/test-forward-undefine-type-decl.abi
new file mode 100644
index 00000000..4197b6a2
--- /dev/null
+++ b/tests/data/test-read-ctf/test-forward-undefine-type-decl.abi
@@ -0,0 +1,31 @@ 
+<abi-corpus version='2.1' path='tests/data/test-read-ctf/test-forward-undefine-type-decl.o' architecture='elf-amd-x86_64'>
+  <elf-variable-symbols>
+    <elf-symbol name='k' size='24' type='object-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
+  </elf-variable-symbols>
+  <abi-instr address-size='64' language='LANG_C'>
+    <class-decl name='key_restriction' size-in-bits='192' alignment-in-bits='64' is-struct='yes' visibility='default' id='type-id-1'>
+      <data-member access='public' layout-offset-in-bits='0'>
+        <var-decl name='check' type-id='type-id-2' visibility='default'/>
+      </data-member>
+      <data-member access='public' layout-offset-in-bits='64'>
+        <var-decl name='keytype' type-id='type-id-3' visibility='default'/>
+      </data-member>
+      <data-member access='public' layout-offset-in-bits='128'>
+        <var-decl name='keyutype' type-id='type-id-4' visibility='default'/>
+      </data-member>
+    </class-decl>
+    <typedef-decl name='key_restrict_link_func_t' type-id='type-id-5' id='type-id-2'/>
+    <pointer-type-def type-id='type-id-6' size-in-bits='64' alignment-in-bits='64' id='type-id-3'/>
+    <pointer-type-def type-id='type-id-7' size-in-bits='64' alignment-in-bits='64' id='type-id-4'/>
+    <pointer-type-def type-id='type-id-8' size-in-bits='64' alignment-in-bits='64' id='type-id-5'/>
+    <class-decl name='key_type' is-struct='yes' visibility='default' is-declaration-only='yes' id='type-id-6'/>
+    <var-decl name='k' type-id='type-id-1' mangled-name='k' visibility='default'/>
+    <function-type size-in-bits='64' alignment-in-bits='8' id='type-id-8'>
+      <parameter type-id='type-id-3'/>
+      <parameter type-id='type-id-4'/>
+      <return type-id='type-id-9'/>
+    </function-type>
+    <union-decl name='key_utype' visibility='default' is-declaration-only='yes' id='type-id-7'/>
+    <type-decl name='void' id='type-id-9'/>
+  </abi-instr>
+</abi-corpus>
diff --git a/tests/data/test-read-ctf/test-forward-undefine-type-decl.c b/tests/data/test-read-ctf/test-forward-undefine-type-decl.c
new file mode 100644
index 00000000..b6ab4896
--- /dev/null
+++ b/tests/data/test-read-ctf/test-forward-undefine-type-decl.c
@@ -0,0 +1,17 @@ 
+/* Test undefined forward declarations
+ * gcc -gctf -c test-forward-undefine-type-decl.c -o \
+ *    test-forward-undefine-type-decl.o
+ */
+
+struct key_type;
+union  key_utype;
+typedef void (*key_restrict_link_func_t)(struct key_type *type,
+                                         union key_utype *utype);
+
+struct key_restriction {
+ key_restrict_link_func_t check;
+ struct key_type *keytype;
+ union key_utype *keyutype;
+};
+
+struct key_restriction k;
diff --git a/tests/data/test-read-ctf/test-forward-undefine-type-decl.o b/tests/data/test-read-ctf/test-forward-undefine-type-decl.o
new file mode 100644
index 0000000000000000000000000000000000000000..e8adc1441c5142cd085cf4cc18356bb521ec137c
GIT binary patch
literal 1488
zcmbtUOK#La5G@bk8!1dSh|k6d5fZ@m%q+7&77+v?gxIh^2#x&d$ynph$ZkWtf+fe`
z0C0!g1g-!VfO>X!#_9=dD5<)tUe)VQwc8(`ym&gWEYf1pBU(ioB^sVv`F`vAv`JU!
z*Y}Nq&b-wQ+YP0c*g}V_Us+FBFSEW;3SX^vfn7)&@}$?`U>+n?az__2w1q8QWo6m9
z*jbEQ=X*XjccY8%qh`X&yqnkAKJM^#f6v(WS+U+L)=#YMTJMV_riEyxvRR7OsZYI9
zRI*GWIZcy%F`d<UG?i_MP9#N6(Og6epqi%m<jsprcwrMZUQq^7D!i(Uyfg`eFgXg6
z)Tx&|y$CW-@;a3l2Qp|~hr!s1<gANkMfo8pW2eqzF-vmcU?C?KQR+rShX)7y_U_@!
z*L(JO;*Q*rJsC~LV^(`t94|$gWFnVAy2l4*!TD`g^pHt3AnOE}7E8tI&NRGcySw}O
z+f3YHU>i4}1ozMi&glH}C<`LDYBCvwY-QQmx%Q+umgJ&G<c3v6F87CrBE#{>%?l}9
zK9Ez%fUl#AkMi1GmIXIKHhLfbX>TzfXYYNd?Tmgqt`BA(Z*vK-Rfjr`-<h@1<lo~8
zytlvcPy4NA!n^FtLk`S2k@qpMd@+(nNMo)6HbQYveyG9+Jq)hqYrEgW`lXwA$Y+S=
z>C|&}@XpQhp#sc(p;vvud?pupUxD~fK=9uJr1`q9z|Gz>M4uJ^E`pwz``_l=S^RH`
L|E|Y{Yx4g9(}I;l

literal 0
HcmV?d00001

diff --git a/tests/test-read-ctf.cc b/tests/test-read-ctf.cc
index 7f5381f6..6ea85c4d 100644
--- a/tests/test-read-ctf.cc
+++ b/tests/test-read-ctf.cc
@@ -268,6 +268,14 @@  static InOutSpec in_out_specs[] =
     "data/test-read-ctf/test-callback2.abi",
     "output/test-read-ctf/test-callback2.abi",
   },
+  {
+    "data/test-read-ctf/test-forward-undefine-type-decl.o",
+    "",
+    "",
+    SEQUENCE_TYPE_ID_STYLE,
+    "data/test-read-ctf/test-forward-undefine-type-decl.abi",
+    "output/test-read-ctf/test-forward-undefine-type-decl.abi",
+  },
   // This should be the last entry.
   {NULL, NULL, NULL, SEQUENCE_TYPE_ID_STYLE, NULL, NULL}
 };