[vala/wip/bug567269: 2/7] 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/7] Warn when using 'this' before chain up
- Date: Fri, 2 Dec 2016 12:42:53 +0000 (UTC)
commit f0ebbdee0b2fd2bd808699e303d4cd8ac02e02ca
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 9f94a42..56a399d 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -416,7 +416,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;
}
@@ -448,7 +452,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 6911bef..4a660a9 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -934,7 +934,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 f620316..f8208b7 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);
}
@@ -669,7 +670,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);
@@ -677,7 +690,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 c4b119a..cd5f299 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> ();
@@ -530,6 +531,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]