From c2ab04f9e57d248094640e87fdfe3f9454a9cd5d Mon Sep 17 00:00:00 2001
From: nathan <nathan@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Mon, 28 Nov 2005 10:34:30 +0000
Subject: [PATCH] .: 	PR c++/21166 	* c-decl.c (finish_struct):  Only set
 DECL_PACKED on a field 	when its natural alignment is > BITS_PER_UNIT.
 	* stor-layout.c (finalize_type_size): Revert my patch of 2005-08-08. 
 * c-common.c (handle_packed_attribute): Ignore packing on a field 	whose
 type is naturally char aligned. cp: 	PR c++/21166 	* class.c
 (check_field_decls): Only set DECL_PACKED on a field 	when its natural
 alignment is > BITS_PER_UNIT.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@107599 138bc75d-0d04-0410-961f-82ee72b054a4
---
 gcc/ChangeLog     |  9 +++++++++
 gcc/c-common.c    | 14 ++++++++++----
 gcc/c-decl.c      |  4 +++-
 gcc/cp/ChangeLog  |  6 ++++++
 gcc/cp/class.c    |  2 +-
 gcc/stor-layout.c | 11 -----------
 6 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index a1b40fdf81ee..35d08ba1185a 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2005-11-28  Nathan Sidwell  <nathan@codesourcery.com>
+
+	PR c++/21166
+	* c-decl.c (finish_struct):  Only set DECL_PACKED on a field
+	when its natural alignment is > BITS_PER_UNIT.
+	* stor-layout.c (finalize_type_size): Revert my patch of 2005-08-08.
+	* c-common.c (handle_packed_attribute): Ignore packing on a field
+	whose type is naturally char aligned.
+
 2005-11-28  Richard Guenther  <rguenther@suse.de>
 
 	* c-common.c (strict_aliasing_warning): Handle all
diff --git a/gcc/c-common.c b/gcc/c-common.c
index b416a5d3648a..43c147b30686 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -4065,17 +4065,23 @@ handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
 
 	     struct Foo {
 	       struct Foo const *ptr; // creates a variant w/o packed flag
-	       } __ attribute__((packed)); // packs it now.
-	  */
+	     } __ attribute__((packed)); // packs it now.
+	   */
 	  tree probe;
 
 	  for (probe = *node; probe; probe = TYPE_NEXT_VARIANT (probe))
 	    TYPE_PACKED (probe) = 1;
 	}
-
     }
   else if (TREE_CODE (*node) == FIELD_DECL)
-    DECL_PACKED (*node) = 1;
+    {
+      if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT)
+	warning (OPT_Wattributes,
+		 "%qE attribute ignored for field of type %qT",
+		 name, TREE_TYPE (*node));
+      else
+	DECL_PACKED (*node) = 1;
+    }
   /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
      used for DECL_REGISTER.  It wouldn't mean anything anyway.
      We can't set DECL_PACKED on the type of a TYPE_DECL, because
diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index a86cfdc01b64..44e03abacdcc 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -5326,7 +5326,9 @@ finish_struct (tree t, tree fieldlist, tree attributes)
   for (x = fieldlist; x; x = TREE_CHAIN (x))
     {
       DECL_CONTEXT (x) = t;
-      DECL_PACKED (x) |= TYPE_PACKED (t);
+
+      if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
+	DECL_PACKED (x) = 1;
 
       /* If any field is const, the structure type is pseudo-const.  */
       if (TREE_READONLY (x))
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 71f7d0bb44da..785c926e6574 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2005-11-28  Nathan Sidwell  <nathan@codesourcery.com>
+
+	PR c++/21166
+	* class.c (check_field_decls): Only set DECL_PACKED on a field
+	when its natural alignment is > BITS_PER_UNIT.
+
 2005-11-27  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
 
 	PR c++/24979
diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 7c43d3b242ed..647b7cb67b99 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -2801,7 +2801,7 @@ check_field_decls (tree t, tree *access_decls,
 		  (0,
 		   "ignoring packed attribute on unpacked non-POD field %q+#D",
 		   x);
-	      else
+	      else if (TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
 		DECL_PACKED (x) = 1;
 	    }
 
diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c
index 97104d1af6ba..54ab5ac14a50 100644
--- a/gcc/stor-layout.c
+++ b/gcc/stor-layout.c
@@ -1495,8 +1495,6 @@ finalize_type_size (tree type)
 void
 finish_record_layout (record_layout_info rli, int free_p)
 {
-  tree field;
-  
   /* Compute the final size.  */
   finalize_record_size (rli);
 
@@ -1506,15 +1504,6 @@ finish_record_layout (record_layout_info rli, int free_p)
   /* Perform any last tweaks to the TYPE_SIZE, etc.  */
   finalize_type_size (rli->t);
 
-  /* We might be able to clear DECL_PACKED on any members that happen
-     to be suitably aligned (not forgetting the alignment of the type
-     itself).  */
-  for (field = TYPE_FIELDS (rli->t); field; field = TREE_CHAIN (field))
-    if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field)
-	&& DECL_OFFSET_ALIGN (field) >= TYPE_ALIGN (TREE_TYPE (field))
-	&& TYPE_ALIGN (rli->t) >= TYPE_ALIGN (TREE_TYPE (field)))
-      DECL_PACKED (field) = 0;
-  
   /* Lay out any static members.  This is done now because their type
      may use the record's type.  */
   while (rli->pending_statics)
-- 
GitLab