]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Don't write declaration of extern symbols with given header 70c4b16d83c90959e806cc055fb0dd65bef0cab3 65/head
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 18 Jun 2019 21:34:09 +0000 (23:34 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 4 Jul 2019 10:18:11 +0000 (12:18 +0200)
The corresponding header will provide the declaration and therefore fix
build with -Werror=redundant-decls.

Set cheader_filename for implicit to_string() method of enums which is
transformed into symbol calls from glib-object.h. This avoids the leak of
an superfluous prototype of that method into the generated c code.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/745

codegen/valaccodeattribute.vala
codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/methods/extern.vala [new file with mode: 0644]
vala/valaenumvaluetype.vala

index 5728fb46995ba0347466ae36ffd9fff05377e3b0..87d5142e8fd648f742ab8d001491c23c62be0bed 100644 (file)
@@ -837,13 +837,13 @@ public class Vala.CCodeAttribute : AttributeCache {
                if (sym is DynamicProperty || sym is DynamicMethod) {
                        return "";
                }
-               if (sym.parent_symbol != null) {
+               if (sym.parent_symbol != null && !sym.is_extern) {
                        var parent_headers = get_ccode_header_filenames (sym.parent_symbol);
                        if (parent_headers.length > 0) {
                                return parent_headers;
                        }
                }
-               if (sym.source_reference != null && !sym.external_package) {
+               if (sym.source_reference != null && !sym.external_package && !sym.is_extern) {
                        // don't add default include directives for VAPI files
                        return sym.source_reference.file.get_cinclude_filename ();
                }
index e3d8d3b55b9bde48e9b9079fee6191c8b67e9c1a..af1b761cadb0bb14a1de32452215983b08936e47 100644 (file)
@@ -657,16 +657,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                if (sym is Constant && ((Constant) sym).value is InitializerList) {
                        return false;
                }
-               if (sym.external_package || (!decl_space.is_header && CodeContext.get ().use_header && !sym.is_internal_symbol ())) {
+               if (sym.external_package || (!decl_space.is_header && CodeContext.get ().use_header && !sym.is_internal_symbol ())
+                   || (sym.is_extern && get_ccode_header_filenames (sym).length > 0)) {
                        // add feature test macros
                        foreach (unowned string feature_test_macro in get_ccode_feature_test_macros (sym).split (",")) {
                                decl_space.add_feature_test_macro (feature_test_macro);
                        }
                        // add appropriate include file
                        foreach (unowned string header_filename in get_ccode_header_filenames (sym).split (",")) {
-                               decl_space.add_include (header_filename, !sym.external_package ||
-                                                                        (sym.external_package &&
-                                                                         sym.from_commandline));
+                               decl_space.add_include (header_filename,
+                                       !sym.is_extern && (!sym.external_package || (sym.external_package && sym.from_commandline)));
                        }
                        // declaration complete
                        return true;
index b67d91c5b9d9207e06b1a79dc7d30e6138949ee2..3439c786facf5b6ac991a40891e6588f37fbc118 100644 (file)
@@ -98,6 +98,7 @@ TESTS = \
        methods/lambda.vala \
        methods/closures.vala \
        methods/contains.vala \
+       methods/extern.vala \
        methods/iterator.vala \
        methods/prepostconditions.vala \
        methods/same-name.vala \
diff --git a/tests/methods/extern.vala b/tests/methods/extern.vala
new file mode 100644 (file)
index 0000000..45ee4b3
--- /dev/null
@@ -0,0 +1,31 @@
+[CCode (cname = "g_strdup", cheader_filename = "glib.h")]
+extern string strdup (string s);
+
+[CCode (cname = "vala_some_thing")]
+extern void some_thing ();
+
+namespace Foo {
+       [CCode (cname = "g_strdup", cheader_filename = "glib.h")]
+       extern string strdup (string s);
+
+       [CCode (cname = "vala_some_thing")]
+       extern void some_thing ();
+}
+
+public class Bar {
+       [CCode (cname = "g_strdup", cheader_filename = "glib.h")]
+       public static extern string strdup (string s);
+
+       [CCode (cname = "vala_some_thing")]
+       public static extern void some_thing ();
+}
+
+void main () {
+       assert ("foo" == strdup ("foo"));
+       assert ("foo" == Foo.strdup ("foo"));
+       assert ("foo" == Bar.strdup ("foo"));
+
+       assert (some_thing != null);
+       assert (Foo.some_thing != null);
+       assert (Bar.some_thing != null);
+}
index 89637132332ff461a786e70aa542bf09e0a040cd..be8ac15d0ac8ee3012f5db9c66f9cfc192af7a84 100644 (file)
@@ -48,6 +48,7 @@ public class Vala.EnumValueType : ValueType {
                        to_string_method = new Method ("to_string", string_type);
                        to_string_method.access = SymbolAccessibility.PUBLIC;
                        to_string_method.is_extern = true;
+                       to_string_method.set_attribute_string ("CCode", "cheader_filename", "glib-object.h");
                        to_string_method.owner = type_symbol.scope;
                        to_string_method.this_parameter = new Parameter ("this", this);
                        to_string_method.scope.add (to_string_method.this_parameter.name, to_string_method.this_parameter);