[vala/0.36] codegen: Prioritize "array_length=true" over "array_null_terminated=true"



commit 6e78d05a07793adf262016ed37be68cfe5024eb3
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Oct 10 14:09:10 2017 +0200

    codegen: Prioritize "array_length=true" over "array_null_terminated=true"
    
    If arrays claim to have an array-length and also are null-terminated then
    use and rely on the given length.
    
    Adapt the girparser for this and add "array_length = true" accordingly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=788775

 codegen/valaccodeattribute.vala  |    5 ++++-
 tests/Makefile.am                |    2 ++
 tests/basic-types/bug788775.vala |   10 ++++++++++
 tests/gir/bug788775.test         |   29 +++++++++++++++++++++++++++++
 vala/valagirparser.vala          |   18 +++++++++---------
 vapi/glib-2.0.vapi               |    2 +-
 6 files changed, 55 insertions(+), 11 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index d3a97d5..69867fd 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -508,7 +508,10 @@ public class Vala.CCodeAttribute : AttributeCache {
        public bool array_null_terminated {
                get {
                        if (_array_null_terminated == null) {
-                               if (ccode != null && ccode.has_argument ("array_null_terminated")) {
+                               // If arrays claim to have an array-length and also are null-terminated then 
rely on the given length
+                               if (ccode != null && ccode.has_argument ("array_length") && ccode.get_bool 
("array_length")) {
+                                       _array_null_terminated = false;
+                               } else if (ccode != null && ccode.has_argument ("array_null_terminated")) {
                                        _array_null_terminated = ccode.get_bool ("array_null_terminated");
                                } else {
                                        _array_null_terminated = get_default_array_null_terminated ();
diff --git a/tests/Makefile.am b/tests/Makefile.am
index dcd7579..3f9f0c7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -47,6 +47,7 @@ TESTS = \
        basic-types/bug772426.vala \
        basic-types/bug777697.test \
        basic-types/bug787152.vala \
+       basic-types/bug788775.vala \
        chainup/class-base.vala \
        chainup/class-base-foo.vala \
        chainup/class-object.vala \
@@ -295,6 +296,7 @@ TESTS = \
        gir/bug651773.test \
        gir/bug667751.test \
        gir/bug742012.test \
+       gir/bug788775.test \
        gir/array-fixed-length.test \
        gir/delegate-alias-without-target.test \
        annotations/deprecated.vala \
diff --git a/tests/basic-types/bug788775.vala b/tests/basic-types/bug788775.vala
new file mode 100644
index 0000000..bd92a94
--- /dev/null
+++ b/tests/basic-types/bug788775.vala
@@ -0,0 +1,10 @@
+void test_compile_only () {
+       try {
+               var file = new KeyFile ();
+               var list = file.get_string_list ("foo", "bar");
+       } catch (KeyFileError e) {
+       }
+}
+
+void main () {
+}
diff --git a/tests/gir/bug788775.test b/tests/gir/bug788775.test
new file mode 100644
index 0000000..80ae7cd
--- /dev/null
+++ b/tests/gir/bug788775.test
@@ -0,0 +1,29 @@
+GIR
+
+Input:
+
+      <function name="get_string_list" c:identifier="test_get_string_list">
+        <return-value transfer-ownership="full">
+          <array length="1" zero-terminated="1" c:type="gchar**">
+            <type name="utf8"/>
+          </array>
+        </return-value>
+        <parameters>
+          <parameter name="key" transfer-ownership="none">
+            <type name="utf8" c:type="const gchar*"/>
+          </parameter>
+          <parameter name="length"
+                     direction="out"
+                     caller-allocates="0"
+                     transfer-ownership="full"
+                     optional="1"
+                     allow-none="1">
+            <type name="gsize" c:type="gsize*"/>
+          </parameter>
+        </parameters>
+      </function>
+
+Output:
+
+[CCode (array_length = true, array_length_pos = 1.1, array_length_type = "gsize", array_null_terminated = 
true, cheader_filename = "test.h")]
+public static string[] get_string_list (string key);
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index faaf4b1..fb811c8 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -2503,8 +2503,8 @@ public class Vala.GirParser : CodeVisitor {
                                if (metadata.has_argument (ArgumentType.ARRAY_LENGTH_IDX)) {
                                        array_length_idx = metadata.get_integer 
(ArgumentType.ARRAY_LENGTH_IDX);
                                } else {
-                                       if (no_array_length) {
-                                               param.set_attribute_bool ("CCode", "array_length", false);
+                                       if (no_array_length || array_null_terminated) {
+                                               param.set_attribute_bool ("CCode", "array_length", 
!no_array_length);
                                        }
                                        if (array_null_terminated) {
                                                param.set_attribute_bool ("CCode", "array_null_terminated", 
array_null_terminated);
@@ -2937,11 +2937,11 @@ public class Vala.GirParser : CodeVisitor {
                        field.set_attribute_string ("CCode", "cname", cname);
                }
                if (type is ArrayType) {
-                       if (!no_array_length && !array_null_terminated && array_length_idx > -1) {
+                       if (!no_array_length && array_length_idx > -1) {
                                current.array_length_idx = array_length_idx;
                        }
-                       if (no_array_length) {
-                               field.set_attribute_bool ("CCode", "array_length", false);
+                       if (no_array_length || array_null_terminated) {
+                               field.set_attribute_bool ("CCode", "array_length", !no_array_length);
                        }
                        if (array_null_terminated) {
                                field.set_attribute_bool ("CCode", "array_null_terminated", true);
@@ -2975,8 +2975,8 @@ public class Vala.GirParser : CodeVisitor {
                prop.access = SymbolAccessibility.PUBLIC;
                prop.external = true;
                prop.is_abstract = is_abstract;
-               if (no_array_length) {
-                       prop.set_attribute_bool ("CCode", "array_length", false);
+               if (no_array_length || array_null_terminated) {
+                       prop.set_attribute_bool ("CCode", "array_length", !no_array_length);
                }
                if (array_null_terminated) {
                        prop.set_attribute_bool ("CCode", "array_null_terminated", true);
@@ -3158,8 +3158,8 @@ public class Vala.GirParser : CodeVisitor {
                if (return_type is ArrayType && metadata.has_argument (ArgumentType.ARRAY_LENGTH_IDX)) {
                        return_array_length_idx = metadata.get_integer (ArgumentType.ARRAY_LENGTH_IDX);
                } else {
-                       if (return_no_array_length) {
-                               s.set_attribute_bool ("CCode", "array_length", false);
+                       if (return_no_array_length || return_array_null_terminated) {
+                               s.set_attribute_bool ("CCode", "array_length", !return_no_array_length);
                        }
                        if (return_array_null_terminated) {
                                s.set_attribute_bool ("CCode", "array_null_terminated", true);
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index a94719a..45b330b 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -4116,7 +4116,7 @@ namespace GLib {
                public uint64 get_uint64 (string group_name, string key) throws KeyFileError;
                [Version (since = "2.12")]
                public double get_double (string group_name, string key) throws KeyFileError;
-               [CCode (array_length_type = "gsize")]
+               [CCode (array_length = true, array_length_type = "gsize", array_null_terminated = true)]
                public string[] get_string_list (string group_name, string key) throws KeyFileError;
                [CCode (array_length_type = "gsize")]
                public string[] get_locale_string_list (string group_name, string key, string? locale = null) 
throws KeyFileError;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]