]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
girparser: Don't blindly translate utf8 to string and check the ctype too
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 26 Nov 2023 12:23:18 +0000 (13:23 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 26 Nov 2023 14:40:18 +0000 (15:40 +0100)
This fixes array out-parameters with ctype "char**" and array return values
with ctype "char*".

tests/Makefile.am
tests/gir/method-array-return.gir [new file with mode: 0644]
tests/gir/method-array-return.vapi-expected [new file with mode: 0644]
tests/gir/parameter-array-out.gir [new file with mode: 0644]
tests/gir/parameter-array-out.vapi-expected [new file with mode: 0644]
vala/valagirparser.vala

index 2fb805f8960607103c361a8f9f21ec16a1c522c2..0b4a8396d1fd7279a9af97b231e3aeb0cfdae199 100644 (file)
@@ -899,9 +899,11 @@ TESTS = \
        gir/gtype-struct-name.gir \
        gir/instance-parameter-owned.gir \
        gir/method-array-length-type.gir \
+       gir/method-array-return.gir \
        gir/method-class.gir \
        gir/method-nowrapper.gir \
        gir/parameter-array-length-type.gir \
+       gir/parameter-array-out.gir \
        gir/parameter-nullable-out-simple-type.gir \
        gir/property-non-readable.gir \
        gir/signal-virtual.gir \
diff --git a/tests/gir/method-array-return.gir b/tests/gir/method-array-return.gir
new file mode 100644 (file)
index 0000000..3c961aa
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<repository version="1.2" xmlns="http://www.gtk.org/introspection/core/1.0" xmlns:c="http://www.gtk.org/introspection/c/1.0" xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
+<include name="GObject" version="2.0"/>
+<include name="GLib" version="2.0"/>
+<include name="Gio" version="2.0"/>
+<package name="test"/>
+<c:include name="test.h"/>
+<namespace name="Test" version="1.0" shared-library="test" c:prefix="Test" c:identifier-prefixes="Test" c:symbol-prefixes="test">
+<function name="function" c:identifier="test_function">
+  <return-value transfer-ownership="full">
+    <array length="0" zero-terminated="0" c:type="char*">
+      <type name="utf8" c:type="char"/>
+    </array>
+  </return-value>
+  <parameters>
+    <parameter name="size" direction="out" transfer-ownership="full">
+      <type name="guint" c:type="unsigned int*"/>
+    </parameter>
+  </parameters>
+</function>
+</namespace>
+</repository>
diff --git a/tests/gir/method-array-return.vapi-expected b/tests/gir/method-array-return.vapi-expected
new file mode 100644 (file)
index 0000000..a9c9ef8
--- /dev/null
@@ -0,0 +1,5 @@
+[CCode (cprefix = "Test", gir_namespace = "Test", gir_version = "1.0", lower_case_cprefix = "test_")]
+namespace Test {
+       [CCode (array_length_pos = 0.1, array_length_type = "guint", cheader_filename = "test.h")]
+       public static char[] function ();
+}
diff --git a/tests/gir/parameter-array-out.gir b/tests/gir/parameter-array-out.gir
new file mode 100644 (file)
index 0000000..c485756
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0"?>
+<repository version="1.2" xmlns="http://www.gtk.org/introspection/core/1.0" xmlns:c="http://www.gtk.org/introspection/c/1.0" xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
+<include name="GObject" version="2.0"/>
+<include name="GLib" version="2.0"/>
+<include name="Gio" version="2.0"/>
+<package name="test"/>
+<c:include name="test.h"/>
+<namespace name="Test" version="1.0" shared-library="test" c:prefix="Test" c:identifier-prefixes="Test" c:symbol-prefixes="test">
+<function name="function" c:identifier="test_function">
+  <return-value transfer-ownership="none">
+    <type name="none"/>
+  </return-value>
+  <parameters>
+    <parameter name="size" direction="out" transfer-ownership="full">
+      <type name="guint" c:type="unsigned int*"/>
+    </parameter>
+    <parameter name="text" direction="out" transfer-ownership="full">
+      <array length="0" zero-terminated="0" c:type="char*">
+        <type name="utf8" c:type="char"/>
+      </array>
+    </parameter>
+  </parameters>
+</function>
+</namespace>
+</repository>
diff --git a/tests/gir/parameter-array-out.vapi-expected b/tests/gir/parameter-array-out.vapi-expected
new file mode 100644 (file)
index 0000000..ea15a47
--- /dev/null
@@ -0,0 +1,5 @@
+[CCode (cprefix = "Test", gir_namespace = "Test", gir_version = "1.0", lower_case_cprefix = "test_")]
+namespace Test {
+       [CCode (cheader_filename = "test.h")]
+       public static void function ([CCode (array_length_cname = "size", array_length_pos = 0.5, array_length_type = "guint")] out char[] text);
+}
index 3f5cc8424b89eef62ce3c6cf79efa0c07243d9e1..ed685cd7f11f46ec5f2a1222f600b836db2f36bf 100644 (file)
@@ -2870,7 +2870,12 @@ public class Vala.GirParser : CodeVisitor {
                } else {
                        bool known_type = true;
                        if (type_name == "utf8") {
-                               type_name = "string";
+                               if (ctype == null || ctype.has_suffix ("*") || ctype == "gpointer" || ctype == "gconstpointer") {
+                                       type_name = "string";
+                               } else {
+                                       //FIXME Work around a g-ir-scanner bug
+                                       type_name = "char";
+                               }
                        } else if (type_name == "gboolean") {
                                type = new BooleanType ((Struct) context.root.scope.lookup ("bool"));
                        } else if (type_name == "gchar") {