diff options
| author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-12-28 17:33:25 +0100 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-12-28 18:06:21 +0100 |
| commit | f1e3c9e9c9aef42c63519d3a6d77e52f3ffa6b53 (patch) | |
| tree | 7b8aec92f6dbfb053b9ddd171806eadf51144e74 | |
| parent | 69a789a78d4e64052628307f25310e195a50f5ee (diff) | |
| download | sparse-dev-f1e3c9e9c9aef42c63519d3a6d77e52f3ffa6b53.tar.gz | |
warn on empty parenthesized expressions
Empty sub-expressions are normally caught as syntax error
in most expressions but this is not the case for parenthesized
expressions.
Fix this by adding a check at the end of parens_expressions()
and warning if needed.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
| -rw-r--r-- | expression.c | 6 | ||||
| -rw-r--r-- | validation/empty-expr.c | 27 |
2 files changed, 33 insertions, 0 deletions
diff --git a/expression.c b/expression.c index e5ebad65..6f4300b9 100644 --- a/expression.c +++ b/expression.c @@ -62,7 +62,10 @@ static struct token *comma_expression(struct token *, struct expression **); struct token *parens_expression(struct token *token, struct expression **expr, const char *where) { + struct token *p; + token = expect(token, '(', where); + p = token; if (match_op(token, '{')) { struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT); struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND); @@ -74,6 +77,9 @@ struct token *parens_expression(struct token *token, struct expression **expr, c token = expect(token, '}', "at end of statement expression"); } else token = parse_expression(token, expr); + + if (token == p) + sparse_error(token->pos, "an expression is expected before ')'"); return expect(token, ')', where); } diff --git a/validation/empty-expr.c b/validation/empty-expr.c new file mode 100644 index 00000000..506cfba7 --- /dev/null +++ b/validation/empty-expr.c @@ -0,0 +1,27 @@ +static int foo(void) +{ + switch () { + case 0: + return 0; + default: + return 1; + } +} + +static int bar(void) +{ + if () + return 0; + else + return 1; +} + +/* + * check-name: empty expression + * check-known-to-fail + * + * check-error-start +empty-expr.c:3:17: error: an expression is expected before ')' +empty-expr.c:13:13: error: an expression is expected before ')' + * check-error-end + */ |
