This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix 11072
- From: Nathan Sidwell <nathan at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 02 Jul 2003 15:31:12 +0100
- Subject: [PATCH] Fix 11072
- Organization: Codesourcery LLC
Hi,
I've installed this obvious patch for 11072. It reverts a fix for DR 273,
but that fix was broken. Unfortunately it doesn't seem possible to
implement offsetof in C++ without a compiler extension.
built & tested on i686-pc-linux-gnu.
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
The voices in my head said this was stupid too
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk
2003-07-02 Nathan Sidwell <nathan@codesourcery.com>
PR c++/11072
* ginclude/stddef.h (offsetof): Remove cast to 'char &'. Explain why.
2003-07-02 Nathan Sidwell <nathan@codesourcery.com>
PR c++/11072
* g++.dg/other/offsetof2.C: XFAIL.
* g++.dg/other/offsetof5.C: New.
Index: ginclude/stddef.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/ginclude/stddef.h,v
retrieving revision 1.18
diff -c -3 -p -r1.18 stddef.h
*** ginclude/stddef.h 22 Apr 2003 12:28:49 -0000 1.18
--- ginclude/stddef.h 2 Jul 2003 14:26:27 -0000
*************** typedef __WINT_TYPE__ wint_t;
*** 409,424 ****
#ifdef _STDDEF_H
! /* Offset of member MEMBER in a struct of type TYPE. */
#ifndef __cplusplus
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
! #else /* C++ */
! /* The reference cast is necessary to thwart an operator& that might
! be applicable to MEMBER's type. See DR 273 for details. */
#define offsetof(TYPE, MEMBER) (reinterpret_cast <size_t> \
! (&reinterpret_cast <char &>(static_cast <TYPE *> (0)->MEMBER)))
#endif /* C++ */
-
#endif /* _STDDEF_H was defined this time */
#endif /* !_STDDEF_H && !_STDDEF_H_ && !_ANSI_STDDEF_H && !__STDDEF_H__
--- 409,434 ----
#ifdef _STDDEF_H
! /* Offset of member MEMBER in a struct of type TYPE. */
#ifndef __cplusplus
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
! #else
! /* In C++ a POD type can have a user defined address-of operator, and
! that will break offsetof. C++ core defect 273 addresses this and
! claims that reinterpret_casts to char & type are sufficient to
! overcome this problem.
!
! (reinterpret_cast <size_t>
! (&reinterpret_cast <char &>(static_cast <TYPE *> (0)->MEMBER)))
!
! But, such casts are not permitted in integral constant expressions,
! which offsetof is supposed to be.
!
! It appears that offsetof is unimplementable in C++ without a
! compiler extension. */
#define offsetof(TYPE, MEMBER) (reinterpret_cast <size_t> \
! (&static_cast<TYPE *> (0)->MEMBER))
#endif /* C++ */
#endif /* _STDDEF_H was defined this time */
#endif /* !_STDDEF_H && !_STDDEF_H_ && !_ANSI_STDDEF_H && !__STDDEF_H__
Index: testsuite/g++.dg/other/offsetof2.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/other/offsetof2.C,v
retrieving revision 1.1
diff -c -3 -p -r1.1 offsetof2.C
*** testsuite/g++.dg/other/offsetof2.C 22 Apr 2003 12:28:51 -0000 1.1
--- testsuite/g++.dg/other/offsetof2.C 2 Jul 2003 14:26:27 -0000
***************
*** 1,10 ****
! // { dg-do run }
// { dg-options -Wold-style-cast }
// Copyright (C) 2003 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 22 Apr 2003 <nathan@codesourcery.com>
// DR273 POD can have an operator&, offsetof is still required to work
#include <stddef.h>
--- 1,12 ----
! // { dg-do run { xfail *-*-* } }
// { dg-options -Wold-style-cast }
// Copyright (C) 2003 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 22 Apr 2003 <nathan@codesourcery.com>
// DR273 POD can have an operator&, offsetof is still required to work
+
+ // XFAILED - you can't write offsetof without an extension
#include <stddef.h>
Index: testsuite/g++.dg/other/offsetof5.C
===================================================================
RCS file: testsuite/g++.dg/other/offsetof5.C
diff -N testsuite/g++.dg/other/offsetof5.C
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- testsuite/g++.dg/other/offsetof5.C 2 Jul 2003 14:26:27 -0000
***************
*** 0 ****
--- 1,17 ----
+ // { dg-do compile }
+
+ // Copyright (C) 2003 Free Software Foundation, Inc.
+ // Contributed by Nathan Sidwell 30 June 2003 <nathan@codesourcery.com>
+
+ // PR c++ 11072, DR 273's solution is broken
+
+ #include <stddef.h>
+
+ struct F
+ {
+ char i;
+ char j;
+ };
+
+ static int ary[offsetof (F, j)];
+