tcon: add test case for const members.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 2 Jun 2016 01:58:54 +0000 (11:28 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 2 Jun 2016 01:58:54 +0000 (11:28 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tcon/tcon.h
ccan/tcon/test/compile_ok-container.c [new file with mode: 0644]

index 247a7331c8b98aa52c7da44750b6db0c02c6c872..e0f84b383976c4d106e6d9fe3ee9043c449d1ed8 100644 (file)
  * 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 (file)
index 0000000..9493c4d
--- /dev/null
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+
+#include <ccan/tcon/tcon.h>
+#include <ccan/build_assert/build_assert.h>
+#include <ccan/tap/tap.h>
+
+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);
+}
+