Skip to content

Commit

Permalink
Forbid case-after-default, tweak error messages (#4831)
Browse files Browse the repository at this point in the history
* Forbid case-after-default, tweak error messages

* Update tests

* Fix formatting

* Bring back positive version of default-switch.p4 test
  • Loading branch information
vlstill authored Jul 29, 2024
1 parent 4c25ea5 commit 8c70f3b
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 39 deletions.
9 changes: 5 additions & 4 deletions frontends/p4/validateParsedProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ void ValidateParsedProgram::postorder(const IR::SwitchStatement *statement) {
for (auto c : statement->cases) {
if (defaultFound != nullptr) {
if (c->label->is<IR::DefaultExpression>())
::error(ErrorType::ERR_INVALID, "%1% has multiple 'default' labels: %2% and %3%.",
statement, defaultFound->label, c->label);
::error(ErrorType::ERR_INVALID, "%1%switch has multiple 'default' labels:%2%%3%",
statement->srcInfo, defaultFound->label->srcInfo, c->label->srcInfo);
else
warn(ErrorType::WARN_ORDERING, "%1%: label following 'default' %2% label.",
c->label, defaultFound->label);
::error(ErrorType::ERR_INVALID,
"%1%switch label %2% follows 'default' label, which is not allowed.%3%",
statement->srcInfo, c->label, defaultFound->label->srcInfo);
break;
}
if (c->label->is<IR::DefaultExpression>()) defaultFound = c;
Expand Down
16 changes: 16 additions & 0 deletions testdata/p4_16_errors/default-last-switch.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
control ctrl() {
action a() {}
action b() {}

table t {
actions = { a; b; }
default_action = a;
}

apply {
switch (t.apply().action_run) {
default:
b: { return; }
}
}
}
3 changes: 1 addition & 2 deletions testdata/p4_16_errors/issue3611.p4
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ control c()
}
apply {
switch(t.apply().action_run) {
default: {}
1: {} // { dg-error "" }
default: {}
a: {}
a: {}
default: {}
}
}
}
22 changes: 22 additions & 0 deletions testdata/p4_16_errors_outputs/default-last-switch.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
control ctrl() {
action a() {
}
action b() {
}
table t {
actions = {
a;
b;
}
default_action = a;
}
apply {
switch (t.apply().action_run) {
default:
b: {
return;
}
}
}
}

9 changes: 9 additions & 0 deletions testdata/p4_16_errors_outputs/default-last-switch.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default-last-switch.p4(11): [--Werror=invalid] error: switch label b follows 'default' label, which is not allowed.
switch (t.apply().action_run) {
^^^^^^
default-last-switch.p4(13)
b: { return; }
^
default-last-switch.p4(12)
default:
^^^^^^^
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue2525.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue2525.p4(64): [--Werror=invalid] error: SwitchStatement has multiple 'default' labels: DefaultExpression and DefaultExpression.
issue2525.p4(64): [--Werror=invalid] error: switch has multiple 'default' labels:
switch (t.apply().action_run) {
^^^^^^
issue2525.p4(66)
Expand Down
6 changes: 2 additions & 4 deletions testdata/p4_16_errors_outputs/issue3611.p4
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ control c() {
}
apply {
switch (t.apply().action_run) {
default: {
}
1: {
}
default: {
}
a: {
}
a: {
}
default: {
}
}
}
}
Expand Down
18 changes: 3 additions & 15 deletions testdata/p4_16_errors_outputs/issue3611.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
issue3611.p4(13): [--Wwarn=ordering] warning: 1: label following 'default' DefaultExpression label.
issue3611.p4(12): [--Werror=type-error] error: 1: 'switch' label must be an action name or 'default'
1: {} // { dg-error "" }
^
issue3611.p4(12)
default: {}
^^^^^^^
issue3611.p4(13): [--Werror=type-error] error: 1: 'switch' label must be an action name or 'default'
1: {} // { dg-error "" }
^
issue3611.p4(14): [--Werror=type-error] error: DefaultExpression: multiple 'default' labels DefaultExpression
default: {}
^^^^^^^
issue3611.p4(12)
default: {}
^^^^^^^
issue3611.p4(16): [--Werror=type-error] error: a: 'switch' label duplicates a
issue3611.p4(14): [--Werror=type-error] error: a: 'switch' label duplicates a
a: {}
^
issue3611.p4(15)
issue3611.p4(13)
a: {}
^
6 changes: 3 additions & 3 deletions testdata/p4_16_samples/default-switch.p4
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ control ctrl() {

apply {
switch (t.apply().action_run) {
default:
b: { return; }
b:
default: { return; }
}
}
}
}
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/default-switch-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ control ctrl() {
}
apply {
switch (t.apply().action_run) {
default:
b: {
b:
default: {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/default-switch.p4
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ control ctrl() {
}
apply {
switch (t.apply().action_run) {
default:
b: {
b:
default: {
return;
}
}
Expand Down
6 changes: 0 additions & 6 deletions testdata/p4_16_samples_outputs/default-switch.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
default-switch.p4(13): [--Wwarn=ordering] warning: b: label following 'default' DefaultExpression label.
b: { return; }
^
default-switch.p4(12)
default:
^^^^^^^
default-switch.p4(1): [--Wwarn=unused] warning: Control ctrl is not used; removing
control ctrl() {
^^^^
Expand Down

0 comments on commit 8c70f3b

Please sign in to comment.