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)'.
This commit is contained in:
rillig 2021-02-28 03:59:28 +00:00
parent 597058d81f
commit 0d0d46fd3a
4 changed files with 26 additions and 15 deletions

View File

@ -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");
}

View File

@ -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]

View File

@ -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 */

View File

@ -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, bool dofreeblk, bool constcond_false_ok)
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 */