Module Name: src Committed By: rillig Date: Sun Jan 24 17:44:37 UTC 2021
Modified Files: src/tests/usr.bin/xlint/lint1: msg_329.c msg_329.exp Log Message: lint: add test for message 329, union cast with incompatible type To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/xlint/lint1/msg_329.c \ src/tests/usr.bin/xlint/lint1/msg_329.exp 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_329.c diff -u src/tests/usr.bin/xlint/lint1/msg_329.c:1.1 src/tests/usr.bin/xlint/lint1/msg_329.c:1.2 --- src/tests/usr.bin/xlint/lint1/msg_329.c:1.1 Sat Jan 2 10:22:44 2021 +++ src/tests/usr.bin/xlint/lint1/msg_329.c Sun Jan 24 17:44:37 2021 @@ -1,7 +1,40 @@ -/* $NetBSD: msg_329.c,v 1.1 2021/01/02 10:22:44 rillig Exp $ */ +/* $NetBSD: msg_329.c,v 1.2 2021/01/24 17:44:37 rillig Exp $ */ # 3 "msg_329.c" // Test for message: type '%s' is not a member of '%s' [329] -TODO: "Add example code that triggers the above message." -TODO: "Add example code that almost triggers the above message." +union u { + int i1; + int i2; + void *vp; +}; + +void +example(void) +{ + /* + * A type cast to a union type is valid if the source type is any + * member type of the union. Since all union members with the same + * type have the same representation, the name of the union member + * doesn't matter. + * + * XXX: could there be padding bits or other tricky details that are + * settable per-member? These could make the type alone insufficient + * for determining the exact representation. + * + * C99 6.5.4 "Cast operators" does not mention a union cast. On the + * contrary, it says that the type name shall specify a scalar type. + * + * C11 6.5.4 "Cast operators" differs from C99 but still requires + * scalar types for both the target type and the source value. + * + * This is a GCC extension. + * See https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html. + * + * FIXME: lint says in message 328 that "union cast is a C9X feature", + * but that is wrong. It is a GCC feature. + */ + union u u_i1 = (union u)3; + union u u_vp = (union u)(void *)0; + union u u_cp = (union u)(char *)0; /* expect: 329 */ +} Index: src/tests/usr.bin/xlint/lint1/msg_329.exp diff -u src/tests/usr.bin/xlint/lint1/msg_329.exp:1.1 src/tests/usr.bin/xlint/lint1/msg_329.exp:1.2 --- src/tests/usr.bin/xlint/lint1/msg_329.exp:1.1 Sat Jan 2 10:22:44 2021 +++ src/tests/usr.bin/xlint/lint1/msg_329.exp Sun Jan 24 17:44:37 2021 @@ -1 +1 @@ -msg_329.c(6): syntax error ':' [249] +msg_329.c(39): type 'pointer to char' is not a member of 'union u' [329]