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: add query for comparing 'char' wit...



details:   https://anonhg.NetBSD.org/src/rev/0c8fb56551d4
branches:  trunk
changeset: 376616:0c8fb56551d4
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jun 24 08:11:12 2023 +0000

description:
lint: add query for comparing 'char' with plain integers

diffstat:

 tests/usr.bin/xlint/lint1/queries.c  |  24 ++++++++++++++++++++++--
 tests/usr.bin/xlint/lint1/t_usage.sh |   8 ++++----
 usr.bin/xlint/lint1/debug.c          |   6 ++++--
 usr.bin/xlint/lint1/err.c            |   5 +++--
 usr.bin/xlint/lint1/lex.c            |   6 ++++--
 usr.bin/xlint/lint1/lint1.h          |   3 ++-
 usr.bin/xlint/lint1/tree.c           |  23 ++++++++++++++++++-----
 7 files changed, 57 insertions(+), 18 deletions(-)

diffs (233 lines):

diff -r 80bed1b2178d -r 0c8fb56551d4 tests/usr.bin/xlint/lint1/queries.c
--- a/tests/usr.bin/xlint/lint1/queries.c       Sat Jun 24 08:00:52 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/queries.c       Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: queries.c,v 1.16 2023/06/24 06:55:34 rillig Exp $      */
+/*     $NetBSD: queries.c,v 1.17 2023/06/24 08:11:12 rillig Exp $      */
 # 3 "queries.c"
 
 /*
@@ -15,7 +15,7 @@
  *     such as casts between arithmetic types.
  */
 
-/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10,11,12,13 -X 351 */
+/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10,11,12,13,14 -X 351 */
 
 typedef unsigned char u8_t;
 typedef unsigned short u16_t;
@@ -403,6 +403,26 @@ void extern_Q13(void);
 /* expect+1: redundant 'extern' in function declaration of 'extern_Q13' [Q13] */
 extern void extern_Q13(void), *extern_ptr;
 
+int
+Q14(char c, signed char sc, unsigned char uc, int wc)
+{
+       /* expect+2: comparison '==' of 'char' with plain integer 92 [Q14] */
+       /* expect+1: comparison '==' of 'char' with plain integer 0 [Q14] */
+       if (c == 'c' || c == L'w' || c == 92 || c == 0)
+               return 1;
+       if (sc == 'c' || sc == L'w' || sc == 92 || sc == 0)
+               return 2;
+       /* expect+4: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
+       /* expect+3: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
+       /* expect+2: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
+       /* expect+1: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
+       if (uc == 'c' || uc == L'w' || uc == 92 || uc == 0)
+               return 3;
+       if (wc == 'c' || wc == L'w' || wc == 92 || wc == 0)
+               return 4;
+       return 5;
+}
+
 /*
  * Since queries do not affect the exit status, force a warning to make this
  * test conform to the general expectation that a test that produces output
diff -r 80bed1b2178d -r 0c8fb56551d4 tests/usr.bin/xlint/lint1/t_usage.sh
--- a/tests/usr.bin/xlint/lint1/t_usage.sh      Sat Jun 24 08:00:52 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/t_usage.sh      Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_usage.sh,v 1.6 2023/06/24 06:55:34 rillig Exp $
+# $NetBSD: t_usage.sh,v 1.7 2023/06/24 08:11:12 rillig Exp $
 #
 # Copyright (c) 2023 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -97,13 +97,13 @@ enable_queries_body()
 
        # The largest known query.
        atf_check \
-           "$lint1" -q 13 code.c /dev/null
+           "$lint1" -q 14 code.c /dev/null
 
        # Larger than the largest known query.
        atf_check \
            -s 'exit:1' \
-           -e "inline:lint1: invalid query ID '14'\n" \
-           "$lint1" -q 14 code.c /dev/null
+           -e "inline:lint1: invalid query ID '15'\n" \
+           "$lint1" -q 15 code.c /dev/null
 
        # Whitespace is not allowed before a query ID.
        atf_check \
diff -r 80bed1b2178d -r 0c8fb56551d4 usr.bin/xlint/lint1/debug.c
--- a/usr.bin/xlint/lint1/debug.c       Sat Jun 24 08:00:52 2023 +0000
+++ b/usr.bin/xlint/lint1/debug.c       Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.34 2023/06/23 20:37:02 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.35 2023/06/24 08:11:12 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: debug.c,v 1.34 2023/06/23 20:37:02 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.35 2023/06/24 08:11:12 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -198,6 +198,8 @@ debug_node(const tnode_t *tn) // NOLINT(
                }
                if (tn->tn_val->v_unsigned_since_c90)
                        debug_printf(", unsigned_since_c90");
+               if (tn->tn_val->v_char_constant)
+                       debug_printf(", char_constant");
                debug_printf("\n");
                break;
        case STRING:
diff -r 80bed1b2178d -r 0c8fb56551d4 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Sat Jun 24 08:00:52 2023 +0000
+++ b/usr.bin/xlint/lint1/err.c Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: err.c,v 1.201 2023/06/24 06:55:34 rillig Exp $ */
+/*     $NetBSD: err.c,v 1.202 2023/06/24 08:11:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.201 2023/06/24 06:55:34 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.202 2023/06/24 08:11:12 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -709,6 +709,7 @@ static const char *queries[] = {
        "static variable '%s' in function",                           /* Q11 */
        "comma operator with types '%s' and '%s'",                    /* Q12 */
        "redundant 'extern' in function declaration of '%s'",         /* Q13 */
+       "comparison '%s' of 'char' with plain integer %d",            /* Q14 */
 };
 
 bool any_query_enabled;                /* for optimizing non-query scenarios */
