Module Name: src Committed By: rillig Date: Sat Jun 24 08:11:12 UTC 2023
Modified Files: src/tests/usr.bin/xlint/lint1: queries.c t_usage.sh src/usr.bin/xlint/lint1: debug.c err.c lex.c lint1.h tree.c Log Message: lint: add query for comparing 'char' with plain integers To generate a diff of this commit: cvs rdiff -u -r1.16 -r1.17 src/tests/usr.bin/xlint/lint1/queries.c cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/t_usage.sh cvs rdiff -u -r1.34 -r1.35 src/usr.bin/xlint/lint1/debug.c cvs rdiff -u -r1.201 -r1.202 src/usr.bin/xlint/lint1/err.c cvs rdiff -u -r1.160 -r1.161 src/usr.bin/xlint/lint1/lex.c cvs rdiff -u -r1.165 -r1.166 src/usr.bin/xlint/lint1/lint1.h cvs rdiff -u -r1.528 -r1.529 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/queries.c diff -u src/tests/usr.bin/xlint/lint1/queries.c:1.16 src/tests/usr.bin/xlint/lint1/queries.c:1.17 --- src/tests/usr.bin/xlint/lint1/queries.c:1.16 Sat Jun 24 06:55:34 2023 +++ src/tests/usr.bin/xlint/lint1/queries.c Sat Jun 24 08:11:12 2023 @@ -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 Index: src/tests/usr.bin/xlint/lint1/t_usage.sh diff -u src/tests/usr.bin/xlint/lint1/t_usage.sh:1.6 src/tests/usr.bin/xlint/lint1/t_usage.sh:1.7 --- src/tests/usr.bin/xlint/lint1/t_usage.sh:1.6 Sat Jun 24 06:55:34 2023 +++ src/tests/usr.bin/xlint/lint1/t_usage.sh Sat Jun 24 08:11:12 2023 @@ -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 \ Index: src/usr.bin/xlint/lint1/debug.c diff -u src/usr.bin/xlint/lint1/debug.c:1.34 src/usr.bin/xlint/lint1/debug.c:1.35 --- src/usr.bin/xlint/lint1/debug.c:1.34 Fri Jun 23 20:37:02 2023 +++ src/usr.bin/xlint/lint1/debug.c Sat Jun 24 08:11:12 2023 @@ -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: Index: src/usr.bin/xlint/lint1/err.c diff -u src/usr.bin/xlint/lint1/err.c:1.201 src/usr.bin/xlint/lint1/err.c:1.202 --- src/usr.bin/xlint/lint1/err.c:1.201 Sat Jun 24 06:55:34 2023 +++ src/usr.bin/xlint/lint1/err.c Sat Jun 24 08:11:12 2023 @@ -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 */ Index: src/usr.bin/xlint/lint1/lex.c diff -u src/usr.bin/xlint/lint1/lex.c:1.160 src/usr.bin/xlint/lint1/lex.c:1.161 --- src/usr.bin/xlint/lint1/lex.c:1.160 Fri Jun 9 15:36:31 2023 +++ src/usr.bin/xlint/lint1/lex.c Sat Jun 24 08:11:12 2023 @@ -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; Index: src/usr.bin/xlint/lint1/lint1.h diff -u src/usr.bin/xlint/lint1/lint1.h:1.165 src/usr.bin/xlint/lint1/lint1.h:1.166 --- src/usr.bin/xlint/lint1/lint1.h:1.165 Fri Jun 9 15:36:31 2023 +++ src/usr.bin/xlint/lint1/lint1.h Sat Jun 24 08:11:12 2023 @@ -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 */ Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.528 src/usr.bin/xlint/lint1/tree.c:1.529 --- src/usr.bin/xlint/lint1/tree.c:1.528 Thu Jun 22 13:57:44 2023 +++ src/usr.bin/xlint/lint1/tree.c Sat Jun 24 08:11:12 2023 @@ -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];