[vala/staging] codegen: Add support for feature test macros



commit 345b08b35281a1468b10c96eea7af728a7bac8eb
Author: Dr. Michael Lauer <mickey vanille-media de>
Date:   Tue Feb 20 16:47:34 2018 +0100

    codegen: Add support for feature test macros
    
    This adds new CCode string attribute 'feature_test_macro = "VALUE"'. Such
    values will be added before the headers section as '#define VALUE'.
    
    https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
    
    https://bugzilla.gnome.org/show_bug.cgi?id=793444

 ccode/Makefile.am                    |    1 +
 ccode/valaccodefeaturetestmacro.vala |   44 ++++++++++++++++++++++++++++++++++
 ccode/valaccodefile.vala             |   10 +++++++
 codegen/valaccode.vala               |    4 +++
 codegen/valaccodeattribute.vala      |   15 +++++++++++
 codegen/valaccodebasemodule.vala     |    4 +++
 vala/valausedattr.vala               |    2 +-
 7 files changed, 79 insertions(+), 1 deletions(-)
---
diff --git a/ccode/Makefile.am b/ccode/Makefile.am
index 0f86b96..3518c82 100644
--- a/ccode/Makefile.am
+++ b/ccode/Makefile.am
@@ -34,6 +34,7 @@ libvalaccode_la_VALASOURCES = \
        valaccodeenumvalue.vala \
        valaccodeexpression.vala \
        valaccodeexpressionstatement.vala \
+       valaccodefeaturetestmacro.vala \
        valaccodefile.vala \
        valaccodeforstatement.vala \
        valaccodefragment.vala \
diff --git a/ccode/valaccodefeaturetestmacro.vala b/ccode/valaccodefeaturetestmacro.vala
new file mode 100644
index 0000000..d18f28d
--- /dev/null
+++ b/ccode/valaccodefeaturetestmacro.vala
@@ -0,0 +1,44 @@
+/* valaccodefeaturetestmacro.vala
+ *
+ * Copyright (C) 2018  Dr. Michael 'Mickey' Lauer
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Dr. Michael 'Mickey' Lauer <mickey vanille-media de>
+ */
+
+using GLib;
+
+/**
+ * Represents a feature test macro definition in the C code.
+ */
+public class Vala.CCodeFeatureTestMacro : CCodeNode {
+       /**
+        * The name of this macro.
+        */
+       public string name { get; set; }
+
+       public CCodeFeatureTestMacro (string name) {
+               this.name = name;
+       }
+
+       public override void write (CCodeWriter writer) {
+               writer.write_indent ();
+               writer.write_string ("#define ");
+               writer.write_string (name);
+               writer.write_newline ();
+       }
+}
diff --git a/ccode/valaccodefile.vala b/ccode/valaccodefile.vala
index 128cd39..fbad369 100644
--- a/ccode/valaccodefile.vala
+++ b/ccode/valaccodefile.vala
@@ -24,9 +24,11 @@
 public class Vala.CCodeFile {
        public bool is_header { get; set; }
 
+       Set<string> features = new HashSet<string> (str_hash, str_equal);
        Set<string> declarations = new HashSet<string> (str_hash, str_equal);
        Set<string> includes = new HashSet<string> (str_hash, str_equal);
        CCodeFragment comments = new CCodeFragment ();
+       CCodeFragment feature_test_macros = new CCodeFragment ();
        CCodeFragment include_directives = new CCodeFragment ();
        CCodeFragment type_declaration = new CCodeFragment ();
        CCodeFragment type_definition = new CCodeFragment ();
@@ -46,6 +48,12 @@ public class Vala.CCodeFile {
                comments.append (comment);
        }
 
+       public void add_feature_test_macro (string feature_test_macro) {
+               if (!(feature_test_macro in features)) {
+                       feature_test_macros.append (new CCodeFeatureTestMacro (feature_test_macro));
+               }
+       }
+
        public void add_include (string filename, bool local = false) {
                if (!(filename in includes)) {
                        include_directives.append (new CCodeIncludeDirective (filename, local));
@@ -133,6 +141,8 @@ public class Vala.CCodeFile {
 
                        comments.write (writer);
                        writer.write_newline ();
+                       feature_test_macros.write (writer);
+                       writer.write_newline ();
                        include_directives.write (writer);
                        writer.write_newline ();
                        type_declaration.write_combined (writer);
diff --git a/codegen/valaccode.vala b/codegen/valaccode.vala
index e84f0e5..85d7f43 100644
--- a/codegen/valaccode.vala
+++ b/codegen/valaccode.vala
@@ -104,6 +104,10 @@ namespace Vala {
                return get_ccode_attribute(sym).header_filenames;
        }
 
+       public static string get_ccode_feature_test_macros (Symbol sym) {
+               return get_ccode_attribute(sym).feature_test_macros;
+       }
+
        public static string get_ccode_prefix (Symbol sym) {
                return get_ccode_attribute(sym).prefix;
        }
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index 79a6933..d7fb4b0 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -71,6 +71,20 @@ public class Vala.CCodeAttribute : AttributeCache {
                }
        }
 
+       public string feature_test_macros {
+               get {
+                       if (_feature_test_macros == null) {
+                               if (ccode != null) {
+                                       _feature_test_macros = ccode.get_string ("feature_test_macro");
+                               }
+                               if (_feature_test_macros == null) {
+                                       _feature_test_macros = "";
+                               }
+                       }
+                       return _feature_test_macros;
+               }
+       }
+
        public string header_filenames {
                get {
                        if (_header_filenames == null) {
@@ -545,6 +559,7 @@ public class Vala.CCodeAttribute : AttributeCache {
        private string _name;
        private string _const_name;
        private string _type_name;
+       private string _feature_test_macros;
        private string _header_filenames;
        private string _prefix;
        private string _lower_case_prefix;
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 9935a4a..ffd773e 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -617,6 +617,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        sym.source_reference.file.used = true;
                }
                if (sym.external_package || (!decl_space.is_header && CodeContext.get ().use_header && 
!sym.is_internal_symbol ())) {
+                       // 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 ||
diff --git a/vala/valausedattr.vala b/vala/valausedattr.vala
index 9b0a859..4a76e2d 100644
--- a/vala/valausedattr.vala
+++ b/vala/valausedattr.vala
@@ -40,7 +40,7 @@ public class Vala.UsedAttr : CodeVisitor {
                "array_length_type", "array_length", "array_length_cname", "array_length_cexpr", 
"array_null_terminated",
                "vfunc_name", "finish_vfunc_name", "finish_name", "free_function_address_of", "pos", 
"delegate_target", "delegate_target_cname",
                "array_length_pos", "delegate_target_pos", "destroy_notify_pos", "ctype", "has_new_function", 
"notify", "finish_instance",
-               "use_inplace", "",
+               "use_inplace", "feature_test_macro", "",
 
                "Immutable", "",
                "Compact", "",


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