[vala/wip/issue/745: 4/4] codegen: Don't write declaration of extern symbols with given header



commit 70c4b16d83c90959e806cc055fb0dd65bef0cab3
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Jun 18 23:34:09 2019 +0200

    codegen: Don't write declaration of extern symbols with given header
    
    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  |  4 ++--
 codegen/valaccodebasemodule.vala |  8 ++++----
 tests/Makefile.am                |  1 +
 tests/methods/extern.vala        | 31 +++++++++++++++++++++++++++++++
 vala/valaenumvaluetype.vala      |  1 +
 5 files changed, 39 insertions(+), 6 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index 5728fb469..87d5142e8 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -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 ();
                }
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index e3d8d3b55..af1b761ca 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -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;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b67d91c5b..3439c786f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..45ee4b3f7
--- /dev/null
+++ b/tests/methods/extern.vala
@@ -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);
+}
diff --git a/vala/valaenumvaluetype.vala b/vala/valaenumvaluetype.vala
index 896371323..be8ac15d0 100644
--- a/vala/valaenumvaluetype.vala
+++ b/vala/valaenumvaluetype.vala
@@ -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);


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