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: extend check for unconst functions
details: https://anonhg.NetBSD.org/src/rev/761a71733184
branches: trunk
changeset: 1022951:761a71733184
user: rillig <rillig%NetBSD.org@localhost>
date: Sun Aug 15 14:26:39 2021 +0000
description:
lint: extend check for unconst functions
The functions memchr, strpbrk, strrchr and strstr effectively remove the
const qualifier of their first argument, just like strchr.
diffstat:
tests/usr.bin/xlint/lint1/msg_346.c | 10 ++--
tests/usr.bin/xlint/lint1/msg_346.exp | 4 ++
usr.bin/xlint/lint1/tree.c | 59 ++++++++++++++++++++++++----------
3 files changed, 50 insertions(+), 23 deletions(-)
diffs (142 lines):
diff -r b70c98430fad -r 761a71733184 tests/usr.bin/xlint/lint1/msg_346.c
--- a/tests/usr.bin/xlint/lint1/msg_346.c Sun Aug 15 14:00:27 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_346.c Sun Aug 15 14:26:39 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_346.c,v 1.2 2021/08/15 14:00:27 rillig Exp $ */
+/* $NetBSD: msg_346.c,v 1.3 2021/08/15 14:26:39 rillig Exp $ */
# 3 "msg_346.c"
// Test for message: call to '%s' effectively discards 'const' from argument [346]
@@ -40,14 +40,14 @@
void
all_functions(void)
{
- /* TODO: expect+1: warning: call to 'memchr' effectively discards 'const' from argument [346] */
+ /* expect+1: warning: call to 'memchr' effectively discards 'const' from argument [346] */
take_char_ptr(memchr("string", 'c', 7));
/* expect+1: warning: call to 'strchr' effectively discards 'const' from argument [346] */
take_char_ptr(strchr("string", 'c'));
- /* TODO: expect+1: warning: call to 'strpbrk' effectively discards 'const' from argument [346] */
+ /* expect+1: warning: call to 'strpbrk' effectively discards 'const' from argument [346] */
take_char_ptr(strpbrk("string", "c"));
- /* TODO: expect+1: warning: call to 'strrchr' effectively discards 'const' from argument [346] */
+ /* expect+1: warning: call to 'strrchr' effectively discards 'const' from argument [346] */
take_char_ptr(strrchr("string", 'c'));
- /* TODO: expect+1: warning: call to 'strstr' effectively discards 'const' from argument [346] */
+ /* expect+1: warning: call to 'strstr' effectively discards 'const' from argument [346] */
take_char_ptr(strstr("string", "c"));
}
diff -r b70c98430fad -r 761a71733184 tests/usr.bin/xlint/lint1/msg_346.exp
--- a/tests/usr.bin/xlint/lint1/msg_346.exp Sun Aug 15 14:00:27 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_346.exp Sun Aug 15 14:26:39 2021 +0000
@@ -1,4 +1,8 @@
msg_346.c(26): warning: call to 'strchr' effectively discards 'const' from argument [346]
msg_346.c(32): warning: call to 'strchr' effectively discards 'const' from argument [346]
msg_346.c(37): warning: call to 'strchr' effectively discards 'const' from argument [346]
+msg_346.c(44): warning: call to 'memchr' effectively discards 'const' from argument [346]
msg_346.c(46): warning: call to 'strchr' effectively discards 'const' from argument [346]
+msg_346.c(48): warning: call to 'strpbrk' effectively discards 'const' from argument [346]
+msg_346.c(50): warning: call to 'strrchr' effectively discards 'const' from argument [346]
+msg_346.c(52): warning: call to 'strstr' effectively discards 'const' from argument [346]
diff -r b70c98430fad -r 761a71733184 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c Sun Aug 15 14:00:27 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c Sun Aug 15 14:26:39 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.335 2021/08/15 13:08:19 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.336 2021/08/15 14:26:39 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.335 2021/08/15 13:08:19 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.336 2021/08/15 14:26:39 rillig Exp $");
#endif
#include <float.h>
@@ -1349,12 +1349,27 @@
}
static bool
-is_direct_function_call(const tnode_t *tn, const char *name)
+is_direct_function_call(const tnode_t *tn, const char **out_name)
{
- return tn->tn_op == CALL &&
- tn->tn_left->tn_op == ADDR &&
- tn->tn_left->tn_left->tn_op == NAME &&
- strcmp(tn->tn_left->tn_left->tn_sym->s_name, name) == 0;
+
+ if (!(tn->tn_op == CALL &&
+ tn->tn_left->tn_op == ADDR &&
+ tn->tn_left->tn_left->tn_op == NAME))
+ return false;
+
+ *out_name = tn->tn_left->tn_left->tn_sym->s_name;
+ return true;
+}
+
+static bool
+is_unconst_function(const char *name)
+{
+
+ return strcmp(name, "memchr") == 0 ||
+ strcmp(name, "strchr") == 0 ||
+ strcmp(name, "strpbrk") == 0 ||
+ strcmp(name, "strrchr") == 0 ||
+ strcmp(name, "strstr") == 0;
}
static bool
@@ -1385,23 +1400,31 @@
}
static bool
-is_strchr_arg_const(const tnode_t *tn)
+is_first_arg_const(const tnode_t *tn)
{
- return tn->tn_right->tn_op == PUSH &&
- tn->tn_right->tn_right->tn_op == PUSH &&
- tn->tn_right->tn_right->tn_right == NULL &&
- is_const_char_pointer(tn->tn_right->tn_right->tn_left);
+ const tnode_t *an;
+
+ an = tn->tn_right;
+ if (an == NULL)
+ return false;
+
+ while (an->tn_right != NULL)
+ an = an->tn_right;
+ return is_const_char_pointer(an->tn_left);
}
static void
-check_unconst_strchr(const type_t *lstp,
- const tnode_t *rn, const type_t *rstp)
+check_unconst_function(const type_t *lstp,
+ const tnode_t *rn, const type_t *rstp)
{
+ const char *function_name;
+
if (lstp->t_tspec == CHAR && !lstp->t_const &&
- is_direct_function_call(rn, "strchr") &&
- is_strchr_arg_const(rn)) {
+ is_direct_function_call(rn, &function_name) &&
+ is_unconst_function(function_name) &&
+ is_first_arg_const(rn)) {
/* call to '%s' effectively discards 'const' from argument */
- warning(346, "strchr");
+ warning(346, function_name);
}
}
@@ -1490,7 +1513,7 @@
}
if (!tflag)
- check_unconst_strchr(lstp, rn, rstp);
+ check_unconst_function(lstp, rn, rstp);
return true;
}
Home |
Main Index |
Thread Index |
Old Index