From cd2d09063cdfb0e730e7d3c344c85716d3217028 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 2 Jun 2016 11:28:54 +0930 Subject: [PATCH] tcon: add test case for const members. Signed-off-by: Rusty Russell --- ccan/tcon/tcon.h | 2 ++ ccan/tcon/test/compile_ok-container.c | 35 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 ccan/tcon/test/compile_ok-container.c diff --git a/ccan/tcon/tcon.h b/ccan/tcon/tcon.h index 247a7331..e0f84b38 100644 --- a/ccan/tcon/tcon.h +++ b/ccan/tcon/tcon.h @@ -321,6 +321,8 @@ * tcon_container_of() evaluates to a pointer to the container * structure. With sufficient compiler support, the pointer will be * correctly typed, and the type of @member_ptr will be verified. + * Note that const is discarded; a const @member_ptr still yields a + * non-const container (unless @canary is const). * * Returns NULL if @member_ptr is NULL. */ diff --git a/ccan/tcon/test/compile_ok-container.c b/ccan/tcon/test/compile_ok-container.c new file mode 100644 index 00000000..9493c4dc --- /dev/null +++ b/ccan/tcon/test/compile_ok-container.c @@ -0,0 +1,35 @@ +#include + +#include +#include +#include + +struct inner { + int inner_val; +}; + +struct outer { + int outer_val; + struct inner inner; +}; + +struct info_base { + char *infop; +}; + +struct info_tcon { + struct info_base base; + TCON(TCON_CONTAINER(concan, struct outer, inner)); +}; + +int main(int argc, char *argv[]) +{ + /* Const should work! */ + const struct outer *ovar; + struct outer *o; + struct info_tcon info; + + o = tcon_container_of(&info, fi, &ovar->inner); + return (o == ovar); +} + -- 2.39.2