Module Name: src Committed By: rillig Date: Sun Feb 28 03:59:28 UTC 2021
Modified Files: src/tests/usr.bin/xlint/lint1: msg_161.c msg_161.exp src/usr.bin/xlint/lint1: lint1.h tree.c Log Message: lint: do not warn about constant expressions involving sizeof These expressions are indeed constant for a specific platform, but on another platform their value may change. This makes them unsuspicious and legitimate for portable code. Seen in rump_syscalls.c, as 'sizeof(int) > sizeof(register_t)'. To generate a diff of this commit: cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_161.c cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_161.exp cvs rdiff -u -r1.66 -r1.67 src/usr.bin/xlint/lint1/lint1.h cvs rdiff -u -r1.226 -r1.227 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/msg_161.c diff -u src/tests/usr.bin/xlint/lint1/msg_161.c:1.5 src/tests/usr.bin/xlint/lint1/msg_161.c:1.6 --- src/tests/usr.bin/xlint/lint1/msg_161.c:1.5 Sun Feb 28 03:29:12 2021 +++ src/tests/usr.bin/xlint/lint1/msg_161.c Sun Feb 28 03:59:28 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: msg_161.c,v 1.5 2021/02/28 03:29:12 rillig Exp $ */ +/* $NetBSD: msg_161.c,v 1.6 2021/02/28 03:59:28 rillig Exp $ */ # 3 "msg_161.c" // Test for message: constant in conditional context [161] @@ -43,15 +43,15 @@ do_while_1(void) extern void println(const char *); +/* + * Since 2021-02-28, lint no longer warns about constant controlling + * expressions involving sizeof since these are completely legitimate. + */ void test_sizeof(void) { - /* - * XXX: The following conditions should not need CONSTCOND as they - * are perfectly legitimate. - */ - if (sizeof(int) > sizeof(char)) /* expect: 161 */ + if (sizeof(int) > sizeof(char)) println("very probable"); - if (sizeof(int) < sizeof(char)) /* expect: 161 */ + if (sizeof(int) < sizeof(char)) println("impossible"); } Index: src/tests/usr.bin/xlint/lint1/msg_161.exp diff -u src/tests/usr.bin/xlint/lint1/msg_161.exp:1.4 src/tests/usr.bin/xlint/lint1/msg_161.exp:1.5 --- src/tests/usr.bin/xlint/lint1/msg_161.exp:1.4 Sun Feb 28 03:29:12 2021 +++ src/tests/usr.bin/xlint/lint1/msg_161.exp Sun Feb 28 03:59:28 2021 @@ -1,5 +1,3 @@ msg_161.c(11): warning: constant in conditional context [161] msg_161.c(18): warning: constant in conditional context [161] msg_161.c(41): warning: constant in conditional context [161] -msg_161.c(53): warning: constant in conditional context [161] -msg_161.c(55): warning: constant in conditional context [161] Index: src/usr.bin/xlint/lint1/lint1.h diff -u src/usr.bin/xlint/lint1/lint1.h:1.66 src/usr.bin/xlint/lint1/lint1.h:1.67 --- src/usr.bin/xlint/lint1/lint1.h:1.66 Mon Feb 22 15:09:50 2021 +++ src/usr.bin/xlint/lint1/lint1.h Sun Feb 28 03:59:28 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: lint1.h,v 1.66 2021/02/22 15:09:50 rillig Exp $ */ +/* $NetBSD: lint1.h,v 1.67 2021/02/28 03:59:28 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -289,6 +289,7 @@ typedef struct tnode { 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_system_dependent : 1; union { struct { struct tnode *_tn_left; /* (left) operand */ Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.226 src/usr.bin/xlint/lint1/tree.c:1.227 --- src/usr.bin/xlint/lint1/tree.c:1.226 Sun Feb 28 03:33:18 2021 +++ src/usr.bin/xlint/lint1/tree.c Sun Feb 28 03:59:28 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: tree.c,v 1.226 2021/02/28 03:33:18 rillig Exp $ */ +/* $NetBSD: tree.c,v 1.227 2021/02/28 03:59:28 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.226 2021/02/28 03:33:18 rillig Exp $"); +__RCSID("$NetBSD: tree.c,v 1.227 2021/02/28 03:59:28 rillig Exp $"); #endif #include <float.h> @@ -641,7 +641,8 @@ build(op_t op, tnode_t *ln, tnode_t *rn) if (mp->m_left_test_context) { if (ln->tn_op == CON || ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) { - if (hflag && !constcond_flag) + if (hflag && !constcond_flag && + !ln->tn_system_dependent) /* constant in conditional context */ warning(161); } @@ -3161,6 +3162,10 @@ fold(tnode_t *tn) v->v_quad = xsign(q, t, -1); cn = new_constant_node(tn->tn_type, v); + if (tn->tn_left->tn_system_dependent) + cn->tn_system_dependent = true; + if (modtab[tn->tn_op].m_binary && tn->tn_right->tn_system_dependent) + cn->tn_system_dependent = true; return cn; } @@ -3306,7 +3311,10 @@ fold_float(tnode_t *tn) tnode_t * build_sizeof(type_t *tp) { - return new_integer_constant_node(SIZEOF_TSPEC, tsize(tp) / CHAR_SIZE); + int64_t size_in_bytes = tsize(tp) / CHAR_SIZE; + tnode_t *tn = new_integer_constant_node(SIZEOF_TSPEC, size_in_bytes); + tn->tn_system_dependent = true; + return tn; } /* @@ -3321,7 +3329,10 @@ build_offsetof(type_t *tp, sym_t *sym) error(111, "offsetof"); // XXX: wrong size, no checking for sym fixme - return new_integer_constant_node(SIZEOF_TSPEC, tsize(tp) / CHAR_SIZE); + int64_t offset_in_bytes = tsize(tp) / CHAR_SIZE; + tnode_t *tn = new_integer_constant_node(SIZEOF_TSPEC, offset_in_bytes); + tn->tn_system_dependent = true; + return tn; } int64_t @@ -3759,6 +3770,7 @@ expr(tnode_t *tn, bool vctx, bool tctx, warning(159); } else if (tn->tn_op == CON) { if (hflag && tctx && !constcond_flag && + !tn->tn_system_dependent && !(constcond_false_ok && is_constcond_false(tn, tn->tn_type->t_tspec))) /* constant in conditional context */