[vala/staging] codegen: Avoid passing a pointer to sizeof() if is not intended



commit fa7361187cedcaa953facdab822981100a3e6ffa
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Mar 8 13:57:29 2018 +0100

    codegen: Avoid passing a pointer to sizeof() if is not intended
    
    https://bugzilla.gnome.org/show_bug.cgi?id=728274

 codegen/valaccode.vala           |    4 +++
 codegen/valaccodeattribute.vala  |   36 ++++++++++++++++++++++++++++++++
 codegen/valaccodebasemodule.vala |   20 +++++++++++++++++-
 tests/Makefile.am                |    1 +
 tests/objects/bug728274.vala     |   42 ++++++++++++++++++++++++++++++++++++++
 vala/valausedattr.vala           |    2 +-
 6 files changed, 103 insertions(+), 2 deletions(-)
---
diff --git a/codegen/valaccode.vala b/codegen/valaccode.vala
index 85d7f43..e048a99 100644
--- a/codegen/valaccode.vala
+++ b/codegen/valaccode.vala
@@ -44,6 +44,10 @@ namespace Vala {
                return get_ccode_attribute(node).const_name;
        }
 
+       public static string get_ccode_struct_name (CodeNode node) {
+               return get_ccode_attribute(node).struct_name;
+       }
+
        public static string get_ccode_type_name (Interface iface) {
                return get_ccode_attribute(iface).type_name;
        }
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index d7fb4b0..432b6af 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -57,6 +57,20 @@ public class Vala.CCodeAttribute : AttributeCache {
                }
        }
 
+       public string struct_name {
+               get {
+                       if (_struct_name == null) {
+                               if (ccode != null) {
+                                       _struct_name = ccode.get_string ("struct_cname");
+                               }
+                               if (_struct_name == null) {
+                                       _struct_name = get_default_struct_name ();
+                               }
+                       }
+                       return _struct_name;
+               }
+       }
+
        public string type_name {
                get {
                        if (_type_name == null) {
@@ -558,6 +572,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 
        private string _name;
        private string _const_name;
+       private string _struct_name;
        private string _type_name;
        private string _feature_test_macros;
        private string _header_filenames;
@@ -753,6 +768,27 @@ public class Vala.CCodeAttribute : AttributeCache {
                }
        }
 
+       private string get_default_struct_name () {
+               unowned DataType? data_type = node as DataType;
+
+               if (data_type is ObjectType) {
+                       return get_ccode_name (((ObjectType) data_type).type_symbol);
+               } else if (data_type is ErrorType) {
+                       return "GError";
+               } else if (data_type is ClassType) {
+                       return "%sClass".printf (get_ccode_name (((ClassType) data_type).class_symbol));
+               } else if (data_type is InterfaceType) {
+                       return get_ccode_type_name (((InterfaceType) data_type).interface_symbol);
+               } else if (data_type is ValueType && !data_type.nullable) {
+                       return get_ccode_name (((ValueType) data_type).type_symbol);
+               } else if (node is CType) {
+                       return ((CType) node).ctype_name;
+               }
+
+               Report.error (node.source_reference, "internal error: `%s' doesn't have a struct name".printf 
(node.type_name));
+               return "";
+       }
+
        private string get_default_header_filenames () {
                if (sym is DynamicProperty || sym is DynamicMethod) {
                        return "";
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index ffd773e..180a975 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -4978,8 +4978,26 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public override void visit_sizeof_expression (SizeofExpression expr) {
                generate_type_declaration (expr.type_reference, cfile);
 
+               string struct_name;
+               unowned DataType data_type = expr.type_reference;
+               if (data_type is PointerType) {
+                       if (!(((PointerType) data_type).base_type is VoidType)) {
+                               Report.warning (expr.source_reference, "Passing a pointer-type to sizeof() 
defaults to sizeof(void*)");
+                       }
+                       struct_name = "void*";
+               } else if (data_type is ValueType && data_type.nullable) {
+                       // boxed-types are pointers too
+                       Report.warning (expr.source_reference, "Passing a boxed-type to sizeof() defaults to 
sizeof(void*)");
+                       struct_name = "void*";
+               } else if (data_type is GenericType) {
+                       // generic-types are represented by gpointer
+                       struct_name = "void*";
+               } else {
+                       struct_name = get_ccode_struct_name (data_type);
+               }
+
                var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
-               csizeof.add_argument (new CCodeIdentifier (get_ccode_name (expr.type_reference)));
+               csizeof.add_argument (new CCodeIdentifier (struct_name));
                set_cvalue (expr, csizeof);
        }
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f0f49ce..0a9461f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -286,6 +286,7 @@ TESTS = \
        objects/bug701978.vala \
        objects/bug702736.vala \
        objects/bug702846.vala \
+       objects/bug728274.vala \
        objects/bug731547.vala \
        objects/bug741465.vala \
        objects/bug751338.vala \
diff --git a/tests/objects/bug728274.vala b/tests/objects/bug728274.vala
new file mode 100644
index 0000000..78dcdb2
--- /dev/null
+++ b/tests/objects/bug728274.vala
@@ -0,0 +1,42 @@
+class Foo {
+}
+
+[Compact]
+class FooCompact {
+       public int i;
+}
+
+class FooObject : Object {
+}
+
+struct Bar {
+       public int i;
+}
+
+enum Manam {
+       NONE;
+}
+
+errordomain FooError {
+       FAIL;
+}
+
+void generic<G> (G bar) {
+       assert (sizeof (G) == sizeof (void*));
+}
+
+void main () {
+       assert (sizeof (Foo) >= sizeof (GLib.TypeInstance) + sizeof (int) + sizeof (void*));
+       assert (sizeof (FooCompact) == sizeof (int));
+       assert (sizeof (FooObject) == sizeof (GLib.Object) + sizeof (void*));
+
+       assert (sizeof (Bar) == sizeof (int));
+       assert (sizeof (Manam) == sizeof (uint));
+
+       assert (sizeof (FooError) == sizeof (GLib.Quark) + sizeof (int) + sizeof (void*));
+
+       assert (sizeof (Foo*) == sizeof (void*));
+       assert (sizeof (int?) == sizeof (void*));
+
+       generic<Foo> (new Foo ());
+}
diff --git a/vala/valausedattr.vala b/vala/valausedattr.vala
index 4a76e2d..6e15289 100644
--- a/vala/valausedattr.vala
+++ b/vala/valausedattr.vala
@@ -31,7 +31,7 @@ public class Vala.UsedAttr : CodeVisitor {
 
        const string[] valac_default_attrs = {
                "CCode", "type_signature", "default_value", "set_value_function", "type_id", "cprefix", 
"cheader_filename",
-               "marshaller_type_name", "get_value_function", "cname", "destroy_function", "lvalue_access",
+               "marshaller_type_name", "get_value_function", "cname", "struct_cname", "destroy_function", 
"lvalue_access",
                "has_type_id", "instance_pos", "const_cname", "take_value_function", "copy_function", 
"free_function",
                "param_spec_function", "has_target", "type_cname", "ref_function", "ref_function_void", 
"unref_function", "type",
                "has_construct_function", "returns_floating_reference", "gir_namespace", "gir_version", 
"construct_function",


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