This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for c++/7046 (#pragma pack and class templates)
- From: Jason Merrill <jason at redhat dot com>
- To: gcc-patches List <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 05 Nov 2009 22:36:23 -0500
- Subject: C++ PATCH for c++/7046 (#pragma pack and class templates)
We don't use TYPE_PRECISION for anything in class templates, so we might
as well use it to remember the value of maximum_field_alignment.
Tested x86_64-pc-linux-gnu, applied to trunk.
commit 79f9e0a74065393ef6a48744ee18d47a567df655
Author: Jason Merrill <jason@redhat.com>
Date: Thu Nov 5 18:27:37 2009 -0500
PR c++/7046
* class.c (finish_struct): Store maximum_field_alignment in
TYPE_PRECISION.
* pt.c (instantiate_class_template): Set maximum_field_alignment.
diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index dc4c6b3..4020144 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -5516,6 +5516,9 @@ finish_struct (tree t, tree attributes)
if (DECL_PURE_VIRTUAL_P (x))
VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
complete_vars (t);
+
+ /* Remember current #pragma pack value. */
+ TYPE_PRECISION (t) = maximum_field_alignment;
}
else
finish_struct_1 (t);
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index d4556cd..75180ea 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -7352,6 +7352,7 @@ instantiate_class_template (tree type)
tree typedecl;
tree pbinfo;
tree base_list;
+ unsigned int saved_maximum_field_alignment;
if (type == error_mark_node)
return error_mark_node;
@@ -7412,6 +7413,9 @@ instantiate_class_template (tree type)
push_deferring_access_checks (dk_no_deferred);
push_to_top_level ();
+ /* Use #pragma pack from the template context. */
+ saved_maximum_field_alignment = maximum_field_alignment;
+ maximum_field_alignment = TYPE_PRECISION (pattern);
SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
@@ -7827,6 +7831,7 @@ instantiate_class_template (tree type)
perform_typedefs_access_check (pattern, args);
perform_deferred_access_checks ();
pop_nested_class ();
+ maximum_field_alignment = saved_maximum_field_alignment;
pop_from_top_level ();
pop_deferring_access_checks ();
pop_tinst_level ();
diff --git a/gcc/testsuite/g++.dg/abi/pragma-pack1.C b/gcc/testsuite/g++.dg/abi/pragma-pack1.C
new file mode 100644
index 0000000..d90fc20
--- /dev/null
+++ b/gcc/testsuite/g++.dg/abi/pragma-pack1.C
@@ -0,0 +1,37 @@
+// PR c++/7046
+
+extern "C" int printf (const char *, ...);
+
+#pragma pack(4)
+
+template <typename X >
+struct T
+{
+ char x1; /* Usually 3 padding bytes are added after x1 member. */
+ int x2;
+};
+
+template <class T>
+int f()
+{
+ struct A { char i1; int i2; };
+ return sizeof (A);
+}
+
+#pragma pack(1)
+template struct T<int>; /* T<int> is instantiated here */
+template int f<int>();
+
+#pragma pack(4)
+template struct T<float>; /* T<float> is instantiated here */
+template int f<double>();
+
+int main()
+{
+ printf("sizeof T<int> = %d\n", sizeof(T<int>));
+ printf("sizeof T<float> = %d\n", sizeof(T<float>));
+ printf("f<int>() = %d\n", f<int>());
+ printf("f<float>() = %d\n", f<float>());
+ return (sizeof(T<int>) != sizeof(T<float>)
+ || f<int>() != f<float>());
+}