* tests/test-stddef.c: Add tests for offsetof. Signed-off-by: Eric Blake <ebl...@redhat.com> ---
I was about to push this, so we can detect any gotchas with using offsetof according to POSIX. Then I decided to test first. And the first loser is Solaris' /usr/bin/cc: "../../gltests/test-stddef.c", line 44: syntax error before or at: & "../../gltests/test-stddef.c", line 44: warning: syntax requires ";" after last struct/union member "../../gltests/test-stddef.c", line 44: zero-sized struct/union "../../gltests/test-stddef.c", line 44: cannot recover from previous errors cc: acomp failed for ../../gltests/test-stddef.c Adding "redundant" parens to the sizeof(offsetof()) call fixed the it for that compiler. Sure enough, /usr/include/iso/stddef_iso.h has: #define offsetof(s, m) (std::size_t)(&(((s *)0)->m)) So before I push, I guess we need to beef up the stddef module to detect and work around broken compilers/headers that don't properly parenthesize their offsetof macro. ChangeLog | 5 +++++ tests/test-stddef.c | 10 ++++++++++ 2 files changed, 15 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 86de94d..896557a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-08-16 Eric Blake <ebl...@redhat.com> + + test-stddef: test offsetof compliance + * tests/test-stddef.c: Add tests for offsetof. + 2010-08-15 Bruno Haible <br...@clisp.org> stpncpy: Allow stpncpy to be defined as a macro. diff --git a/tests/test-stddef.c b/tests/test-stddef.c index d047e57..be8bc5e 100644 --- a/tests/test-stddef.c +++ b/tests/test-stddef.c @@ -31,6 +31,16 @@ size_t c = 2; per POSIX 2008. */ verify (sizeof NULL == sizeof (void *)); +/* Check that offsetof produces integer constants with correct type. */ +struct d +{ + char e; + char f; +}; +verify (sizeof offsetof (struct d, e) == sizeof (size_t)); +verify (offsetof (struct d, e) < -1); /* Must be unsigned. */ +verify (offsetof (struct d, f) == 1); + int main (void) { -- 1.7.2.1