Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: add test for struct po...



details:   https://anonhg.NetBSD.org/src/rev/26dc93ccc920
branches:  trunk
changeset: 961069:26dc93ccc920
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Apr 08 19:08:17 2021 +0000

description:
tests/lint: add test for struct pointer cast

diffstat:

 tests/usr.bin/xlint/lint1/msg_247.c   |  44 ++++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_247.exp |   1 +
 2 files changed, 44 insertions(+), 1 deletions(-)

diffs (62 lines):

diff -r 199e35043dd3 -r 26dc93ccc920 tests/usr.bin/xlint/lint1/msg_247.c
--- a/tests/usr.bin/xlint/lint1/msg_247.c       Thu Apr 08 19:06:57 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.c       Thu Apr 08 19:08:17 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_247.c,v 1.8 2021/03/30 15:05:05 rillig Exp $       */
+/*     $NetBSD: msg_247.c,v 1.9 2021/04/08 19:08:17 rillig Exp $       */
 # 3 "msg_247.c"
 
 // Test for message: pointer cast from '%s' to '%s' may be troublesome [247]
@@ -75,3 +75,45 @@
 {
        return (char *)(void *)arg;
 }
+
+
+/*
+ * When implementing types that have a public part that is exposed to the user
+ * (in this case 'struct counter') and a private part that is only visible to
+ * the implementation (in this case 'struct counter_impl'), a common
+ * implementation technique is to use a struct in which the public part is the
+ * first member.  C guarantees that the pointer to the first member is at the
+ * same address as the pointer to the whole struct.
+ *
+ * Seen in external/mpl/bind/dist/lib/isc/mem.c for struct isc_mem and
+ * isc__mem.
+ */
+
+struct counter {
+       int count;
+};
+
+struct counter_impl {
+       struct counter public_part;
+       int saved_count;
+};
+
+void *allocate(void);
+
+struct counter *
+new_type_interface(void)
+{
+       struct counter_impl *impl = allocate();
+       impl->public_part.count = 12345;
+       impl->saved_count = 12346;
+       return &impl->public_part;
+}
+
+void
+counter_increment(struct counter *counter)
+{
+       /* expect+1: 247 */
+       struct counter_impl *impl = (struct counter_impl *)counter;
+       impl->saved_count = impl->public_part.count;
+       impl->public_part.count++;
+}
diff -r 199e35043dd3 -r 26dc93ccc920 tests/usr.bin/xlint/lint1/msg_247.exp
--- a/tests/usr.bin/xlint/lint1/msg_247.exp     Thu Apr 08 19:06:57 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.exp     Thu Apr 08 19:08:17 2021 +0000
@@ -1,2 +1,3 @@
 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]



Home | Main Index | Thread Index | Old Index