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 chained assignments...



details:   https://anonhg.NetBSD.org/src/rev/4bd8ea0bcdc3
branches:  trunk
changeset: 984867:4bd8ea0bcdc3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Jul 26 17:27:22 2021 +0000

description:
lint: fix parsing of chained assignments (since 2021-07-15)

The grammar rule for assignment_expression is quite different from those
of the other expressions, for 2 reasons: first, its precedence is
right-to-left.  Second, its left-hand side must be an lvalue, which
rules out all binary operators.  K&R C even had a grammar rule named
'lvalue' for this purpose.  Later C standards made the kinds of
expressions more fine-grained and used 'unary_expression' in this place.

diffstat:

 tests/usr.bin/xlint/lint1/expr_precedence.c   |  9 ++++++---
 tests/usr.bin/xlint/lint1/expr_precedence.exp |  1 -
 usr.bin/xlint/lint1/cgram.y                   |  8 ++++----
 3 files changed, 10 insertions(+), 8 deletions(-)

diffs (63 lines):

diff -r 81892b4bb152 -r 4bd8ea0bcdc3 tests/usr.bin/xlint/lint1/expr_precedence.c
--- a/tests/usr.bin/xlint/lint1/expr_precedence.c       Mon Jul 26 17:15:24 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_precedence.c       Mon Jul 26 17:27:22 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expr_precedence.c,v 1.4 2021/07/26 17:09:46 rillig Exp $       */
+/*     $NetBSD: expr_precedence.c,v 1.5 2021/07/26 17:27:22 rillig Exp $       */
 # 3 "expr_precedence.c"
 
 /*
@@ -42,8 +42,11 @@
 {
        int left, right;
 
-       /* FIXME */
-       /* expect+1: error: left operand of '=' must be lvalue [114] */
+       /*
+        * Assignments are right-associative.  If they were left-associative,
+        * the result of (left = right) would be an rvalue, resulting in this
+        * error message: 'left operand of '=' must be lvalue [114]'.
+        */
        left = right = arg;
 
        left = arg;
diff -r 81892b4bb152 -r 4bd8ea0bcdc3 tests/usr.bin/xlint/lint1/expr_precedence.exp
--- a/tests/usr.bin/xlint/lint1/expr_precedence.exp     Mon Jul 26 17:15:24 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_precedence.exp     Mon Jul 26 17:27:22 2021 +0000
@@ -2,4 +2,3 @@
 expr_precedence.c(18): error: non-constant initializer [177]
 expr_precedence.c(35): error: 'var' undefined [99]
 expr_precedence.c(35): error: syntax error '=' [249]
-expr_precedence.c(47): error: left operand of '=' must be lvalue [114]
diff -r 81892b4bb152 -r 4bd8ea0bcdc3 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Mon Jul 26 17:15:24 2021 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Mon Jul 26 17:27:22 2021 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.347 2021/07/26 17:15:24 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.348 2021/07/26 17:27:22 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.347 2021/07/26 17:15:24 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.348 2021/07/26 17:27:22 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -691,10 +691,10 @@
 /* K&R ???, C90 ???, C99 6.5.16, C11 6.5.16 */
 assignment_expression:
          conditional_expression
-       | assignment_expression T_ASSIGN conditional_expression {
+       | unary_expression T_ASSIGN assignment_expression {
                $$ = build_binary($1, ASSIGN, $3);
          }
-       | assignment_expression T_OPASSIGN conditional_expression {
+       | unary_expression T_OPASSIGN assignment_expression {
                $$ = build_binary($1, $2, $3);
          }
        ;



Home | Main Index | Thread Index | Old Index