From 79b730c71f77d6c9c1a1d6dbf90c0bc21ea43f67 Mon Sep 17 00:00:00 2001 From: rillig Date: Sun, 4 Jul 2021 09:13:59 +0000 Subject: [PATCH] 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. --- usr.bin/xlint/lint1/Makefile | 3 +-- usr.bin/xlint/lint1/ckbool.c | 11 +++++------ usr.bin/xlint/lint1/lint1.h | 7 +++++-- usr.bin/xlint/lint1/mem1.c | 23 ++++++++++++++++++++--- usr.bin/xlint/lint1/tree.c | 14 +++++++------- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/usr.bin/xlint/lint1/Makefile b/usr.bin/xlint/lint1/Makefile index df0756a34d96..d6339ec02783 100644 --- a/usr.bin/xlint/lint1/Makefile +++ b/usr.bin/xlint/lint1/Makefile @@ -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 @@ -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} diff --git a/usr.bin/xlint/lint1/ckbool.c b/usr.bin/xlint/lint1/ckbool.c index 85ac3816a0a6..e3fee784009e 100644 --- a/usr.bin/xlint/lint1/ckbool.c +++ b/usr.bin/xlint/lint1/ckbool.c @@ -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 #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 @@ -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". */ diff --git a/usr.bin/xlint/lint1/lint1.h b/usr.bin/xlint/lint1/lint1.h index 73d82ad96c75..33d61fceeb29 100644 --- a/usr.bin/xlint/lint1/lint1.h +++ b/usr.bin/xlint/lint1/lint1.h @@ -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 { diff --git a/usr.bin/xlint/lint1/mem1.c b/usr.bin/xlint/lint1/mem1.c index 5d75d4183ddc..1cf77551a088 100644 --- a/usr.bin/xlint/lint1/mem1.c +++ b/usr.bin/xlint/lint1/mem1.c @@ -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 #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 @@ -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; } diff --git a/usr.bin/xlint/lint1/tree.c b/usr.bin/xlint/lint1/tree.c index 018d2ad66080..ed9c30e59261 100644 --- a/usr.bin/xlint/lint1/tree.c +++ b/usr.bin/xlint/lint1/tree.c @@ -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 #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 @@ -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;