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: add query for chained assignments



details:   https://anonhg.NetBSD.org/src/rev/6ad4520a2fe1
branches:  trunk
changeset: 374841:6ad4520a2fe1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 13 20:55:44 2023 +0000

description:
lint: add query for chained assignments

diffstat:

 tests/usr.bin/xlint/lint1/queries.c  |  18 ++++++++++++++++--
 tests/usr.bin/xlint/lint1/t_usage.sh |   8 ++++----
 usr.bin/xlint/lint1/err.c            |   5 +++--
 usr.bin/xlint/lint1/tree.c           |  35 +++++++++++++++++++++++++++--------
 4 files changed, 50 insertions(+), 16 deletions(-)

diffs (156 lines):

diff -r 92b79f838d6f -r 6ad4520a2fe1 tests/usr.bin/xlint/lint1/queries.c
--- a/tests/usr.bin/xlint/lint1/queries.c       Sat May 13 20:13:05 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/queries.c       Sat May 13 20:55:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: queries.c,v 1.12 2023/04/15 11:34:45 rillig Exp $      */
+/*     $NetBSD: queries.c,v 1.13 2023/05/13 20:55:44 rillig Exp $      */
 # 3 "queries.c"
 
 /*
@@ -15,7 +15,7 @@
  *     such as casts between arithmetic types.
  */
 
-/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9 -X 351 */
+/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10 -X 351 */
 
 typedef unsigned char u8_t;
 typedef unsigned short u16_t;
@@ -353,6 +353,20 @@ Q9(int x)
        }
 }
 
+void
+Q10(void)
+{
+       int a, b, c;
+
+       /* expect+2: chained assignment with '=' and '=' [Q10] */
+       /* expect+1: chained assignment with '=' and '=' [Q10] */
+       a = b = c = 0;
+
+       /* expect+2: chained assignment with '*=' and '-=' [Q10] */
+       /* expect+1: chained assignment with '+=' and '*=' [Q10] */
+       a += b *= c -= 0;
+}
+
 /*
  * Since queries do not affect the exit status, force a warning to make this
  * test conform to the general expectation that a test that produces output
diff -r 92b79f838d6f -r 6ad4520a2fe1 tests/usr.bin/xlint/lint1/t_usage.sh
--- a/tests/usr.bin/xlint/lint1/t_usage.sh      Sat May 13 20:13:05 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/t_usage.sh      Sat May 13 20:55:44 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_usage.sh,v 1.2 2023/04/23 09:04:44 rillig Exp $
+# $NetBSD: t_usage.sh,v 1.3 2023/05/13 20:55:44 rillig Exp $
 #
 # Copyright (c) 2023 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -97,13 +97,13 @@ enable_queries_body()
 
        # The largest known query.
        atf_check \
-           "$lint1" -q 9 code.c /dev/null
+           "$lint1" -q 10 code.c /dev/null
 
        # Larger than the largest known query.
        atf_check \
            -s 'exit:1' \
-           -e "inline:lint1: invalid query ID '10'\n" \
-           "$lint1" -q 10 code.c /dev/null
+           -e "inline:lint1: invalid query ID '11'\n" \
+           "$lint1" -q 11 code.c /dev/null
 
        # Whitespace is not allowed before a query ID.
        atf_check \
diff -r 92b79f838d6f -r 6ad4520a2fe1 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Sat May 13 20:13:05 2023 +0000
+++ b/usr.bin/xlint/lint1/err.c Sat May 13 20:55:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: err.c,v 1.195 2023/04/25 19:00:57 rillig Exp $ */
+/*     $NetBSD: err.c,v 1.196 2023/05/13 20:55:44 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.195 2023/04/25 19:00:57 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.196 2023/05/13 20:55:44 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -705,6 +705,7 @@ static const char *queries[] = {
        "redundant cast from '%s' to '%s' before assignment",         /* Q7 */
        "octal number '%.*s'",                                        /* Q8 */
        "parenthesized return value",                                 /* Q9 */
+       "chained assignment with '%s' and '%s'",                      /* Q10 */
 };
 
 bool any_query_enabled;                /* for optimizing non-query scenarios */
diff -r 92b79f838d6f -r 6ad4520a2fe1 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat May 13 20:13:05 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat May 13 20:55:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.522 2023/05/10 21:46:26 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.523 2023/05/13 20:55:44 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.522 2023/05/10 21:46:26 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.523 2023/05/13 20:55:44 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -1305,21 +1305,40 @@ is_cast_redundant(const tnode_t *tn)
        return false;
 }
 
-/*
- * Create a node for an assignment operator (both = and op= ).
- */
+static bool
+is_assignment(op_t op)
+{
+
+       return op == ASSIGN ||
+              op == MULASS ||
+              op == DIVASS ||
+              op == MODASS ||
+              op == ADDASS ||
+              op == SUBASS ||
+              op == SHLASS ||
+              op == SHRASS ||
+              op == ANDASS ||
+              op == XORASS ||
+              op == ORASS ||
+              op == RETURN ||
+              op == INIT;
+}
+
+/* Create a node for an assignment operator (both '=' and 'op='). */
 static tnode_t *
 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
 {
        tspec_t lt, rt;
        tnode_t *ntn, *ctn;
 
-       lint_assert(ln != NULL);
-       lint_assert(rn != NULL);
-
        lt = ln->tn_type->t_tspec;
        rt = rn->tn_type->t_tspec;
 
+       if (any_query_enabled && is_assignment(rn->tn_op)) {
+               /* chained assignment with '%s' and '%s' */
+               query_message(10, op_name(op), op_name(rn->tn_op));
+       }
+
        if ((op == ADDASS || op == SUBASS) && lt == PTR) {
                lint_assert(is_integer(rt));
                ctn = subt_size_in_bytes(ln->tn_type);



Home | Main Index | Thread Index | Old Index