lint: in strict bool mode, allow mixed types in generated C code

This allows flex lexers to be run through lint in strict bool mode.
This commit is contained in:
rillig 2021-07-04 09:13:59 +00:00
parent 024372d5e0
commit 79b730c71f
5 changed files with 38 additions and 20 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.76 2021/07/04 08:49:41 rillig Exp $
# $NetBSD: Makefile,v 1.77 2021/07/04 09:13:59 rillig Exp $
.include <bsd.own.mk>
@ -19,7 +19,6 @@ CWARNFLAGS.clang+= -Wno-error=implicit-int-float-conversion
LINTFLAGS+= -T
LOBJS.${PROG}+= ${SRCS:M*.y:.y=.ln}
LOBJS.${PROG}+= ${SRCS:M*.l:.l=.ln}
LINTFLAGS.scan.c= -X 107,126,330,331,332,333
CPPFLAGS+= -DIS_LINT1
CPPFLAGS+= -I${.CURDIR}

View File

@ -1,4 +1,4 @@
/* $NetBSD: ckbool.c,v 1.6 2021/07/02 21:22:26 rillig Exp $ */
/* $NetBSD: ckbool.c,v 1.7 2021/07/04 09:13:59 rillig Exp $ */
/*-
* Copyright (c) 2021 The NetBSD Foundation, Inc.
@ -36,7 +36,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: ckbool.c,v 1.6 2021/07/02 21:22:26 rillig Exp $");
__RCSID("$NetBSD: ckbool.c,v 1.7 2021/07/04 09:13:59 rillig Exp $");
#endif
#include <string.h>
@ -89,13 +89,12 @@ is_typeok_strict_bool_binary(op_t op,
if ((lt == BOOL) == (rt == BOOL))
return true;
if ((ln->tn_from_system_header || rn->tn_from_system_header) &&
if ((ln->tn_relaxed || rn->tn_relaxed) &&
(is_int_constant_zero(ln, lt) || is_int_constant_zero(rn, rt)))
return true;
if (is_assignment_bool_or_other(op)) {
return lt != BOOL &&
(ln->tn_from_system_header || rn->tn_from_system_header);
return lt != BOOL && (ln->tn_relaxed || rn->tn_relaxed);
}
return !is_symmetric_bool_or_other(op);
@ -223,7 +222,7 @@ is_typeok_bool_operand(const tnode_t *tn)
if (t == BOOL)
return true;
if (tn->tn_from_system_header && is_scalar(t))
if (tn->tn_relaxed && is_scalar(t))
return true;
/* For enums that are used as bit sets, allow "flags & FLAG". */

View File

@ -1,4 +1,4 @@
/* $NetBSD: lint1.h,v 1.109 2021/07/02 18:22:09 rillig Exp $ */
/* $NetBSD: lint1.h,v 1.110 2021/07/04 09:13:59 rillig Exp $ */
/*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@ -306,7 +306,10 @@ typedef struct tnode {
bool tn_lvalue : 1; /* node is lvalue */
bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */
bool tn_parenthesized : 1;
bool tn_from_system_header : 1;
bool tn_relaxed : 1; /* in strict bool mode, allow mixture between
* bool and scalar, for backwards
* compatibility
*/
bool tn_system_dependent : 1; /* depends on sizeof or offsetof */
union {
struct {

View File

@ -1,4 +1,4 @@
/* $NetBSD: mem1.c,v 1.44 2021/06/20 18:51:50 rillig Exp $ */
/* $NetBSD: mem1.c,v 1.45 2021/07/04 09:13:59 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: mem1.c,v 1.44 2021/06/20 18:51:50 rillig Exp $");
__RCSID("$NetBSD: mem1.c,v 1.45 2021/07/04 09:13:59 rillig Exp $");
#endif
#include <sys/types.h>
@ -346,6 +346,16 @@ expr_zalloc(size_t s)
return xgetblk(&tmblk, s);
}
static bool
str_endswith(const char *haystack, const char *needle)
{
size_t hlen = strlen(haystack);
size_t nlen = strlen(needle);
return nlen <= hlen &&
memcmp(haystack + hlen - nlen, needle, nlen) == 0;
}
/*
* Return a freshly allocated tree node that is freed at the end of the
* current expression.
@ -354,7 +364,14 @@ tnode_t *
expr_zalloc_tnode(void)
{
tnode_t *tn = expr_zalloc(sizeof(*tn));
tn->tn_from_system_header = in_system_header;
/*
* files named *.c that are different from the main translation unit
* typically contain generated code that cannot be influenced, such
* as a flex lexer or a yacc parser.
*/
tn->tn_relaxed = in_system_header ||
(curr_pos.p_file != csrc_pos.p_file &&
str_endswith(curr_pos.p_file, ".c"));
return tn;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: tree.c,v 1.305 2021/07/04 08:19:06 rillig Exp $ */
/* $NetBSD: tree.c,v 1.306 2021/07/04 09:13:59 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: tree.c,v 1.305 2021/07/04 08:19:06 rillig Exp $");
__RCSID("$NetBSD: tree.c,v 1.306 2021/07/04 09:13:59 rillig Exp $");
#endif
#include <float.h>
@ -1642,10 +1642,10 @@ new_tnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
ntn->tn_op = op;
ntn->tn_type = type;
if (ln->tn_from_system_header)
ntn->tn_from_system_header = true;
if (rn != NULL && rn->tn_from_system_header)
ntn->tn_from_system_header = true;
if (ln->tn_relaxed)
ntn->tn_relaxed = true;
if (rn != NULL && rn->tn_relaxed)
ntn->tn_relaxed = true;
ntn->tn_left = ln;
ntn->tn_right = rn;
@ -1887,7 +1887,7 @@ convert(op_t op, int arg, type_t *tp, tnode_t *tn)
ntn->tn_op = CVT;
ntn->tn_type = tp;
ntn->tn_cast = op == CVT;
ntn->tn_from_system_header |= tn->tn_from_system_header;
ntn->tn_relaxed |= tn->tn_relaxed;
ntn->tn_right = NULL;
if (tn->tn_op != CON || nt == VOID) {
ntn->tn_left = tn;