libctf: print array dimensions in the right order

Message ID 20240910172835.45289-2-bruce.mcculloch@oracle.com
State New
Headers
Series libctf: print array dimensions in the right order |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Bruce McCulloch Sept. 10, 2024, 5:28 p.m. UTC
  Hi All,

This is the patch relating to bug 32161, which I just posted.
I'm mostly looking for comments on how to proceed with this, given that
applying this patch while using a version of gcc older than 14.2.0 would
result in reversing the nelems of multidimensional arrays, which work
properly with the current version of CTF and older versions of gcc.

Bruce
---
 libctf/ChangeLog                              |  7 ++++
 libctf/ctf-decl.c                             |  6 ++--
 .../libctf-lookup/multidim-array-ctf.c        |  2 ++
 .../testsuite/libctf-lookup/multidim-array.c  | 34 +++++++++++++++++++
 .../testsuite/libctf-lookup/multidim-array.lk |  8 +++++
 5 files changed, 55 insertions(+), 2 deletions(-)
 create mode 100644 libctf/testsuite/libctf-lookup/multidim-array-ctf.c
 create mode 100644 libctf/testsuite/libctf-lookup/multidim-array.c
 create mode 100644 libctf/testsuite/libctf-lookup/multidim-array.lk
  

Patch

diff --git a/libctf/ChangeLog b/libctf/ChangeLog
index 5c53be7ec7a..703890342d2 100644
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,3 +1,10 @@ 
+2024-09-09 Bruce McCulloch	<bruce.mcculloch@oracle.com>
+
+	* ctf-decl.c (ctf_decl_push):
+	* testsuite/libctf-lookup/multidim-array-ctf.c: New test.
+	* testsuite/libctf-lookup/multidim-array.c: New test.
+	* testsuite/libctf-lookup/multidim-array.lk: New test.
+
 2024-07-20  Nick Clifton  <nickc@redhat.com>
 
 	* 2.43 branch point.
diff --git a/libctf/ctf-decl.c b/libctf/ctf-decl.c
index 2fd4bde1864..772d337b1ee 100644
--- a/libctf/ctf-decl.c
+++ b/libctf/ctf-decl.c
@@ -154,9 +154,11 @@  ctf_decl_push (ctf_decl_t *cd, ctf_dict_t *fp, ctf_id_t type)
     cd->cd_qualp = prec;
 
   /* By convention qualifiers of base types precede the type specifier (e.g.
-     const int vs. int const) even though the two forms are equivalent.  */
+     const int vs. int const) even though the two forms are equivalent.
+     As of gcc-14.2.0, arrays must also be prepended in order to dump with the
+     dimensions properly ordered.  */
 
-  if (is_qual && prec == CTF_PREC_BASE)
+  if ((is_qual && prec == CTF_PREC_BASE) || (kind == CTF_K_ARRAY))
     ctf_list_prepend (&cd->cd_nodes[prec], cdp);
   else
     ctf_list_append (&cd->cd_nodes[prec], cdp);
diff --git a/libctf/testsuite/libctf-lookup/multidim-array-ctf.c b/libctf/testsuite/libctf-lookup/multidim-array-ctf.c
new file mode 100644
index 00000000000..e450e8112bd
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/multidim-array-ctf.c
@@ -0,0 +1,2 @@ 
+int a[3][5][9];
+int b[1][2];
diff --git a/libctf/testsuite/libctf-lookup/multidim-array.c b/libctf/testsuite/libctf-lookup/multidim-array.c
new file mode 100644
index 00000000000..56db6ff8507
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/multidim-array.c
@@ -0,0 +1,34 @@ 
+#include <ctf-api.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main (int argc, char *argv[])
+{
+  ctf_archive_t *ctf;
+  ctf_dict_t *fp;
+  int err;
+  ctf_dump_state_t *state = NULL;
+  ctf_sect_names_t sect = CTF_SECT_TYPE;
+  char *item;
+  ctf_next_t *it = NULL;
+  ctf_id_t type;
+
+
+  if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
+    goto open_err;
+  if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
+    goto open_err;
+
+  while ((type = ctf_type_next (fp, &it, NULL, 1)) != -1)
+    printf("%s\n", ctf_type_aname(fp, type));
+
+  ctf_dict_close (fp);
+  ctf_close (ctf);
+
+  return 0;
+
+  open_err:
+    fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
+    return 1;
+}
diff --git a/libctf/testsuite/libctf-lookup/multidim-array.lk b/libctf/testsuite/libctf-lookup/multidim-array.lk
new file mode 100644
index 00000000000..2e155d67fc6
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/multidim-array.lk
@@ -0,0 +1,8 @@ 
+# source: multidim-array-ctf.c
+int
+long unsigned int
+int \[9\]
+int \[5\]\[9\]
+int \[3\]\[5\]\[9\]
+int \[2\]
+int \[1\]\[2\]