aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorNathan Sidwell <nathan@codesourcery.com>2003-07-02 14:30:53 +0000
committerNathan Sidwell <nathan@gcc.gnu.org>2003-07-02 14:30:53 +0000
commit300e89a2b66eb8d9d9467168251927525d5c4fe8 (patch)
treec7270cd5c87616a67700ed7086e69da94972c8fc /gcc
parenteaac6968d6102aa421ef9109d2440b520fad0a76 (diff)
downloadgcc-300e89a2b66eb8d9d9467168251927525d5c4fe8.zip
gcc-300e89a2b66eb8d9d9467168251927525d5c4fe8.tar.gz
gcc-300e89a2b66eb8d9d9467168251927525d5c4fe8.tar.bz2
re PR c++/11072 (Implementation of offsetof macro)
PR c++/11072 * ginclude/stddef.h (offsetof): Remove cast to 'char &'. Explain why. testsuite: PR c++/11072 * g++.dg/other/offsetof2.C: XFAIL. * g++.dg/other/offsetof5.C: New. From-SVN: r68831
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog6
-rw-r--r--gcc/ginclude/stddef.h22
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/g++.dg/other/offsetof2.C4
-rw-r--r--gcc/testsuite/g++.dg/other/offsetof5.C17
5 files changed, 46 insertions, 7 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index c832f24..89afad4 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2003-07-02 Nathan Sidwell <nathan@codesourcery.com>
+
+ PR c++/11072
+ * ginclude/stddef.h (offsetof): Remove cast to 'char &'. Explain why.
+
2003-07-02 Andreas Schwab <schwab@suse.de>
* dbxout.c (pending_bincls): Only define if DBX_DEBUGGING_INFO.
@@ -6209,6 +6214,7 @@ Fri May 23 21:19:31 CEST 2003 Jan Hubicka <jh@suse.cz>
2003-05-18 Neil Booth <neil@daikokuya.co.uk>
* config/sparc/sparc.h: Define sparc for now.
+
2003-05-18 Nathanael Nerode <neroden@gcc.gnu.org>
* config.gcc: Clear xm_file, md_file at the beginning of each pass.
diff --git a/gcc/ginclude/stddef.h b/gcc/ginclude/stddef.h
index ad091ea..d19d78a 100644
--- a/gcc/ginclude/stddef.h
+++ b/gcc/ginclude/stddef.h
@@ -409,16 +409,26 @@ typedef __WINT_TYPE__ wint_t;
#ifdef _STDDEF_H
-/* Offset of member MEMBER in a struct of type TYPE. */
+/* 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. */
+#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> \
- (&reinterpret_cast <char &>(static_cast <TYPE *> (0)->MEMBER)))
+ (&static_cast<TYPE *> (0)->MEMBER))
#endif /* C++ */
-
#endif /* _STDDEF_H was defined this time */
#endif /* !_STDDEF_H && !_STDDEF_H_ && !_ANSI_STDDEF_H && !__STDDEF_H__
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 7f3678c..78f0f59 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,5 +1,9 @@
2003-07-02 Nathan Sidwell <nathan@codesourcery.com>
+ PR c++/11072
+ * g++.dg/other/offsetof2.C: XFAIL.
+ * g++.dg/other/offsetof5.C: New.
+
PR c++/10219
* g++.dg/template/error1.C: New.
diff --git a/gcc/testsuite/g++.dg/other/offsetof2.C b/gcc/testsuite/g++.dg/other/offsetof2.C
index 3ab6398..64b4fbd 100644
--- a/gcc/testsuite/g++.dg/other/offsetof2.C
+++ b/gcc/testsuite/g++.dg/other/offsetof2.C
@@ -1,4 +1,4 @@
-// { dg-do run }
+// { dg-do run { xfail *-*-* } }
// { dg-options -Wold-style-cast }
// Copyright (C) 2003 Free Software Foundation, Inc.
@@ -6,6 +6,8 @@
// DR273 POD can have an operator&, offsetof is still required to work
+// XFAILED - you can't write offsetof without an extension
+
#include <stddef.h>
struct POD1
diff --git a/gcc/testsuite/g++.dg/other/offsetof5.C b/gcc/testsuite/g++.dg/other/offsetof5.C
new file mode 100644
index 0000000..40a4406
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/offsetof5.C
@@ -0,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)];
+