[cfe-dev] The lexical DeclContext of class template instantiations.

Enea Zaffanella zaffanella at cs.unipr.it
Tue Aug 21 02:51:11 PDT 2012


Ping^3.

I no one has objections, we would like to apply this patch
(passes all clang tests).

Enea.


On 07/31/2012 10:51 AM, Enea Zaffanella wrote:
> Ping^2.
>
> On 07/13/2012 09:17 AM, Enea Zaffanella wrote:
>> Ping.
>>
>> On 07/08/2012 07:19 PM, Enea Zaffanella wrote:
>>> Hello.
>>>
>>> I would like to hear some informed opinions on the following, which
>>> seems to be an AST quality issue.
>>>
>>> When parsing the following file:
>>>
>>> $ cat lex_dc.cc
>>> template <typename T>
>>> struct Outer {
>>>       template <typename U> static void InnerFun(T, U) {}
>>>       template <typename U> struct InnerRecord { T* t; U u; };
>>> };
>>>
>>> struct S {
>>>       static void bar() { Outer<S>::InnerFun<int>(S(), 5); }
>>>       Outer<S>::InnerRecord<int> is;
>>> };
>>>
>>>
>>> the AST generated by clang contains implicit instantiations for
>>>        Outer<S>::InnerFun<int>
>>> and
>>>        Outer<S>::InnerRecord<int>.
>>>
>>> It happens that the function Outer<S>::InnerFun<int> has the same
>>> semantic and lexical DeclContext, namely struct Outer<S>.
>>>
>>> In contrast, the record Outer<S>::InnerRecord<int> has Outer<S> as the
>>> semantic DeclContext, but it has struct S as the lexical one.
>>>
>>> This looks quite odd ... for instance, one of the consequences is that,
>>> when querying Outer<S>::InnerRecord<int> with method
>>>        bool isOutOfLine()
>>> we would be obtaining a positive answer.
>>>
>>> Afaict, the problem is caused by method
>>>
>>> QualType
>>> Sema::CheckTemplateIdType(
>>>         TemplateName Name,
>>>         SourceLocation TemplateLoc,
>>>         TemplateArgumentListInfo &TemplateArgs);
>>>
>>> around line 2077 of SemaTemplate.cpp, where it does
>>>
>>>       ClassTemplate->AddSpecialization(Decl, InsertPos);
>>>       Decl->setLexicalDeclContext(CurContext);
>>>
>>> Am I guessing correctly if I say that it should be something like
>>>
>>>       ClassTemplate->AddSpecialization(Decl, InsertPos);
>>>       if (ClassTemplate->isOutOfLine())
>>>         Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext());
>>>
>>> ?
>>>
>>> Enea.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: class_templ_inst_lexdc.patch
Type: text/x-diff
Size: 646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120821/2f8d375f/attachment.patch>


More information about the cfe-dev mailing list