Module Name: src Committed By: rillig Date: Sat Jan 20 10:02:31 UTC 2024
Modified Files: src/tests/usr.bin/xlint/lint1: queries.c t_usage.sh src/usr.bin/xlint/lint1: decl.c err.c Log Message: lint: add query for const automatic variables To generate a diff of this commit: cvs rdiff -u -r1.21 -r1.22 src/tests/usr.bin/xlint/lint1/queries.c cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/xlint/lint1/t_usage.sh cvs rdiff -u -r1.386 -r1.387 src/usr.bin/xlint/lint1/decl.c cvs rdiff -u -r1.222 -r1.223 src/usr.bin/xlint/lint1/err.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.21 src/tests/usr.bin/xlint/lint1/queries.c:1.22 --- src/tests/usr.bin/xlint/lint1/queries.c:1.21 Sun Jan 7 18:42:37 2024 +++ src/tests/usr.bin/xlint/lint1/queries.c Sat Jan 20 10:02:31 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: queries.c,v 1.21 2024/01/07 18:42:37 rillig Exp $ */ +/* $NetBSD: queries.c,v 1.22 2024/01/20 10:02:31 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,10,11,12,13,14,15,16,17 -X 351 */ +/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 -X 351 */ typedef unsigned char u8_t; typedef unsigned short u16_t; @@ -452,9 +452,10 @@ Q15(void) * non-'static' declaration, it may look confusing. */ static void Q16(void); -/* expect+2: 'Q16' was declared 'static', now non-'static' [Q16] */ -/* expect+1: warning: static function 'Q16' unused [236] */ -void Q16(void) +/* expect+3: 'Q16' was declared 'static', now non-'static' [Q16] */ +/* expect+2: warning: static function 'Q16' unused [236] */ +void +Q16(void) { } @@ -464,6 +465,24 @@ char Q17_char[] = { ' ', '\0', ' ' }; char Q17_string[] = " \0 "; /* + * Variables with automatic storage duration often have so small scope that + * adding the 'const' qualifier hurts readability more than it helps. + */ +int +/* expect+1: const automatic variable 'const_arg' [Q18] */ +Q18(const int const_arg, int arg) +{ + /* expect+1: const automatic variable 'Q18_scalar' [Q18] */ + const char Q18_scalar = '1'; + const char Q18_array[] = { '1', '2', '3' }; + const char Q18_string[] = "123"; + const char *Q18_string_pointer = "123"; + + return const_arg + arg + + Q18_scalar + Q18_array[0] + Q18_string[0] + Q18_string_pointer[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 * exits non-successfully. Index: src/tests/usr.bin/xlint/lint1/t_usage.sh diff -u src/tests/usr.bin/xlint/lint1/t_usage.sh:1.13 src/tests/usr.bin/xlint/lint1/t_usage.sh:1.14 --- src/tests/usr.bin/xlint/lint1/t_usage.sh:1.13 Sun Jan 7 18:42:37 2024 +++ src/tests/usr.bin/xlint/lint1/t_usage.sh Sat Jan 20 10:02:31 2024 @@ -1,4 +1,4 @@ -# $NetBSD: t_usage.sh,v 1.13 2024/01/07 18:42:37 rillig Exp $ +# $NetBSD: t_usage.sh,v 1.14 2024/01/20 10:02:31 rillig Exp $ # # Copyright (c) 2023 The NetBSD Foundation, Inc. # All rights reserved. @@ -89,13 +89,13 @@ enable_queries_body() # The largest known query. atf_check \ - "$lint1" -q 17 code.c /dev/null + "$lint1" -q 18 code.c /dev/null # Larger than the largest known query. atf_check \ -s 'exit:1' \ - -e "inline:lint1: invalid query ID '18'\n" \ - "$lint1" -q 18 code.c /dev/null + -e "inline:lint1: invalid query ID '19'\n" \ + "$lint1" -q 19 code.c /dev/null # Whitespace is not allowed before a query ID. atf_check \ Index: src/usr.bin/xlint/lint1/decl.c diff -u src/usr.bin/xlint/lint1/decl.c:1.386 src/usr.bin/xlint/lint1/decl.c:1.387 --- src/usr.bin/xlint/lint1/decl.c:1.386 Sat Jan 6 15:05:24 2024 +++ src/usr.bin/xlint/lint1/decl.c Sat Jan 20 10:02:31 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: decl.c,v 1.386 2024/01/06 15:05:24 rillig Exp $ */ +/* $NetBSD: decl.c,v 1.387 2024/01/20 10:02:31 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: decl.c,v 1.386 2024/01/06 15:05:24 rillig Exp $"); +__RCSID("$NetBSD: decl.c,v 1.387 2024/01/20 10:02:31 rillig Exp $"); #endif #include <sys/param.h> @@ -2351,6 +2351,12 @@ declare_parameter(sym_t *sym, bool has_i /* parameter '%s' declared inline */ warning(269, sym->s_name); + if (any_query_enabled && sym->s_type->t_const + && (sym->s_scl == AUTO || sym->s_scl == REG)) { + /* const automatic variable '%s' */ + query_message(18, sym->s_name); + } + /* * Arguments must have complete types. length_in_bits prints the needed * error messages (null dimension is impossible because arrays are @@ -2694,6 +2700,12 @@ declare_local(sym_t *dsym, bool has_init } } + if (any_query_enabled && dsym->s_type->t_const + && (dsym->s_scl == AUTO || dsym->s_scl == REG)) { + /* const automatic variable '%s' */ + query_message(18, dsym->s_name); + } + check_function_definition(dsym, true); check_type(dsym); Index: src/usr.bin/xlint/lint1/err.c diff -u src/usr.bin/xlint/lint1/err.c:1.222 src/usr.bin/xlint/lint1/err.c:1.223 --- src/usr.bin/xlint/lint1/err.c:1.222 Sun Jan 7 18:42:37 2024 +++ src/usr.bin/xlint/lint1/err.c Sat Jan 20 10:02:31 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: err.c,v 1.222 2024/01/07 18:42:37 rillig Exp $ */ +/* $NetBSD: err.c,v 1.223 2024/01/20 10:02:31 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.222 2024/01/07 18:42:37 rillig Exp $"); +__RCSID("$NetBSD: err.c,v 1.223 2024/01/20 10:02:31 rillig Exp $"); #endif #include <limits.h> @@ -716,6 +716,7 @@ static const char *queries[] = { "implicit conversion from integer 0 to pointer '%s'", /* Q15 */ "'%s' was declared 'static', now non-'static'", /* Q16 */ "invisible character U+%04X in %s", /* Q17 */ + "const automatic variable '%s'", /* Q18 */ }; bool any_query_enabled; /* for optimizing non-query scenarios */