This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] restore -Wunused-variable on a typedef'd variable in a function template (PR 79548)
- From: Jason Merrill <jason at redhat dot com>
- To: Martin Sebor <msebor at gmail dot com>
- Cc: Gcc Patch List <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 21 Mar 2017 15:28:03 -0400
- Subject: Re: [PATCH] restore -Wunused-variable on a typedef'd variable in a function template (PR 79548)
- Authentication-results: sourceware.org; auth=none
- References: <28dd4f25-b208-52af-af1e-7bc5e36db557@gmail.com> <2c85e43f-d1dc-a74e-4d5b-c00fca8588d3@redhat.com> <066019c5-c85e-8d26-102e-aeba34988416@gmail.com> <CADzB+2=D+cU8Lnq5eGqJzo=cXjL_YPHaj1M_RtnwxR97keYsug@mail.gmail.com> <0a4c97ac-fe30-2e3e-6694-9a3f9dbdf90c@gmail.com> <CADzB+2=4sDN0_ro3Wzh_3x-E20U7qarkb4qszXz5SZoHaL49NA@mail.gmail.com> <00eb2a46-a8e8-7744-a093-7eaad8c42e36@gmail.com> <CADzB+2nUGZ5N-dNap48GquOTYgMYCLD1j9tUo_Rxh9OWzM2HrA@mail.gmail.com> <a6cb940f-1c0b-8d5b-558f-ad8842fb4826@gmail.com> <CADzB+2mP7Fod12M=o6-GOHQ51emFqVngQdFdsJatUBHsYOFTQA@mail.gmail.com> <CADzB+2kuYqfCSOwMT1SEw8Dc+Tixwdo+Tv9HmLfKWcTsOyyFQg@mail.gmail.com> <32433606-776d-b579-81ce-e891ccc98a21@gmail.com>
On Tue, Mar 21, 2017 at 2:40 PM, Martin Sebor <msebor@gmail.com> wrote:
> On 03/20/2017 03:11 PM, Jason Merrill wrote:
>>
>> On Thu, Feb 23, 2017 at 6:33 PM, Jason Merrill <jason@redhat.com> wrote:
>>>
>>> On Thu, Feb 23, 2017 at 12:56 PM, Martin Sebor <msebor@gmail.com> wrote:
>>>>
>>>> On 02/22/2017 05:43 PM, Jason Merrill wrote:
>>>>>
>>>>> On Wed, Feb 22, 2017 at 3:44 PM, Martin Sebor <msebor@gmail.com> wrote:
>>>>>>
>>>>>> On 02/22/2017 11:02 AM, Jason Merrill wrote:
>>>
>>>
>>>>>> The TREE_USED bit on the type (i.e., on
>>>>>> TREE_TYPE(decl) where decl is the u in the test case above) is
>>>>>> set when the function template is instantiated, in
>>>>>> set_underlying_type called from tsubst_decl.
>>>>>
>>>>>
>>>>> Aha! That seems like the problem. Does removing that copy of
>>>>> TREE_USED from the decl to the type break anything?
>>>>
>>>>
>>>> As far as I can see it breaks just gcc.dg/unused-3.c which is:
>>>>
>>>> typedef short unused_type __attribute__ ((unused));
>>>>
>>>> void f (void)
>>>> {
>>>> unused_type y;
>>>> }
>>>
>>>
>>> Ah. So the problem here is that we're using TREE_USED on a TYPE_DECL
>>> for two things: to track whether the typedef has been used, and to
>>> determine whether to treat a variable of that type as already used.
>>> Normally this isn't too much of a problem because we copy the
>>> TREE_USED flag from decl to type before there could be any uses, but
>>> in a template I guess we mark the typedef within the template when it
>>> is used, and then when we instantiate the typedef we incorrectly pass
>>> that mark along to the type.
>>>
>>> Your patch deals with this by ignoring TREE_USED on the type, so it
>>> doesn't matter that it's wrong. Another approach would be to correct
>>> the setting of TREE_USED by setting it based on looking up the unused
>>> attribute, rather than copying it from the TYPE_DECL. So also going
>>> back to the attribute, but in set_underlying_type rather than
>>> poplevel.
>>
>> Thoughts?
>
> I'm not 100% sure I understand what changes you're suggesting
> but the attached patch does what I think you're after. Is that
> what you had in mind?
Looks good.
Jason