Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: fix parsing of _Generic selection ...



details:   https://anonhg.NetBSD.org/src/rev/ee05454fb570
branches:  trunk
changeset: 1021962:ee05454fb570
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jun 27 21:30:46 2021 +0000

description:
lint: fix parsing of _Generic selection expressions

Previously, lint accepted comma-expressions where only
assignment-expressions are allowed.

This change does not make a difference in practice though since lint is
usually only run on source code that properly compiles.  Nevertheless,
rather be precise and accurate since the grammar might some day be
reused on less reliable input.

diffstat:

 tests/usr.bin/xlint/lint1/c11_generic_expression.c   |   6 +++---
 tests/usr.bin/xlint/lint1/c11_generic_expression.exp |   3 ++-
 usr.bin/xlint/lint1/cgram.y                          |  14 +++++++++-----
 3 files changed, 14 insertions(+), 9 deletions(-)

diffs (82 lines):

diff -r b2d0c7df1bde -r ee05454fb570 tests/usr.bin/xlint/lint1/c11_generic_expression.c
--- a/tests/usr.bin/xlint/lint1/c11_generic_expression.c        Sun Jun 27 21:16:40 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/c11_generic_expression.c        Sun Jun 27 21:30:46 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: c11_generic_expression.c,v 1.4 2021/06/27 21:16:40 rillig Exp $        */
+/*     $NetBSD: c11_generic_expression.c,v 1.5 2021/06/27 21:30:46 rillig Exp $        */
 # 3 "c11_generic_expression.c"
 
 /*
@@ -69,9 +69,9 @@
 const int *
 comma_expression(char first, double second)
 {
-       return _Generic(first, second,  /* FIXME */
+       return _Generic(first, second,  /* expect: syntax error 'second' */
            char: "first",
            double: 2.0
        );
-       /* expect-1: mismatch (pointer to const int) and (double) [211] */
+       /* expect+1: without returning value [217] */
 }
diff -r b2d0c7df1bde -r ee05454fb570 tests/usr.bin/xlint/lint1/c11_generic_expression.exp
--- a/tests/usr.bin/xlint/lint1/c11_generic_expression.exp      Sun Jun 27 21:16:40 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/c11_generic_expression.exp      Sun Jun 27 21:30:46 2021 +0000
@@ -2,4 +2,5 @@
 c11_generic_expression.c(21): warning: argument 'var' unused in function 'classify_type_without_default' [231]
 c11_generic_expression.c(37): warning: argument 'var' unused in function 'classify_type_with_default' [231]
 c11_generic_expression.c(53): warning: argument 'c' unused in function 'classify_char' [231]
-c11_generic_expression.c(75): error: return value type mismatch (pointer to const int) and (double) [211]
+c11_generic_expression.c(72): error: syntax error 'second' [249]
+c11_generic_expression.c(77): warning: function comma_expression falls off bottom without returning value [217]
diff -r b2d0c7df1bde -r ee05454fb570 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Sun Jun 27 21:16:40 2021 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Sun Jun 27 21:30:46 2021 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.237 2021/06/27 20:47:13 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.238 2021/06/27 21:30:46 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.y,v 1.237 2021/06/27 20:47:13 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.238 2021/06/27 21:30:46 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -323,6 +323,7 @@
 %type  <y_sym>         parameter_type_list
 %type  <y_sym>         parameter_declaration
 %type  <y_tnode>       expr
+%type  <y_tnode>       assignment_expression
 %type  <y_tnode>       gcc_statement_expr_list
 %type  <y_tnode>       gcc_statement_expr_item
 %type  <y_tnode>       term
@@ -1682,10 +1683,9 @@
          }
        ;
 
-/* TODO: C11 6.5.1.1 says "assignment-expression", not plain "expr". */
-/* TODO: c11ism */
 generic_selection:             /* C11 6.5.1.1 */
-         T_GENERIC T_LPAREN expr T_COMMA generic_assoc_list T_RPAREN {
+         T_GENERIC T_LPAREN assignment_expression T_COMMA
+           generic_assoc_list T_RPAREN {
                /* generic selection requires C11 or later */
                c11ism(345);
                $$ = build_generic_selection($3, $5);
@@ -1911,6 +1911,10 @@
          }
        ;
 
+assignment_expression:         /* C99 6.5.16 */
+         expr %prec T_ASSIGN
+       ;
+
 term:
          T_NAME {
                /* XXX really necessary? */



Home | Main Index | Thread Index | Old Index