summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>2003-04-22 12:28:51 +0000
committernathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>2003-04-22 12:28:51 +0000
commit8f95801871f9a18fcefd6868c6b709425e744d2b (patch)
treecf90264870e497c09eb9a301d929a33fbb4c69f2
parent09258369da6ac4b9bdbcc2902e258b4b67e7c87b (diff)
downloadgcc-8f95801871f9a18fcefd6868c6b709425e744d2b.tar.gz
.:
* ginclude/stddef.h: Provide C++ safe offsetof. testsuite: * g++.dg/other/offsetof2.C: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@65936 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/ChangeLog4
-rw-r--r--gcc/ginclude/stddef.h8
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/g++.dg/other/offsetof2.C47
4 files changed, 62 insertions, 1 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 9965b90bca1..d5b8370daf1 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,7 @@
+2003-04-22 Nathan Sidwell <nathan@codesourcery.com>
+
+ * ginclude/stddef.h: Provide C++ safe offsetof.
+
2003-04-22 J"orn Rennecke <joern.rennecke@superh.com>
* function.c (purge_addressof_1): In (mem (addressof (reg))) case
diff --git a/gcc/ginclude/stddef.h b/gcc/ginclude/stddef.h
index f1046f44bb1..ad091ea1383 100644
--- a/gcc/ginclude/stddef.h
+++ b/gcc/ginclude/stddef.h
@@ -410,8 +410,14 @@ typedef __WINT_TYPE__ wint_t;
#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 */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 577a6090432..8328263e424 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2003-04-22 Nathan Sidwell <nathan@codesourcery.com>
+
+ * g++.dg/other/offsetof2.C: New test.
+
2003-04-21 Mark Mitchell <mark@codesourcery.com>
* g++.dg/template/recurse.C: Adjust location of error messages.
diff --git a/gcc/testsuite/g++.dg/other/offsetof2.C b/gcc/testsuite/g++.dg/other/offsetof2.C
new file mode 100644
index 00000000000..3ab63981d9b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/offsetof2.C
@@ -0,0 +1,47 @@
+// { 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>
+
+struct POD1
+{
+ int m;
+
+ void *operator& () const {return 0;} // yes, still a pod!
+};
+
+struct POD2
+{
+ int m;
+};
+
+void *operator& (POD2 const &) {return 0;} // ouch!
+
+struct POD3
+{
+ int prefix;
+
+ POD1 m;
+};
+
+struct POD4
+{
+ int prefix;
+
+ POD1 m;
+};
+
+int main ()
+{
+ if (offsetof (POD3, m) != sizeof (int))
+ return 1;
+ if (offsetof (POD4, m) != sizeof (int))
+ return 2;
+ return 0;
+}
+