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: investigate why lint only warns ab...



details:   https://anonhg.NetBSD.org/src/rev/0e8f845af86b
branches:  trunk
changeset: 365270:0e8f845af86b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Apr 16 20:57:10 2022 +0000

description:
lint: investigate why lint only warns about some constant conditions

Noticed in lex.c, in the macro kwdef, where lint complains about the
condition containing '||' but not about the conditions containing only
'>' or '=='.

diffstat:

 tests/usr.bin/xlint/lint1/msg_161.c   |  21 ++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_161.exp |   6 ++++++
 usr.bin/xlint/lint1/lint1.h           |   3 ++-
 usr.bin/xlint/lint1/tree.c            |  33 +++++++++++++--------------------
 4 files changed, 41 insertions(+), 22 deletions(-)

diffs (144 lines):

diff -r 76a5639d70f6 -r 0e8f845af86b tests/usr.bin/xlint/lint1/msg_161.c
--- a/tests/usr.bin/xlint/lint1/msg_161.c       Sat Apr 16 20:18:51 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_161.c       Sat Apr 16 20:57:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_161.c,v 1.7 2021/03/21 15:44:57 rillig Exp $       */
+/*     $NetBSD: msg_161.c,v 1.8 2022/04/16 20:57:10 rillig Exp $       */
 # 3 "msg_161.c"
 
 // Test for message: constant in conditional context [161]
@@ -55,3 +55,22 @@
        if (sizeof(int) < sizeof(char))
                println("impossible");
 }
+
+const _Bool conditions[] = {
+       /* XXX: Why no warning here? */
+       13 < 13,
+       /* XXX: Why no warning here? */
+       0 < 0,
+       /* XXX: Why no warning here? */
+       0 != 0,
+       /* expect+1: warning: constant in conditional context [161] */
+       0 == 0 && 1 == 0,
+       /* expect+1: warning: constant in conditional context [161] */
+       1 == 0 || 2 == 1,
+       /* expect+2: warning: constant in conditional context [161] */
+       /* expect+1: error: non-constant initializer [177] */
+       0 == 0 && ""[0] == '\0',
+       /* expect+2: warning: constant in conditional context [161] */
+       /* expect+1: error: non-constant initializer [177] */
+       ""[0] == '\0' && 0 == 0,
+};
diff -r 76a5639d70f6 -r 0e8f845af86b tests/usr.bin/xlint/lint1/msg_161.exp
--- a/tests/usr.bin/xlint/lint1/msg_161.exp     Sat Apr 16 20:18:51 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_161.exp     Sat Apr 16 20:57:10 2022 +0000
@@ -2,3 +2,9 @@
 msg_161.c(18): warning: constant in conditional context [161]
 msg_161.c(19): warning: statement not reached [193]
 msg_161.c(41): warning: constant in conditional context [161]
+msg_161.c(67): warning: constant in conditional context [161]
+msg_161.c(69): warning: constant in conditional context [161]
+msg_161.c(72): warning: constant in conditional context [161]
+msg_161.c(72): error: non-constant initializer [177]
+msg_161.c(75): warning: constant in conditional context [161]
+msg_161.c(75): error: non-constant initializer [177]
diff -r 76a5639d70f6 -r 0e8f845af86b usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h       Sat Apr 16 20:18:51 2022 +0000
+++ b/usr.bin/xlint/lint1/lint1.h       Sat Apr 16 20:57:10 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.152 2022/04/16 19:18:17 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.153 2022/04/16 20:57:10 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -493,6 +493,7 @@
 
 #  define wrap_check_printf(func, msgid, args...)                      \
        ({                                                              \
+               debug_step("%s:%d: %s", __FILE__, __LINE__, __func__);  \
                check_printf(__CONCAT(MSG_, msgid), ##args);            \
                (func)(msgid, ##args);                                  \
                /* LINTED 129 */                                        \
diff -r 76a5639d70f6 -r 0e8f845af86b usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Apr 16 20:18:51 2022 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Apr 16 20:57:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.430 2022/04/16 20:02:55 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.431 2022/04/16 20:57:10 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.430 2022/04/16 20:02:55 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.431 2022/04/16 20:57:10 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -534,7 +534,7 @@
 
        /*
         * Apply class conversions to the left operand, but only if its
-        * value is needed or it is compared with null.
+        * value is needed or it is compared with zero.
         */
        if (mp->m_left_value_context || mp->m_left_test_context)
                ln = cconv(ln);
@@ -549,21 +549,14 @@
         * Print some warnings for comparisons of unsigned values with
         * constants lower than or equal to null. This must be done
         * before promote() because otherwise unsigned char and unsigned
-        * short would be promoted to int. Also types are tested to be
+        * short would be promoted to int. Types are also tested to be
         * CHAR, which would also become int.
         */
        if (mp->m_comparison)
                check_integer_comparison(op, ln, rn);
 
-       /*
-        * Promote the left operand if it is in a test or value context
-        */
        if (mp->m_left_value_context || mp->m_left_test_context)
                ln = promote(op, false, ln);
-       /*
-        * Promote the right operand, but only if it is no struct or
-        * union member, or if it is not to be assigned to the left operand
-        */
        if (mp->m_binary && op != ARROW && op != POINT &&
            op != ASSIGN && op != RETURN && op != INIT) {
                rn = promote(op, false, rn);
@@ -653,7 +646,7 @@
        default:
                rettp = mp->m_returns_bool
                    ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
-               lint_assert(mp->m_binary || rn == NULL);
+               lint_assert(mp->m_binary == (rn != NULL));
                ntn = new_tnode(op, sys, rettp, ln, rn);
                break;
        }
@@ -670,14 +663,14 @@
         * Print a warning if one of the operands is in a context where
         * it is compared with zero and if this operand is a constant.
         */
-       if (mp->m_left_test_context) {
-               if (ln->tn_op == CON ||
-                   ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
-                       if (hflag && !constcond_flag &&
-                           !ln->tn_system_dependent)
-                               /* constant in conditional context */
-                               warning(161);
-               }
+       if (hflag && !constcond_flag &&
+           mp->m_left_test_context &&
+           (ln->tn_op == CON ||
+            ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) &&
+           /* XXX: rn->tn_system_dependent should be checked as well */
+           !ln->tn_system_dependent) {
+               /* constant in conditional context */
+               warning(161);
        }
 
        /* Fold if the operator requires it */



Home | Main Index | Thread Index | Old Index