]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Add G_GNUC_FORMAT attribute for FormatArg functions
authorJürg Billeter <j@bitron.ch>
Mon, 7 Nov 2016 19:23:38 +0000 (20:23 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 7 Nov 2016 20:27:36 +0000 (21:27 +0100)
https://bugzilla.gnome.org/show_bug.cgi?id=774060

ccode/valaccodefunction.vala
ccode/valaccodeparameter.vala
codegen/valaccodemethodmodule.vala
codegen/valagtypemodule.vala
tests/Makefile.am
tests/methods/bug774060.vala [new file with mode: 0644]

index 7f3b3bb12c49e89fc595be144fab4b6bd70aa1b2..44dd5b9560e52ab3e998396e50ac400558c2c87b 100644 (file)
@@ -126,16 +126,19 @@ public class Vala.CCodeFunction : CCodeNode {
                writer.write_string (name);
                writer.write_string (" (");
                
-               bool first = true;
+               int i = 0;
+               int format_arg_index = -1;
                foreach (CCodeParameter param in parameters) {
-                       if (!first) {
+                       if (i > 0) {
                                writer.write_string (", ");
-                       } else {
-                               first = false;
                        }
                        param.write (writer);
+                       if (param.format_arg) {
+                               format_arg_index = i;
+                       }
+                       i++;
                }
-               if (first) {
+               if (i == 0) {
                        writer.write_string ("void");
                }
                
@@ -146,6 +149,10 @@ public class Vala.CCodeFunction : CCodeNode {
                }
 
                if (is_declaration) {
+                       if (format_arg_index >= 0) {
+                               writer.write_string (" G_GNUC_FORMAT(%d)".printf (format_arg_index + 1));
+                       }
+
                        if (attributes != null) {
                                writer.write_string (" ");
                                writer.write_string (attributes);
index 9f1c2e1864cfd9e3f3a2ee58d701a4d41cd9a507..9404f470c49238f7dc2dbfa6a06e434aa7cc7923 100644 (file)
@@ -42,6 +42,8 @@ public class Vala.CCodeParameter : CCodeNode {
         */
        public bool ellipsis { get; set; }
 
+       public bool format_arg { get; set; }
+
        public CCodeParameter (string n, string type) {
                name = n;
                type_name = type;
index 19f507b6086d1e1e9b53e995c882aad9857c7406..b50380a9ab16cdc7ef1f397c09c8a99172819afa 100644 (file)
@@ -881,6 +881,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        }
 
                        cparam = new CCodeParameter (get_variable_cname (param.name), ctypename);
+                       cparam.format_arg = param.format_arg;
                } else if (ellipses_to_valist) {
                        cparam = new CCodeParameter ("_vala_va_list", "va_list");
                } else {
index 2407446f7dc24ace87b05a5e3d4adaba52b8bc3a..692a9bd33106938f363b0dcded6a5e58480b3072 100644 (file)
@@ -38,6 +38,7 @@ public class Vala.GTypeModule : GErrorModule {
                }
 
                var cparam = new CCodeParameter (get_variable_cname (param.name), ctypename);
+               cparam.format_arg = param.format_arg;
 
                cparam_map.set (get_param_pos (get_ccode_pos (param)), cparam);
                if (carg_map != null) {
index 363dc29bf6dc66d9d4f6c02b649c7314ed634303..cfda178d911590ef1f6fec3e9157a9f32568d50d 100644 (file)
@@ -76,6 +76,7 @@ TESTS = \
        methods/bug737222.vala \
        methods/bug743877.vala \
        methods/bug771964.vala \
+       methods/bug774060.vala \
        methods/generics.vala \
        methods/printf-invalid.test \
        methods/printf-constructor.vala \
diff --git a/tests/methods/bug774060.vala b/tests/methods/bug774060.vala
new file mode 100644 (file)
index 0000000..84ab9c6
--- /dev/null
@@ -0,0 +1,7 @@
+unowned string format_wrapper ([FormatArg] string format) {
+       return format;
+}
+
+void main () {
+       print (format_wrapper ("%d"), 42);
+}