diff -r 80bed1b2178d -r 0c8fb56551d4 usr.bin/xlint/lint1/lex.c
--- a/usr.bin/xlint/lint1/lex.c Sat Jun 24 08:00:52 2023 +0000
+++ b/usr.bin/xlint/lint1/lex.c Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.160 2023/06/09 15:36:31 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.161 2023/06/24 08:11:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: lex.c,v 1.160 2023/06/09 15:36:31 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.161 2023/06/24 08:11:12 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -878,6 +878,7 @@ lex_character_constant(void)
 
        yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
        yylval.y_val->v_tspec = INT;
+       yylval.y_val->v_char_constant = true;
        yylval.y_val->v_quad = val;
 
        return T_CON;
@@ -925,6 +926,7 @@ lex_wide_character_constant(void)
 
        yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
        yylval.y_val->v_tspec = WCHAR;
+       yylval.y_val->v_char_constant = true;
        yylval.y_val->v_quad = wc;
 
        return T_CON;
diff -r 80bed1b2178d -r 0c8fb56551d4 usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h       Sat Jun 24 08:00:52 2023 +0000
+++ b/usr.bin/xlint/lint1/lint1.h       Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.165 2023/06/09 15:36:31 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.166 2023/06/24 08:11:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -109,6 +109,7 @@ typedef struct {
         * See the operators table in ops.def, columns "l r".
         */
        bool    v_unsigned_since_c90;
+       bool    v_char_constant;
        union {
                int64_t         _v_quad;        /* integers */
                long double     _v_ldbl;        /* floats */
diff -r 80bed1b2178d -r 0c8fb56551d4 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Jun 24 08:00:52 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Jun 24 08:11:12 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.528 2023/06/22 13:57:44 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.529 2023/06/24 08:11:12 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.528 2023/06/22 13:57:44 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.529 2023/06/24 08:11:12 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -376,6 +376,7 @@ build_constant(type_t *tp, val_t *v)
        n->tn_val = expr_zero_alloc(sizeof(*n->tn_val));
        n->tn_val->v_tspec = tp->t_tspec;
        n->tn_val->v_unsigned_since_c90 = v->v_unsigned_since_c90;
+       n->tn_val->v_char_constant = v->v_char_constant;
        n->tn_val->v_u = v->v_u;
        free(v);
        return n;
@@ -583,9 +584,6 @@ is_out_of_char_range(const tnode_t *tn)
                 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
 }
 
-/*
- * Check for ordered comparisons of unsigned values with 0.
- */
 static void
 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
 {
@@ -600,6 +598,21 @@ check_integer_comparison(op_t op, tnode_
        if (!is_integer(lt) || !is_integer(rt))
                return;
 
+       if (any_query_enabled && !in_system_header) {
+               if (lt == CHAR && rn->tn_op == CON &&
+                   !rn->tn_val->v_char_constant) {
+                       /* comparison '%s' of 'char' with plain integer %d */
+                       query_message(14,
+                           op_name(op), (int)rn->tn_val->v_quad);
+               }
+               if (rt == CHAR && ln->tn_op == CON &&
+                   !ln->tn_val->v_char_constant) {
+                       /* comparison '%s' of 'char' with plain integer %d */
+                       query_message(14,
+                           op_name(op), (int)ln->tn_val->v_quad);
+               }
+       }
+
        if (hflag || pflag) {
                if (lt == CHAR && is_out_of_char_range(rn)) {
                        char buf[128];



Home | Main Index | Thread Index | Old Index