<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">For functions and static data members, we instantiate their attributes as part of instantiating the declaration. For classes, we only instantiate most attributes as part of instantiating the definition; only attributes marked as "MeaningfulToClassTemplateDefinition" are instantiated as part of the declaration. (And yes, that name is reversed from what the flag means, and in any case should not mention templates as it should affect classes not templates...)<div><br></div><div>However, it looks like we're missing the call to instantiate the early-instantiated attributes when we instantiate a member class of a class template specialization. (See TemplateDeclInstantiator::VisitCXXRecordDecl, and note that it does not call InstantiateAttrsForDecl.) The same problem appears to exist for member class templates (TemplateDeclInstantiator::VisitClassTemplateDecl), member partial specializations (TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization), and member explicit specializations (TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl).</div><div><br></div><div>This bug can easily be observed prior to your patch:</div><div><br></div><div><div>template<typename T> struct A { struct __attribute__((abi_tag("foo"))) X {}; };</div><div>#if CHANGE_MANGLING</div><div>A<int>::X axi;</div><div>#endif</div><div>A<int>::X *f() { return 0; }</div></div><div><br></div><div>Note that with CHANGE_MANGLING not defined, we mangle f as _Z1fv, but with it defined, we instead mangle f as _Z1fB3foov. Oops!</div><div><br></div><div>So, we should fix all those places above so that they call InstantiateAttrsForDecl, as a precursor to your patch. We should also rename MeaningfulToClassTemplateDefinition to something that reflects what it actually means. (Maybe reverse the sense of it and rename to OnlyMeaningfulOnDefinition, or rename it to InstantiateWithDeclaration, or something like that.)</div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, 7 Sep 2018 at 08:21, Louis Dionne via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for  your reply. Unfortunately, naively adding<br>
<br>
        let MeaningfulToClassTemplateDefinition = 1;<br>
<br>
to the attribute definition does not seem to solve the problem. In Sema::InstantiateClassMembers(), I still see:<br>
<br>
  InstantiateClassMembers(): D->getNameAsString() = member_class<br>
  InstantiateClassMembers(): D->hasAttr<NoExternTemplateAttr>() = false<br>
<br>
Like I said, this does not happen for member functions or static data members. Member classes must be treated specially somehow but I can’t find out where.<br>
<br>
Louis<br>
<br>
> On Sep 7, 2018, at 11:14, Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>> wrote:<br>
> <br>
> I seem to remember adding a bit to the Attr.td class to do exactly this (have an attribute propagate to instantiations).  It seems to have changed names unfortunately and my git-fu seems to have failed me here, but there if I would guess, I'd say the following is the current incantation:<br>
> <br>
> // Set to true if this attribute meaningful when applied to or inherited<br>
>  // in a class template definition.<br>
>  bit MeaningfulToClassTemplateDefinition = 0;<br>
> <br>
> <br>
> You should be able to put <br>
> "MeaningfulToClassTemplateDefintion = 1" in your review and have it work I think?<br>
> <br>
> <br>
> <br>
> -----Original Message-----<br>
> From: cfe-dev [mailto:<a href="mailto:cfe-dev-bounces@lists.llvm.org" target="_blank">cfe-dev-bounces@lists.llvm.org</a>] On Behalf Of Louis Dionne via cfe-dev<br>
> Sent: Friday, September 7, 2018 8:08 AM<br>
> To: Clang Dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
> Subject: [cfe-dev] Question about Decls representing member classes of class templates<br>
> <br>
> Hi,<br>
> <br>
> This is a noob question.<br>
> <br>
> I'm working on the no_extern_template attribute discussed at [1] and I encountered something I don't understand in Clang. I traced through the code but couldn't find an answer, so I thought I'd ask here in case the question is easy to answer for someone else.<br>
> <br>
> I have the following class with an explicit template instantiation:<br>
> <br>
>    template <typename T><br>
>    struct Foo {<br>
>        struct __attribute__((no_extern_template)) member_class { };<br>
>    };<br>
> <br>
>    template struct Foo<int>;<br>
> <br>
> The no_extern_template is the attribute I'm trying to add. My problem is that in Sema::InstantiateClassMembers (triggered by the explicit instantiation), the Decl representing member_class does not seem to have any attribute applied to it. However, I was able to trace code in ProcessDeclAttribute() (in Sema/SemaDeclAttr.cpp) where a Decl representing member_class does have the no_extern_template associated to it. I also noticed that the Decl in ProcessDeclAttribute() and in Sema::InstantiateClassMembers() were different Decl objects, but both representing the same type (or at least they have the same getNameAsString()). Note that I don't have the same problem with static/non-static member functions -- only member classes seem to have multiple Decls going around with different attributes.<br>
> <br>
> My questions are:<br>
> - Why are there two Decl instances representing the same class?<br>
> - How can I make sure that all Decl instances representing the same class get the same attributes?<br>
> <br>
> I uploaded the patch I have so far here to give some context: <a href="https://reviews.llvm.org/D51789" rel="noreferrer" target="_blank">https://reviews.llvm.org/D51789</a><br>
> <br>
> Thanks,<br>
> Louis<br>
> <br>
> [1]: <a href="http://lists.llvm.org/pipermail/cfe-dev/2018-August/059024.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/cfe-dev/2018-August/059024.html</a><br>
> <br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>