Improve error message for if with an else part while in switch

Message ID 20230913222911.1516503-1-apinski@marvell.com
State Accepted
Headers
Series Improve error message for if with an else part while in switch |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Andrew Pinski Sept. 13, 2023, 10:29 p.m. UTC
  While writing some match.pd code, I was trying to figure
out why I was getting an `expected ), got (` error message
while writing an if statement with an else clause. For switch
statements, the if statements cannot have an else clause so
it would be better to have a decent error message saying that
explictly.

OK? Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

	* genmatch.cc (parser::parse_result): For an else clause
	of an if statement inside a switch, error out explictly.
---
 gcc/genmatch.cc | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Richard Biener Sept. 14, 2023, 8:38 a.m. UTC | #1
On Thu, Sep 14, 2023 at 12:30 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> While writing some match.pd code, I was trying to figure
> out why I was getting an `expected ), got (` error message
> while writing an if statement with an else clause. For switch
> statements, the if statements cannot have an else clause so
> it would be better to have a decent error message saying that
> explictly.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu.

OK.

Richard.

> gcc/ChangeLog:
>
>         * genmatch.cc (parser::parse_result): For an else clause
>         of an if statement inside a switch, error out explictly.
> ---
>  gcc/genmatch.cc | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
> index a1925a747a7..03d325efdf6 100644
> --- a/gcc/genmatch.cc
> +++ b/gcc/genmatch.cc
> @@ -4891,6 +4891,8 @@ parser::parse_result (operand *result, predicate_id *matcher)
>                     ife->trueexpr = parse_result (result, matcher);
>                   else
>                     ife->trueexpr = parse_op ();
> +                 if (peek ()->type == CPP_OPEN_PAREN)
> +                   fatal_at (peek(), "if inside switch cannot have an else");
>                   eat_token (CPP_CLOSE_PAREN);
>                 }
>               else
> --
> 2.31.1
>
  

Patch

diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index a1925a747a7..03d325efdf6 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -4891,6 +4891,8 @@  parser::parse_result (operand *result, predicate_id *matcher)
 		    ife->trueexpr = parse_result (result, matcher);
 		  else
 		    ife->trueexpr = parse_op ();
+		  if (peek ()->type == CPP_OPEN_PAREN)
+		    fatal_at (peek(), "if inside switch cannot have an else");
 		  eat_token (CPP_CLOSE_PAREN);
 		}
 	      else