[vala/wip/issue/442: 1/2] codegen: Don't pass a pointer to sizeof() if is not intended
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/issue/442: 1/2] codegen: Don't pass a pointer to sizeof() if is not intended
- Date: Wed, 1 Dec 2021 20:42:22 +0000 (UTC)
commit ce21d0bf8de2f0876c2c8e86ce7d94e69ddda78a
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Mar 22 08:33:16 2021 +0100
codegen: Don't pass a pointer to sizeof() if is not intended
Fixes https://gitlab.gnome.org/GNOME/vala/issues/442
codegen/valaccode.vala | 19 +++++++++++++++++++
codegen/valaccodebasemodule.vala | 17 ++++++++++++++++-
tests/Makefile.am | 1 +
tests/objects/bug728274.vala | 36 ++++++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccode.vala b/codegen/valaccode.vala
index 9b1da33f7..6663364c2 100644
--- a/codegen/valaccode.vala
+++ b/codegen/valaccode.vala
@@ -45,6 +45,25 @@ namespace Vala {
return get_ccode_attribute(node).const_name;
}
+ public static string get_ccode_struct_name (CodeNode node) {
+ unowned DataType? data_type = node as DataType;
+
+ if (data_type is ObjectType) {
+ return get_ccode_name (data_type.type_symbol);
+ } else if (data_type is ValueType && !data_type.nullable) {
+ return get_ccode_name (data_type.type_symbol);
+ } else if (data_type is ClassType || data_type is InterfaceType) {
+ return get_ccode_type_name ((ObjectTypeSymbol) data_type.type_symbol);
+ } else if (data_type is ErrorType) {
+ return "GError";
+ } else if (node is CType) {
+ return ((CType) node).ctype_name;
+ }
+
+ Report.error (node.source_reference, "internal: No type available");
+ return "";
+ }
+
public static string get_ccode_type_name (ObjectTypeSymbol sym) {
return get_ccode_attribute (sym).type_name;
}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 12bd99db2..814c14c8e 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5404,8 +5404,23 @@ 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 {
+ 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 329fb8795..e4567227a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -615,6 +615,7 @@ TESTS = \
objects/bug695671.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 000000000..9b081dc99
--- /dev/null
+++ b/tests/objects/bug728274.vala
@@ -0,0 +1,36 @@
+class Foo {
+}
+
+[Compact]
+class FooCompact {
+ public int i;
+}
+
+class FooObject : Object {
+}
+
+struct Bar {
+ public int i;
+}
+
+enum Manam {
+ NONE;
+}
+
+errordomain FooError {
+ FAIL;
+}
+
+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*));
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]