[vala/staging] codegen: Don't write declaration of extern symbols with given header



commit 2b2bee081b9d6f786bf4ef1f97a6be9775d21c3e
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Jan 8 14:14:42 2017 +0100

    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.

 codegen/valaccodeattribute.vala  | 2 +-
 codegen/valaccodebasemodule.vala | 8 ++++----
 tests/Makefile.am                | 1 +
 tests/methods/extern.vala        | 9 +++++++++
 vala/valaenumvaluetype.vala      | 1 +
 5 files changed, 16 insertions(+), 5 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index 3740d232a..982860f58 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -799,7 +799,7 @@ public class Vala.CCodeAttribute : AttributeCache {
                                return parent_headers;
                        }
                }
-               if (sym.source_reference != null && !sym.external_package) {
+               if (sym.source_reference != null && !sym.external_package && !sym.external) {
                        // 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 394d710e1..a45cdeb9d 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -629,16 +629,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                if (sym.anonymous) {
                        return !decl_space.is_header && CodeContext.get ().use_header;
                }
-               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.external && !sym.external_package && 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.external && (!sym.external_package || (sym.external_package && 
sym.from_commandline)));
                        }
                        // declaration complete
                        return true;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b84d952a6..00ebc97ce 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -84,6 +84,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..5d33e1f84
--- /dev/null
+++ b/tests/methods/extern.vala
@@ -0,0 +1,9 @@
+[CCode (cname = "g_strdup", cheader_filename = "glib.h")]
+extern string strdup (string s);
+
+[CCode (cname = "vala_some_thing")]
+extern void some_thing ();
+
+void main () {
+       assert ("foo" == strdup ("foo"));
+}
diff --git a/vala/valaenumvaluetype.vala b/vala/valaenumvaluetype.vala
index c409763fe..ef44103ac 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.external = 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]