From: Simon Werbeck Date: Wed, 26 Nov 2014 11:24:31 +0000 (+0100) Subject: Warn when using 'this' before chain up X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3a40f6f1e7d472c1345003d4d1bbfaba6165c6f8;p=thirdparty%2Fvala.git Warn when using 'this' before chain up Do flow analysis on 'this' parameter in creation methods. Report a warning if it is used before a chain up call. https://bugzilla.gnome.org/show_bug.cgi?id=567269 --- diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala index e073c4c21..d2de29ca3 100644 --- a/vala/valaflowanalyzer.vala +++ b/vala/valaflowanalyzer.vala @@ -429,7 +429,11 @@ public class Vala.FlowAnalyzer : CodeVisitor { Report.error (used_var.source_reference, "use of possibly unassigned local variable `%s'".printf (used_var.name)); } else { // parameter - Report.warning (used_var.source_reference, "use of possibly unassigned parameter `%s'".printf (used_var.name)); + if (used_var.name == "this") { + Report.warning (used_var.source_reference, "possible reference to `this' before chaining up"); + } else { + Report.warning (used_var.source_reference, "use of possibly unassigned parameter `%s'".printf (used_var.name)); + } } continue; } @@ -461,7 +465,11 @@ public class Vala.FlowAnalyzer : CodeVisitor { Report.error (node.source_reference, "use of possibly unassigned local variable `%s'".printf (var_symbol.name)); } else { // parameter - Report.warning (node.source_reference, "use of possibly unassigned parameter `%s'".printf (var_symbol.name)); + if (var_symbol.name == "this") { + Report.warning (node.source_reference, "possible reference to `this' before chaining up"); + } else { + Report.warning (node.source_reference, "use of possibly unassigned parameter `%s'".printf (var_symbol.name)); + } } continue; } diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 3bc9eb13b..4fac98adc 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -950,7 +950,7 @@ public class Vala.MemberAccess : Expression { var param = symbol_reference as Parameter; if (local != null) { collection.add (local); - } else if (param != null && param.direction == ParameterDirection.OUT) { + } else if (param != null && (param.direction == ParameterDirection.OUT || (param.name == "this" && param.parent_symbol is CreationMethod && ((CreationMethod) param.parent_symbol).chain_up))) { collection.add (param); } } diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala index 2b43ec842..5c17ef098 100644 --- a/vala/valamethodcall.vala +++ b/vala/valamethodcall.vala @@ -287,6 +287,7 @@ public class Vala.MethodCall : Expression { var struct_creation_expression = new ObjectCreationExpression ((MemberAccess) call, source_reference); struct_creation_expression.struct_creation = true; + struct_creation_expression.struct_chainup = is_chainup; foreach (Expression arg in get_argument_list ()) { struct_creation_expression.add_argument (arg); } @@ -677,7 +678,19 @@ public class Vala.MethodCall : Expression { } public override void get_defined_variables (Collection collection) { - call.get_defined_variables (collection); + if (is_chainup) { + var this_parameter = call.symbol_reference as Parameter; + if (this_parameter == null) { + Symbol sym = (Block) parent_statement.parent_node; + do { + sym = sym.parent_symbol; + } while (sym is Block); + this_parameter = ((Method) sym).this_parameter; + } + collection.add (this_parameter); + } else { + call.get_defined_variables (collection); + } foreach (Expression arg in argument_list) { arg.get_defined_variables (collection); @@ -685,7 +698,9 @@ public class Vala.MethodCall : Expression { } public override void get_used_variables (Collection collection) { - call.get_used_variables (collection); + if (!is_chainup) { + call.get_used_variables (collection); + } foreach (Expression arg in argument_list) { arg.get_used_variables (collection); diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala index 7a2d6fbf8..a920673b0 100644 --- a/vala/valaobjectcreationexpression.vala +++ b/vala/valaobjectcreationexpression.vala @@ -46,6 +46,7 @@ public class Vala.ObjectCreationExpression : Expression { public bool is_yield_expression { get; set; } public bool struct_creation { get; set; } + public bool struct_chainup { get; set; } private List argument_list = new ArrayList (); @@ -535,6 +536,18 @@ public class Vala.ObjectCreationExpression : Expression { } public override void get_defined_variables (Collection collection) { + if (struct_chainup) { + var this_parameter = member_name.inner.symbol_reference as Parameter; + if (this_parameter == null) { + Symbol sym = (Block) parent_statement.parent_node; + do { + sym = sym.parent_symbol; + } while (sym is Block); + this_parameter = ((Method) sym).this_parameter; + } + collection.add (this_parameter); + } + foreach (Expression arg in argument_list) { arg.get_defined_variables (collection); }