[vala/0.46] girwriter: Don't include symbols outside of a namespace



commit 40ebf5371bc0c9a89284899df5f670e00b75127b
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Jul 27 18:10:38 2020 +0200

    girwriter: Don't include symbols outside of a namespace
    
    Additionally report a warning for symbols which doesn't fulfill this rule.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/241

 codegen/valaccodearraymodule.vala     |  2 +-
 codegen/valagirwriter.vala            | 45 +++++++++++++++++++++++++++++++++++
 tests/girwriter/girtest.vala          | 27 +++++++++++++++++++++
 tests/girwriter/girtest.vapi-expected | 27 +++++++++++++++++++++
 4 files changed, 100 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 7a9386219..f89433ce1 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -497,7 +497,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
                unowned ArrayType? array_type = value.value_type as ArrayType;
 
                if (array_type != null && array_type.fixed_length) {
-                       unowned Struct? st = array_type.element_type.type_symbol as Struct;
+                       unowned Struct? st = array_type.element_type.data_type as Struct;
                        if (st != null && !array_type.element_type.nullable) {
                                var ccall = new CCodeFunctionCall (new CCodeIdentifier 
(append_struct_array_destroy (st)));
                                ccall.add_argument (get_cvalue_ (value));
diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala
index 0cf6f6bb5..9c98be05d 100644
--- a/codegen/valagirwriter.vala
+++ b/codegen/valagirwriter.vala
@@ -392,6 +392,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (cl)) {
+                       return;
+               }
+
                if (!(hierarchy[0] is Namespace)) {
                        deferred.add (cl);
                        return;
@@ -591,6 +595,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (st)) {
+                       return;
+               }
+
                if (!(hierarchy[0] is Namespace)) {
                        deferred.add (st);
                        return;
@@ -629,6 +637,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (iface)) {
+                       return;
+               }
+
                if (!(hierarchy[0] is Namespace)) {
                        deferred.add (iface);
                        return;
@@ -794,6 +806,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (en)) {
+                       return;
+               }
+
                if (!(hierarchy[0] is Namespace)) {
                        deferred.add (en);
                        return;
@@ -868,6 +884,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (edomain)) {
+                       return;
+               }
+
                write_indent ();
                buffer.append_printf ("<enumeration name=\"%s\"", edomain.name);
                write_ctype_attributes (edomain);
@@ -925,6 +945,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (c)) {
+                       return;
+               }
+
                //TODO Add better constant evaluation
                var initializer = c.value;
                string value = literal_expression_to_value_string (initializer);
@@ -954,6 +978,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (f)) {
+                       return;
+               }
+
                write_indent ();
                buffer.append_printf ("<field name=\"%s\"", get_ccode_name (f));
                if (f.variable_type.nullable) {
@@ -1175,6 +1203,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (cb)) {
+                       return;
+               }
+
                write_indent ();
                buffer.append_printf ("<callback name=\"%s\"", cb.name);
                buffer.append_printf (" c:type=\"%s\"", get_ccode_name (cb));
@@ -1204,6 +1236,10 @@ public class Vala.GIRWriter : CodeVisitor {
                        return;
                }
 
+               if (!has_namespace (m)) {
+                       return;
+               }
+
                string tag_name = "method";
                var parent = this.hierarchy.get (0);
                if (parent is Enum) {
@@ -1711,4 +1747,13 @@ public class Vala.GIRWriter : CodeVisitor {
        private bool is_visibility (Symbol sym) {
                return sym.get_attribute_bool ("GIR", "visible", true);
        }
+
+       bool has_namespace (Symbol sym) {
+               if (!(sym.parent_symbol is Namespace) || sym.parent_symbol.name != null) {
+                       return true;
+               }
+
+               Report.warning (sym.source_reference, "`%s' must be part of namespace to be included in 
GIR".printf (sym.name));
+               return false;
+       }
 }
diff --git a/tests/girwriter/girtest.vala b/tests/girwriter/girtest.vala
index a63b6d4c3..9aeeacc4a 100644
--- a/tests/girwriter/girtest.vala
+++ b/tests/girwriter/girtest.vala
@@ -320,3 +320,30 @@ namespace GirTest {
                }
        }
 }
+
+public enum InvalidEnum {
+       VALUE
+}
+
+public errordomain InvalidError {
+       FAILED
+}
+
+public class InvalidClass {
+}
+
+public interface InvalidIface {
+}
+
+public struct InvalidStruct {
+       public int i;
+}
+
+public delegate void InvalidFunc ();
+
+public const int INVALID_CONST = 0;
+
+public int invalid_field;
+
+public void invalid_method () {
+}
diff --git a/tests/girwriter/girtest.vapi-expected b/tests/girwriter/girtest.vapi-expected
index b09de120d..23ca2a8bd 100644
--- a/tests/girwriter/girtest.vapi-expected
+++ b/tests/girwriter/girtest.vapi-expected
@@ -192,3 +192,30 @@ namespace GirTest {
        [CCode (cheader_filename = "girtest.h")]
        public const string CONSTANT_STRING;
 }
+[CCode (cheader_filename = "girtest.h")]
+public enum InvalidEnum {
+       VALUE
+}
+[CCode (cheader_filename = "girtest.h")]
+public errordomain InvalidError {
+       FAILED
+}
+[CCode (cheader_filename = "girtest.h")]
+public class InvalidClass {
+       public InvalidClass ();
+}
+[CCode (cheader_filename = "girtest.h")]
+public interface InvalidIface {
+}
+[CCode (cheader_filename = "girtest.h")]
+public struct InvalidStruct {
+       public int i;
+}
+[CCode (cheader_filename = "girtest.h")]
+public delegate void InvalidFunc ();
+[CCode (cheader_filename = "girtest.h")]
+public const int INVALID_CONST;
+[CCode (cheader_filename = "girtest.h")]
+public static int invalid_field;
+[CCode (cheader_filename = "girtest.h")]
+public static void invalid_method ();


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