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]

[C++ PATCH] [PR19508] Do not apply attributes to template parms


Hello,

my patch to apply attributes to TYPE_DECL fields:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl2.c.diff?cvsroot=gcc&r1=1.753&r2=1.754

does not work in some situations within templates: in fact, as we well know,
tsubst ignores attributes, so most of the time things just work because the
attributes is copied down into the instantiated class; other time, when the
attribute is bound to a type that tsubst rebuilds from scratch, the attribute
gets lost. See for instance my other patch for PR 19163 (pending a discussion
still open with Mark):
http://gcc.gnu.org/ml/gcc-patches/2004-12/msg02003.html

The long-term plan devised by Mark is to store the raw attribute list aside,
and then apply it to the instantiated types. This makes a lot of sense, but
it's unfeasable for 4.0, and my patch works for everything but some kind of
dependent types after all.

Now, let me introduce you the problem in the PR: it is an ICE in dwarf2out with
the following testcase (compiled with -g of course):

-------------------------------------------------------
template <typename T>
struct BVector
{
  typedef T value_type __attribute__ ((aligned(8)));
};
BVector<int> m;

-------------------------------------------------------

It is caused by the fact that gen_typedef_die sees a TEMPLATE_TYPE_PARM within
the DECL_ORIGINAL_TYPE of a TYPE_DECL. The DECL_ORIGINAL_TYPE is set by the
attribute machinery while applying the attribute to 'T': the comment above says
that it is necessary to copy the type to make sure we do not modify a builtin
type. So, the TYPE_DECL representing the "T" field in the template above has a
TREE_TYPE which is a TEMPLATE_TYPE_PARM with the attribute applied to it, and a
DECL_ORIGINAL_TYPE which is the original TEMPLATE_TYPE_PARM.

When we instantiate the template, tsubst() totally ignores DECL_ORIGINAL_TYPE:
in fact, before my patch to grokfields linked at the top of this mail, it was
always NULL. But now, the TEMPLATE_TYPE_PARM within DECL_ORIGINAL_TYPE survives
all the way down to dwarf2out which does not know how to handle it and aborts.

My first attempt at fixing this bug was to tsubst() also the
DECL_ORIGINAL_TYPE. This had the effect to avoid the ICE, but of course the
attribute was still ignored: in fact, it is lost while tsubsting the
TEMPLATE_TYPE_PARM with the argument. So it did not look like a proper solution
to me.

I ended up with this patch, which just avoids to apply the attributes to a
TEMPLATE_TYPE_PARM, and also warns the user about the attribute being ignored.
I am using a warning() rather than a sorry(), but I am not sure it is the right
choice. After all, it is sufficient to comment the attribute out to avoid the
diagnostic (since it was ignored in all GCC versions), so a sorry() might do as
well.

Tested on i686-pc-linux-gnu, OK for mainline?

Giovanni Bajo


cp/
        PR c++/19508
        * decl2.c (grokfield): Do not apply attributes to template parameters
        as they are ignored by tsubst anyway.

testsuite/
        PR c++/19508
        * g++.dg/ext/attrib19.C: New test.

Attachment: patch-pr19508.txt
Description: Text document


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