[vala/wip/issue/777: 177/177] WIP vala: Check coverage of switch on enum-type and issue warnings if needed
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/issue/777: 177/177] WIP vala: Check coverage of switch on enum-type and issue warnings if needed
- Date: Wed, 5 Feb 2020 13:16:13 +0000 (UTC)
commit 82c029fee506db2b6719c359b40963701ba12001
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Fri Mar 29 14:27:14 2019 +0100
WIP vala: Check coverage of switch on enum-type and issue warnings if needed
Also don't emit implicit default label.
Fixes https://gitlab.gnome.org/GNOME/vala/issues/777
codegen/valaccodecontrolflowmodule.vala | 2 +-
tests/Makefile.am | 1 +
tests/control-flow/switch-enum.vala | 20 +++++++++++++++++
vala/valaflowanalyzer.vala | 39 +++++++++++++++++++++++++++++++++
4 files changed, 61 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodecontrolflowmodule.vala b/codegen/valaccodecontrolflowmodule.vala
index df31fcef3..a61b3d9fe 100644
--- a/codegen/valaccodecontrolflowmodule.vala
+++ b/codegen/valaccodecontrolflowmodule.vala
@@ -182,7 +182,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
section.emit (this);
}
- if (!has_default) {
+ if (!has_default && !(stmt.expression.value_type is EnumValueType)) {
// silence C compiler
ccode.add_default ();
ccode.add_break ();
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8079e335a..d6be59077 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -191,6 +191,7 @@ TESTS = \
control-flow/missing-return.test \
control-flow/nested-conditional.vala \
control-flow/switch.vala \
+ control-flow/switch-enum.vala \
control-flow/sideeffects.vala \
control-flow/unassigned-captured-local-variable.test \
control-flow/unassigned-local-block-variable.test \
diff --git a/tests/control-flow/switch-enum.vala b/tests/control-flow/switch-enum.vala
new file mode 100644
index 000000000..51aaaba4c
--- /dev/null
+++ b/tests/control-flow/switch-enum.vala
@@ -0,0 +1,20 @@
+enum Foo {
+ FOO,
+ BAR,
+ MANAM
+}
+
+Foo foo () {
+ Foo foo = Foo.BAR;
+
+ switch (foo) {
+ case Foo.MANAM:
+ case Foo.FOO:
+ case Foo.BAR:
+ return foo;
+ }
+}
+
+void main () {
+ assert (foo () == Foo.BAR);
+}
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index 978ab94f8..5bf28677d 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -674,6 +674,14 @@ public class Vala.FlowAnalyzer : CodeVisitor {
handle_errors (stmt.expression);
bool has_default_label = false;
+ bool is_enum_typed = stmt.expression.value_type is EnumValueType;
+
+ unowned Enum? en = null;
+ HashSet<unowned EnumValue>? enum_values = null;
+ if (is_enum_typed) {
+ en = (Enum) ((EnumValueType) stmt.expression.value_type).type_symbol;
+ enum_values = new HashSet<unowned EnumValue> (direct_hash, direct_equal);
+ }
foreach (SwitchSection section in stmt.get_sections ()) {
current_block = new BasicBlock ();
@@ -683,6 +691,17 @@ public class Vala.FlowAnalyzer : CodeVisitor {
section_stmt.accept (this);
}
+ if (is_enum_typed) {
+ foreach (SwitchLabel label in section.get_labels ()) {
+ if (label.expression != null) {
+ unowned EnumValue? val = label.expression.symbol_reference as
EnumValue;
+ if (val != null) {
+ enum_values.add (val);
+ }
+ }
+ }
+ }
+
if (section.has_default_label ()) {
has_default_label = true;
}
@@ -698,6 +717,26 @@ public class Vala.FlowAnalyzer : CodeVisitor {
}
}
+ if (is_enum_typed) {
+ // Check if enum-based switching as fully covered, and if so,
+ // handle it like there was a default-label given
+
+ HashSet<EnumValue> remaining_values = new HashSet<EnumValue> ();
+ remaining_values.add_all (en.get_values ());
+ foreach (var val in enum_values) {
+ remaining_values.remove (val);
+ }
+ if (remaining_values.size == 0) {
+ has_default_label = true;
+ } else if (!has_default_label) {
+ string[] missing_vals = {};
+ foreach (var val in remaining_values) {
+ missing_vals += val.name;
+ }
+ Report.warning (stmt.source_reference, "switch does not handle `%s' of enum
`%s'".printf (string.joinv ("', `", missing_vals), en.get_full_name ()));
+ }
+ }
+
if (!has_default_label) {
condition_block.connect (after_switch_block);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]