Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: do not warn about constant express...



details:   https://anonhg.NetBSD.org/src/rev/1d1d237ce602
branches:  trunk
changeset: 953175:1d1d237ce602
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Feb 28 03:59:28 2021 +0000

description:
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)'.

diffstat:

 tests/usr.bin/xlint/lint1/msg_161.c   |  14 +++++++-------
 tests/usr.bin/xlint/lint1/msg_161.exp |   2 --
 usr.bin/xlint/lint1/lint1.h           |   3 ++-
 usr.bin/xlint/lint1/tree.c            |  22 +++++++++++++++++-----
 4 files changed, 26 insertions(+), 15 deletions(-)

diffs (128 lines):

diff -r 1adabd9a4b1f -r 1d1d237ce602 tests/usr.bin/xlint/lint1/msg_161.c
--- a/tests/usr.bin/xlint/lint1/msg_161.c       Sun Feb 28 03:33:18 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_161.c       Sun Feb 28 03:59:28 2021 +0000
@@ -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 @@
 
 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");
 }
diff -r 1adabd9a4b1f -r 1d1d237ce602 tests/usr.bin/xlint/lint1/msg_161.exp
--- a/tests/usr.bin/xlint/lint1/msg_161.exp     Sun Feb 28 03:33:18 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_161.exp     Sun Feb 28 03:59:28 2021 +0000
@@ -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]
diff -r 1adabd9a4b1f -r 1d1d237ce602 usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h       Sun Feb 28 03:33:18 2021 +0000
+++ b/usr.bin/xlint/lint1/lint1.h       Sun Feb 28 03:59:28 2021 +0000
@@ -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 @@
        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 */
diff -r 1adabd9a4b1f -r 1d1d237ce602 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sun Feb 28 03:33:18 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sun Feb 28 03:59:28 2021 +0000
@@ -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 @@
        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 @@
        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 @@
 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 @@
                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 @@
                        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 */



Home | Main Index | Thread Index | Old Index