[vala/wip/bug567269: 2/5] Warn when using 'this' before chain up
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/bug567269: 2/5] Warn when using 'this' before chain up
- Date: Mon, 19 Feb 2018 15:59:19 +0000 (UTC)
commit abaa2d5e02446141d460f0c9a987811c0c474be9
Author: Simon Werbeck <simon werbeck gmail com>
Date: Wed Nov 26 12:24:31 2014 +0100
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
vala/valaflowanalyzer.vala | 12 ++++++++++--
vala/valamemberaccess.vala | 2 +-
vala/valamethodcall.vala | 19 +++++++++++++++++--
vala/valaobjectcreationexpression.vala | 13 +++++++++++++
4 files changed, 41 insertions(+), 5 deletions(-)
---
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index 853eae7..ff0d9eb 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -419,7 +419,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;
}
@@ -451,7 +455,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 85f8bbb..b445ecb 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -926,7 +926,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 415191a..713b9bb 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -291,6 +291,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);
}
@@ -681,7 +682,19 @@ public class Vala.MethodCall : Expression {
}
public override void get_defined_variables (Collection<Variable> 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);
@@ -689,7 +702,9 @@ public class Vala.MethodCall : Expression {
}
public override void get_used_variables (Collection<Variable> 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 7a2d6fb..a920673 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<Expression> argument_list = new ArrayList<Expression> ();
@@ -535,6 +536,18 @@ public class Vala.ObjectCreationExpression : Expression {
}
public override void get_defined_variables (Collection<Variable> 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);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]