Module Name: src Committed By: rillig Date: Sat May 13 20:55:44 UTC 2023
Modified Files: src/tests/usr.bin/xlint/lint1: queries.c t_usage.sh src/usr.bin/xlint/lint1: err.c tree.c Log Message: lint: add query for chained assignments To generate a diff of this commit: cvs rdiff -u -r1.12 -r1.13 src/tests/usr.bin/xlint/lint1/queries.c cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/t_usage.sh cvs rdiff -u -r1.195 -r1.196 src/usr.bin/xlint/lint1/err.c cvs rdiff -u -r1.522 -r1.523 src/usr.bin/xlint/lint1/tree.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/queries.c diff -u src/tests/usr.bin/xlint/lint1/queries.c:1.12 src/tests/usr.bin/xlint/lint1/queries.c:1.13 --- src/tests/usr.bin/xlint/lint1/queries.c:1.12 Sat Apr 15 11:34:45 2023 +++ src/tests/usr.bin/xlint/lint1/queries.c Sat May 13 20:55:44 2023 @@ -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 Index: src/tests/usr.bin/xlint/lint1/t_usage.sh diff -u src/tests/usr.bin/xlint/lint1/t_usage.sh:1.2 src/tests/usr.bin/xlint/lint1/t_usage.sh:1.3 --- src/tests/usr.bin/xlint/lint1/t_usage.sh:1.2 Sun Apr 23 09:04:44 2023 +++ src/tests/usr.bin/xlint/lint1/t_usage.sh Sat May 13 20:55:44 2023 @@ -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 \ Index: src/usr.bin/xlint/lint1/err.c diff -u src/usr.bin/xlint/lint1/err.c:1.195 src/usr.bin/xlint/lint1/err.c:1.196 --- src/usr.bin/xlint/lint1/err.c:1.195 Tue Apr 25 19:00:57 2023 +++ src/usr.bin/xlint/lint1/err.c Sat May 13 20:55:44 2023 @@ -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 */ Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.522 src/usr.bin/xlint/lint1/tree.c:1.523 --- src/usr.bin/xlint/lint1/tree.c:1.522 Wed May 10 21:46:26 2023 +++ src/usr.bin/xlint/lint1/tree.c Sat May 13 20:55:44 2023 @@ -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);