lint: only skip 'unused' warnings after errors, not other warnings

Previously, in -w mode, any warning suppressed further 'unused'
warnings, even though there was no need to do that.  This can be seen in
the test gcc_attribute_var.c, where only the last unused variable from a
function was marked as unused, the others slipped through.

Fixed by counting the errors and the warnings separately and only
combining them if actually desired.
This commit is contained in:
rillig 2023-07-07 06:03:31 +00:00
parent db06c1e0c0
commit 06b8093259
25 changed files with 60 additions and 44 deletions

View File

@ -1,11 +1,11 @@
/* $NetBSD: d_init_array_using_string.c,v 1.13 2023/03/28 14:44:34 rillig Exp $ */
/* $NetBSD: d_init_array_using_string.c,v 1.14 2023/07/07 06:03:31 rillig Exp $ */
# 3 "d_init_array_using_string.c"
/*
* Test initialization of arrays and pointers by string literals.
*/
/* lint1-extra-flags: -X 351 */
/* lint1-extra-flags: -X 191,351 */
void sink(const void *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: decl.c,v 1.21 2023/03/28 14:44:34 rillig Exp $ */
/* $NetBSD: decl.c,v 1.22 2023/07/07 06:03:31 rillig Exp $ */
# 3 "decl.c"
/*
@ -6,7 +6,7 @@
* declaration-specifiers and the declarators.
*/
/* lint1-extra-flags: -X 351 */
/* lint1-extra-flags: -X 191,351 */
/*
* Even though 'const' comes after 'char' and is therefore quite close to the

View File

@ -1,4 +1,4 @@
/* $NetBSD: gcc_attribute_var.c,v 1.8 2023/03/28 14:44:34 rillig Exp $ */
/* $NetBSD: gcc_attribute_var.c,v 1.9 2023/07/07 06:03:31 rillig Exp $ */
# 3 "gcc_attribute_var.c"
/*
@ -52,6 +52,7 @@ void println(void);
void
ambiguity_for_attribute(void)
{
/* expect+1: warning: 'var1' unused in function 'ambiguity_for_attribute' [192] */
__attribute__((unused)) _Bool var1;
switch (1) {

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_011.c,v 1.5 2021/08/26 19:23:25 rillig Exp $ */
/* $NetBSD: msg_011.c,v 1.6 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_011.c"
// Test for message: bit-field initializer out of range [11]
@ -9,6 +9,7 @@ example(void)
struct {
signed int si: 3;
unsigned int ui: 3;
/* expect+1: warning: 's' set but not used in function 'example' [191] */
} s[] = {
/* expect+2: warning: bit-field initializer out of range [11] */
/* expect+1: warning: initialization of unsigned with negative constant [221] */

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_083.c,v 1.6 2023/03/28 14:44:34 rillig Exp $ */
/* $NetBSD: msg_083.c,v 1.7 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_083.c"
// Test for message: storage class after type is obsolescent [83]
@ -8,6 +8,7 @@
void
example(void)
{
/* expect+2: warning: 'x' unused in function 'example' [192] */
/* expect+1: warning: storage class after type is obsolescent [83] */
int register x;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_090.c,v 1.6 2023/03/28 20:04:52 rillig Exp $ */
/* $NetBSD: msg_090.c,v 1.7 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_090.c"
// Test for message: inconsistent redeclaration of extern '%s' [90]
@ -10,6 +10,7 @@ extern int random_number(void);
void
use(void)
{
/* expect+3: warning: 'random_number' unused in function 'use' [192] */
/* expect+2: warning: nested 'extern' declaration of 'random_number' [352] */
/* expect+1: warning: inconsistent redeclaration of extern 'random_number' [90] */
extern int random_number(int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_092.c,v 1.4 2022/06/21 21:18:30 rillig Exp $ */
/* $NetBSD: msg_092.c,v 1.5 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_092.c"
// Test for message: inconsistent redeclaration of static '%s' [92]
@ -14,6 +14,7 @@ use_random(void)
{
random();
/* expect+3: warning: 'random' unused in function 'use_random' [192] */
/* expect+2: warning: dubious static function 'random' at block level [93] */
/* expect+1: warning: inconsistent redeclaration of static 'random' [92] */
static double random(void);

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_095.c,v 1.6 2023/03/28 14:44:34 rillig Exp $ */
/* $NetBSD: msg_095.c,v 1.7 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_095.c"
// Test for message: declaration of '%s' hides earlier one [95]
@ -12,6 +12,7 @@ example(int identifier)
{
{
/* expect+2: warning: 'identifier' set but not used in function 'example' [191] */
/* expect+1: warning: declaration of 'identifier' hides earlier one [95] */
int identifier = 3;
}

View File

@ -1,9 +1,9 @@
/* $NetBSD: msg_097.c,v 1.4 2022/04/03 09:34:45 rillig Exp $ */
/* $NetBSD: msg_097.c,v 1.5 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_097.c"
/* Test for message: suffix U is illegal in traditional C [97] */
/* lint1-flags: -gtw */
/* lint1-flags: -gtw -X 191 */
void
example()

View File

@ -1,9 +1,9 @@
/* $NetBSD: msg_124.c,v 1.13 2023/03/28 14:44:34 rillig Exp $ */
/* $NetBSD: msg_124.c,v 1.14 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_124.c"
// Test for message: illegal combination of '%s' and '%s', op '%s' [124]
/* lint1-extra-flags: -s -X 351 */
/* lint1-extra-flags: -s -X 191,351 */
typedef void(*signal_handler)(int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_128.c,v 1.5 2022/06/22 19:23:18 rillig Exp $ */
/* $NetBSD: msg_128.c,v 1.6 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_128.c"
// Test for message: operands of '%s' have incompatible pointer types to '%s' and '%s' [128]
@ -7,6 +7,7 @@ void
conversion_to_unconst(const char *cstr)
{
char *str;
/* expect+2: warning: 'str' set but not used in function 'conversion_to_unconst' [191] */
/* expect+1: warning: operands of '=' have incompatible pointer types to 'char' and 'const char' [128] */
str = cstr;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_165.c,v 1.4 2022/06/16 16:58:36 rillig Exp $ */
/* $NetBSD: msg_165.c,v 1.5 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_165.c"
// Test for message: constant truncated by assignment [165]
@ -8,6 +8,7 @@ example(void)
{
unsigned char ch;
/* expect+2: warning: 'ch' set but not used in function 'example' [191] */
/* expect+1: warning: constant truncated by assignment [165] */
ch = 0x1234;
}

View File

@ -1,9 +1,9 @@
/* $NetBSD: msg_169.c,v 1.7 2023/03/28 14:44:35 rillig Exp $ */
/* $NetBSD: msg_169.c,v 1.8 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_169.c"
// Test for message: precedence confusion possible: parenthesize! [169]
/* lint1-flags: -g -h -S -w -X 351 */
/* lint1-flags: -g -h -S -w -X 191,351 */
typedef _Bool bool;

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_182.c,v 1.4 2022/06/22 19:23:18 rillig Exp $ */
/* $NetBSD: msg_182.c,v 1.5 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_182.c"
// Test for message: incompatible pointer types to '%s' and '%s' [182]
@ -13,6 +13,7 @@ return_discarding_volatile(volatile void *arg)
void
init_discarding_volatile(volatile void *arg)
{
/* expect+2: warning: 'array' set but not used in function 'init_discarding_volatile' [191] */
/* expect+1: warning: incompatible pointer types to 'void' and 'volatile void' [182] */
void *array[] = { arg };
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_188.c,v 1.5 2023/03/28 14:44:35 rillig Exp $ */
/* $NetBSD: msg_188.c,v 1.6 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_188.c"
/* Test for message: no automatic aggregate initialization in traditional C [188] */
@ -18,6 +18,7 @@ struct point global = {
void
function()
{
/* expect+2: warning: 'local' set but not used in function 'function' [191] */
/* expect+1: warning: no automatic aggregate initialization in traditional C [188] */
struct point local = {
3,

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_240.c,v 1.4 2022/07/01 20:53:13 rillig Exp $ */
/* $NetBSD: msg_240.c,v 1.5 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_240.c"
// Test for message: assignment of different structures (%s != %s) [240]
@ -32,6 +32,7 @@ return_other_struct(struct s_arg s_arg)
void
assign_other_struct(struct s_arg s_arg)
{
/* expect+1: warning: 's_local' unused in function 'assign_other_struct' [192] */
static struct s_local s_local;
/* XXX: No warning? */
s_local = s_arg;

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_247.c,v 1.30 2023/07/05 11:36:56 rillig Exp $ */
/* $NetBSD: msg_247.c,v 1.31 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_247.c"
// Test for message: pointer cast from '%s' to '%s' may be troublesome [247]
@ -308,6 +308,7 @@ void *
cast_between_first_member_struct(void *ptr)
{
/* Before tree.c 1.462 from 2022-06-24, lint warned about this cast. */
/* expect+1: warning: 't1' set but not used in function 'cast_between_first_member_struct' [191] */
void *t1 = (ctl_node_t *)(ctl_named_node_t *)ptr;
void *t2 = (ctl_named_node_t *)(ctl_node_t *)ptr;

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_277.c,v 1.7 2023/03/28 14:44:35 rillig Exp $ */
/* $NetBSD: msg_277.c,v 1.8 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_277.c"
// Test for message: initialization of '%s' with '%s' [277]
@ -27,7 +27,9 @@ example(enum E e, int i)
sink_int(i2);
sink_int(i3);
/* expect+1: warning: 'init_0' set but not used in function 'example' [191] */
enum E init_0 = 0;
/* expect+2: warning: 'init_1' set but not used in function 'example' [191] */
/* expect+1: warning: initialization of 'enum E' with 'int' [277] */
enum E init_1 = 1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_324.c,v 1.7 2022/06/22 19:23:18 rillig Exp $ */
/* $NetBSD: msg_324.c,v 1.8 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_324.c"
// Test for message: suggest cast from '%s' to '%s' on op '%s' to avoid overflow [324]
@ -22,10 +22,12 @@ example(char c, int i, unsigned u)
long long ll;
unsigned long long ull;
/* expect+2: warning: 'll' set but not used in function 'example' [191] */
/* expect+1: warning: suggest cast from 'int' to 'long long' on op '+' to avoid overflow [324] */
ll = c + i;
/* expect+1: warning: suggest cast from 'int' to 'long long' on op '-' to avoid overflow [324] */
ll = i - c;
/* expect+2: warning: 'ull' set but not used in function 'example' [191] */
/* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '*' to avoid overflow [324] */
ull = c * u;
/* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '+' to avoid overflow [324] */

View File

@ -1,9 +1,9 @@
/* $NetBSD: msg_327.c,v 1.7 2023/03/28 14:44:35 rillig Exp $ */
/* $NetBSD: msg_327.c,v 1.8 2023/07/07 06:03:31 rillig Exp $ */
# 3 "msg_327.c"
/* Test for message: declarations after statements is a C99 feature [327] */
/* lint1-flags: -w -X 351 */
/* lint1-flags: -w -X 192,351 */
void statement(void);

View File

@ -1,4 +1,4 @@
/* $NetBSD: decl.c,v 1.341 2023/07/03 10:23:12 rillig Exp $ */
/* $NetBSD: decl.c,v 1.342 2023/07/07 06:03: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.341 2023/07/03 10:23:12 rillig Exp $");
__RCSID("$NetBSD: decl.c,v 1.342 2023/07/07 06:03:31 rillig Exp $");
#endif
#include <sys/param.h>
@ -2935,7 +2935,7 @@ check_variable_usage(bool novar, sym_t *sym)
return;
/* errors in expressions easily cause lots of these warnings */
if (nerr != 0)
if (seen_error)
return;
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: err.c,v 1.204 2023/07/02 23:40:23 rillig Exp $ */
/* $NetBSD: err.c,v 1.205 2023/07/07 06:03: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.204 2023/07/02 23:40:23 rillig Exp $");
__RCSID("$NetBSD: err.c,v 1.205 2023/07/07 06:03:31 rillig Exp $");
#endif
#include <limits.h>
@ -47,8 +47,8 @@ __RCSID("$NetBSD: err.c,v 1.204 2023/07/02 23:40:23 rillig Exp $");
#include "lint1.h"
/* number of errors found */
int nerr;
bool seen_error;
bool seen_warning;
/* number of syntax errors */
int sytxerr;
@ -529,7 +529,7 @@ verror_at(int msgid, const pos_t *pos, va_list ap)
(void)printf("%s(%d): error: ", fn, pos->p_line);
(void)vprintf(msgs[msgid], ap);
(void)printf(" [%d]\n", msgid);
nerr++;
seen_error = true;
print_stack_trace();
}
@ -550,8 +550,7 @@ vwarning_at(int msgid, const pos_t *pos, va_list ap)
(void)printf("%s(%d): warning: ", fn, pos->p_line);
(void)vprintf(msgs[msgid], ap);
(void)printf(" [%d]\n", msgid);
if (wflag)
nerr++;
seen_warning = true;
print_stack_trace();
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: externs1.h,v 1.188 2023/07/02 23:40:23 rillig Exp $ */
/* $NetBSD: externs1.h,v 1.189 2023/07/07 06:03:31 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -156,7 +156,8 @@ void debug_leave_func(const char *);
/*
* err.c
*/
extern int nerr;
extern bool seen_error;
extern bool seen_warning;
extern int sytxerr;
extern bool any_query_enabled;

View File

@ -1,4 +1,4 @@
/* $NetBSD: main1.c,v 1.71 2023/07/03 11:16:32 rillig Exp $ */
/* $NetBSD: main1.c,v 1.72 2023/07/07 06:03:31 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
__RCSID("$NetBSD: main1.c,v 1.71 2023/07/03 11:16:32 rillig Exp $");
__RCSID("$NetBSD: main1.c,v 1.72 2023/07/07 06:03:31 rillig Exp $");
#endif
#include <sys/types.h>
@ -258,7 +258,7 @@ main(int argc, char *argv[])
outclose();
return nerr != 0 ? 1 : 0;
return seen_error || (wflag && seen_warning) ? 1 : 0;
}
static void __attribute__((noreturn))

View File

@ -1,4 +1,4 @@
/* $NetBSD: tree.c,v 1.547 2023/07/03 21:36:16 rillig Exp $ */
/* $NetBSD: tree.c,v 1.548 2023/07/07 06:03:31 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.547 2023/07/03 21:36:16 rillig Exp $");
__RCSID("$NetBSD: tree.c,v 1.548 2023/07/07 06:03:31 rillig Exp $");
#endif
#include <float.h>
@ -4336,7 +4336,7 @@ integer_constant(tnode_t *tn, bool required)
val_t *v = xcalloc(1, sizeof(*v));
if (tn == NULL) {
lint_assert(nerr != 0);
lint_assert(seen_error);
debug_step("constant node is null; returning 1 instead");
v->v_tspec = INT;
v->u.integer = 1;