Module Name: src
Committed By: rillig
Date: Thu Apr 8 19:20:55 UTC 2021
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_247.c msg_247.exp
src/usr.bin/xlint/lint1: tree.c
Log Message:
lint: don't warn about cast between pointers to compatible structs
To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/xlint/lint1/msg_247.c
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_247.exp
cvs rdiff -u -r1.271 -r1.272 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/msg_247.c
diff -u src/tests/usr.bin/xlint/lint1/msg_247.c:1.9 src/tests/usr.bin/xlint/lint1/msg_247.c:1.10
--- src/tests/usr.bin/xlint/lint1/msg_247.c:1.9 Thu Apr 8 19:08:17 2021
+++ src/tests/usr.bin/xlint/lint1/msg_247.c Thu Apr 8 19:20:54 2021
@@ -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 @@ new_type_interface(void)
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++;
Index: src/tests/usr.bin/xlint/lint1/msg_247.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_247.exp:1.6 src/tests/usr.bin/xlint/lint1/msg_247.exp:1.7
--- src/tests/usr.bin/xlint/lint1/msg_247.exp:1.6 Thu Apr 8 19:08:17 2021
+++ src/tests/usr.bin/xlint/lint1/msg_247.exp Thu Apr 8 19:20:54 2021
@@ -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]
Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.271 src/usr.bin/xlint/lint1/tree.c:1.272
--- src/usr.bin/xlint/lint1/tree.c:1.271 Tue Apr 6 21:59:58 2021
+++ src/usr.bin/xlint/lint1/tree.c Thu Apr 8 19:20:54 2021
@@ -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 @@ check_pointer_integer_conversion(op_t op
}
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)