This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
ginclude/stddef.h patch (don't redefine offsetof)
- To: gcc-patches at gcc dot gnu dot org
- Subject: ginclude/stddef.h patch (don't redefine offsetof)
- From: achurch at achurch dot org (Andrew Church)
- Date: Mon, 30 Apr 2001 09:36:06 JST
The following is a patch to fix gcc's redefining offsetof even after
stddef.h has been #included normally. The problem arises when an include
file tries to use "#define __need_XXX" to get a specific definition from
stddef.h; since stddef.h defines offsetof even if _STDDEF_H is already
defined, any user definition is overriden, which can lead to inappropriate
errors (see example below). While this can admittedly be solved by
rearranging #includes, I think a better solution is to not define offsetof
if it is already defined, and I have included a patch at the bottom of this
message which does that (against gcc-2.95.2).
Example of code that fails: (time-xxx.h is excerpted from
/usr/include/time.h of glibc-2.1.3)
----------------- main.c ---------------
#include <stddef.h>
#undef offsetof
#define offsetof(a,b) ((char *)&(a).b - (char *)&(a))
#include "time-xxx.h"
struct {
int y, z;
} x;
int main() {
printf("%d\n", offsetof(x,z));
return 0;
}
----------------------------------------
--------------- time-xxx.h -------------
/* Get size_t and NULL from <stddef.h>. */
# define __need_size_t
# define __need_NULL
# include <stddef.h>
----------------------------------------
--Andrew Church
achurch@achurch.org | New address - please note.
http://achurch.org/ | メールアドレスが変わりました。
---------------------------------------------------------------------------
ChangeLog:
Mon Apr 30 09:35:34 JST 2001 Andrew Church <achurch@achurch.org>
* ginclude/stddef.h: Avoid redefining offsetof if it is already
defined.
*** gcc/ginclude/stddef.h.old Thu Dec 17 06:19:25 1998
--- gcc/ginclude/stddef.h Mon Apr 30 09:35:34 2001
***************
*** 334,340 ****
--- 334,342 ----
/* Offset of member MEMBER in a struct of type TYPE. */
+ #ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+ #endif
#endif /* _STDDEF_H was defined this time */