This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC] Re: [PATCH] Fix PR C++/9021 and 11005


 --- Jason Merrill wrote: 
> On Sat, 13 Dec 2003 07:31:10 +1100 (EST), Danny Smith
> <danny_r_smith_2001@yahoo.co.nz> wrote:
> 
> > If so, I need to find/write a function that checks if the new attributes
> are
> > contained in the old list 
> 
> attribute_list_contained.
> 
> Jason 

Hello, the attached revised patch fixes the two PR's, while reporting error if 
code attempts to add a new attribute to a complete type, but there are
problems:

1) With this patch, an attribute declared in a foward declaration of a type is
sticky and applies to the completed type when it is defined later.  eg,

struct __attribute__((packed)) B;
struct B
{
  int i;
  char c;
};


B is packed.  This is a new behaviour, and it differs from the behaviour
in C frontend which silent;y ignores the attribute on the forward
declaration.

2) The function attribute_list_contained has problems with things like
__attribute__((aligned(1))).

eg: 
struct E
{
  int i;
  char c;
} __attribute__((aligned(1)));
struct __attribute__((aligned(1))) E;  //  This should be OK

causes an error with my patch.

So I need help. Should addition of attributes to user-defined types be
allowed only _at_ definition (as suggested by Benjamin), and ignored (in
cp_parser_elaborated_type_specifier) for forward introduction of a new type
and re-declarations/friend declarations of existing types.  

If so, I will update patch and testcase.

Danny


cp/ChangeLog

	* decl.c (xref_tag): Merge attributes if type is not complete.

testsuite/ChangeLog

	* g++.dg/ext/attrib10.C: New file.

	
	  

Index: cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1159
diff -c -3 -p -r1.1159 decl.c
*** cp/decl.c	10 Dec 2003 06:34:38 -0000	1.1159
--- cp/decl.c	14 Dec 2003 20:01:28 -0000
*************** xref_tag (enum tag_types tag_code, tree 
*** 9445,9451 ****
  	redeclare_class_template (t, current_template_parms);
      }
  
!   TYPE_ATTRIBUTES (t) = attributes;
  
    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
  }
--- 9445,9461 ----
  	redeclare_class_template (t, current_template_parms);
      }
  
! if (attributes)
!     {
!       /* If the type is not yet complete, merge attributes.
! 	 Otherwise, report an error if there are new attributes.  */
!       if (!COMPLETE_TYPE_P (t))
! 	TYPE_ATTRIBUTES (t) = merge_attributes (attributes,
! 						TYPE_ATTRIBUTES (t));
!       else if (!attribute_list_contained (TYPE_ATTRIBUTES (t),
! 					  attributes))
! 	error ("attributes cannot be added to complete type.");
!     }
  
    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
  }
*** /dev/null	Mon Dec 15 09:13:21 2003
--- testsuite/g++.dg/ext/attrib10.C	Mon Dec 15 09:10:39 2003
***************
*** 0 ****
--- 1,54 ----
+ // Test that attributes on forward and re-declarations
+ // are merged correctly.  
+ 
+ struct A;
+ 
+ struct __attribute__((packed)) B;
+ 
+ struct __attribute__((packed)) A
+ {
+   int i;
+   char c;
+ };
+ 
+ struct B
+ {
+   int i;
+   char c;
+ };
+ 
+ char testA[sizeof(struct A) == sizeof(int) + sizeof(char) ? 1 : -1];
+ 
+ //  attribute from forward declaration
+ char testB[sizeof(struct B) == sizeof(struct A) ? 1 : -1];
+ 
+ struct __attribute__((packed)) C
+ {
+   int i;
+   char c;
+ };
+ 
+ // redeclaration of C without attribute doesn't lose attribute.
+ struct C;
+ char testC[sizeof(struct C) == sizeof(struct A) ? 1 : -1];
+ 
+ // redeclation with old attributes is OK
+ struct __attribute__ ((packed)) A;
+ 
+ // invalid addition of new attribute to complete type.
+ struct D
+ {
+   int i;
+   char c;
+ };
+ 
+ struct __attribute__((packed)) D;  // { dg-error "complete" }
+ 
+ // valid addition of contained attribute to complete type.
+ struct E
+ {
+   int i;
+   char c;
+ } __attribute((aligned(1)));
+ 
+ struct __attribute((aligned(1))) E;  //  This should be OK

http://personals.yahoo.com.au - Yahoo! Personals
New people, new possibilities. FREE for a limited time.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]