]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Add support for feature test macros
authorDr. Michael Lauer <mickey@vanille-media.de>
Tue, 20 Feb 2018 15:47:34 +0000 (16:47 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 20 Feb 2018 21:16:19 +0000 (22:16 +0100)
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
ccode/valaccodefeaturetestmacro.vala [new file with mode: 0644]
ccode/valaccodefile.vala
codegen/valaccode.vala
codegen/valaccodeattribute.vala
codegen/valaccodebasemodule.vala
vala/valausedattr.vala

index 0f86b96468471ce52f9d3fbf0f4e64ebc3d0d8b8..3518c82fd97528d18827200930ffc0279d9beaac 100644 (file)
@@ -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 (file)
index 0000000..d18f28d
--- /dev/null
@@ -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 ();
+       }
+}
index 128cd393830183c1c94635eb9196df56a829d8d2..aa5be219e13a7beeaca84b6d455dc9c6eb3d6634 100644 (file)
 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,13 @@ 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));
+                       features.add (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 +142,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);
index e84f0e5c9e8d719f0124bc704dff3d52ee9518a6..85d7f4393cb7e4070ff2dc9d99b2c9c605a2646c 100644 (file)
@@ -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;
        }
index 79a6933d0c83f5b391a8120ecd14d956bbbc0b97..d7fb4b0baeaf3a684358fa2209f2264b9fedba90 100644 (file)
@@ -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;
index 9935a4a496a7a89a59e9738f0408ad2b70e0cc92..ffd773ed746f541ef4efc7b0c47eb3655f6aebec 100644 (file)
@@ -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 ||
index 9b0a859577e83146b45f2336bde051e0b1bc6a04..4a76e2df442d6867f8f66a4c4a0ec9eae6f2492d 100644 (file)
@@ -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", "",