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: don't warn about cast between poin...
details: https://anonhg.NetBSD.org/src/rev/a486320bf52e
branches: trunk
changeset: 954422:a486320bf52e
user: rillig <rillig%NetBSD.org@localhost>
date: Thu Apr 08 19:20:54 2021 +0000
description:
lint: don't warn about cast between pointers to compatible structs
diffstat:
tests/usr.bin/xlint/lint1/msg_247.c | 7 +++++--
tests/usr.bin/xlint/lint1/msg_247.exp | 1 -
usr.bin/xlint/lint1/tree.c | 21 ++++++++++++++++-----
3 files changed, 21 insertions(+), 8 deletions(-)
diffs (72 lines):
diff -r f24dd9c1353e -r a486320bf52e tests/usr.bin/xlint/lint1/msg_247.c
--- a/tests/usr.bin/xlint/lint1/msg_247.c Thu Apr 08 19:08:17 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.c Thu Apr 08 19:20:54 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_247.c,v 1.9 2021/04/08 19:08:17 rillig Exp $ */
+/* $NetBSD: msg_247.c,v 1.10 2021/04/08 19:20:54 rillig Exp $ */
# 3 "msg_247.c"
// Test for message: pointer cast from '%s' to '%s' may be troublesome [247]
@@ -112,7 +112,10 @@
void
counter_increment(struct counter *counter)
{
- /* expect+1: 247 */
+ /*
+ * Before tree.c 1.272 from 2021-04-08, lint warned about the cast
+ * from 'struct counter' to 'struct counter_impl'.
+ */
struct counter_impl *impl = (struct counter_impl *)counter;
impl->saved_count = impl->public_part.count;
impl->public_part.count++;
diff -r f24dd9c1353e -r a486320bf52e tests/usr.bin/xlint/lint1/msg_247.exp
--- a/tests/usr.bin/xlint/lint1/msg_247.exp Thu Apr 08 19:08:17 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.exp Thu Apr 08 19:20:54 2021 +0000
@@ -1,3 +1,2 @@
msg_247.c(31): warning: pointer cast from 'pointer to struct Other' to 'pointer to struct <unnamed>' may be troublesome [247]
msg_247.c(70): warning: pointer cast from 'pointer to struct Other' to 'pointer to signed char' may be troublesome [247]
-msg_247.c(116): warning: pointer cast from 'pointer to struct counter' to 'pointer to struct counter_impl' may be troublesome [247]
diff -r f24dd9c1353e -r a486320bf52e usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c Thu Apr 08 19:08:17 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c Thu Apr 08 19:20:54 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.271 2021/04/06 21:59:58 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.272 2021/04/08 19:20:54 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.271 2021/04/06 21:59:58 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.272 2021/04/08 19:20:54 rillig Exp $");
#endif
#include <float.h>
@@ -2023,11 +2023,22 @@
}
static bool
-should_warn_about_pointer_cast(const type_t *tp, tspec_t nst,
- const tnode_t *tn, tspec_t ost)
+should_warn_about_pointer_cast(const type_t *ntp, tspec_t nst,
+ const tnode_t *otn, tspec_t ost)
{
+ /*
+ * Casting a pointer to 'struct S' to a pointer to another struct that
+ * has 'struct S' as its first member is ok, see msg_247.c, 'struct
+ * counter'.
+ */
+ if (nst == STRUCT && ost == STRUCT &&
+ ntp->t_subt->t_str->sou_first_member != NULL &&
+ ntp->t_subt->t_str->sou_first_member->s_type ==
+ otn->tn_type->t_subt)
+ return false;
+
if (nst == STRUCT || nst == UNION)
- if (tp->t_subt->t_str != tn->tn_type->t_subt->t_str)
+ if (ntp->t_subt->t_str != otn->tn_type->t_subt->t_str)
return true;
if (nst == CHAR || nst == UCHAR)
Home |
Main Index |
Thread Index |
Old Index