public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Martin Sebor <msebor@gmail.com>
Cc: Gcc Patch List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] restore -Wunused-variable on a typedef'd variable in a function template (PR 79548)
Date: Tue, 21 Feb 2017 20:35:00 -0000	[thread overview]
Message-ID: <CADzB+2=D+cU8Lnq5eGqJzo=cXjL_YPHaj1M_RtnwxR97keYsug@mail.gmail.com> (raw)
In-Reply-To: <066019c5-c85e-8d26-102e-aeba34988416@gmail.com>

On Tue, Feb 21, 2017 at 11:00 AM, Martin Sebor <msebor@gmail.com> wrote:
> On 02/21/2017 11:08 AM, Jason Merrill wrote:
>>
>> On 02/17/2017 05:07 PM, Martin Sebor wrote:
>>>
>>>     * decl.c (poplevel): Avoid diagnosing entities declared with
>>>     attribute unused.
>>
>>
>> This change is OK.
>>
>>>     (initialize_local_var): Do not consider the type of a variable
>>>     when determining whether or not it's used.
>>
>>
>> This is not; the documentation for attribute unused says,
>>
>> When attached to a type (including a @code{union} or a @code{struct}),
>> this attribute means that variables of that type are meant to appear
>> possibly unused.  GCC does not produce a warning for any variables of
>> that type, even if the variable appears to do nothing.  This is often
>> the case with lock or thread classes, which are usually defined and then
>> not referenced, but contain constructors and destructors that have
>> nontrivial bookkeeping functions.
>>
>> So a TREE_USED type should imply TREE_USED on variables of that type.
>
> Yes, I'm familiar with the effect of the attribute on types but
> in my testing this change doesn't affect how it works (i.e., it
> passes a full bootstrap and regression tests and I haven't been
> able to construct a failing test case.)  It looks like that's
> because TREE_USED(decl) is already set here for a decl whose
> type is declared attribute used.
>
> While trying to come up with a test case to exercise the scenario
> you describe I see that current trunk doesn't handle it correctly
> but the patch just happens to fix that too.  For example:
>
> template <class T>
> void f ()
> {
>   T t;   // trunk warns for f<int> (good)
>
>   typedef T U;
>   U u;   // trunk doesn't warn for f<int> (bug 79548)
> }
>
> template void f<int>();
>
> struct __attribute__ ((unused)) S { };
>
> template void f<S>();   // no warnings here (good)
>
> void g ()
> {
>   S s;
>
>   typedef S T;
>   T t;   // trunk warns here (new bug), doesn't with the patch
> }
>
> In the test case above, TREE_USED (TREE_TYPE (decl)) is set for
> t in g() so trunk sets it on t too and warbs.  The patch doesn't
> and so it doesn't warn as expected.
>
> But it's entirely possible I'm missing a case.  Do you have
> a suggestion for a test that trunk handles correctly but that
> fails with the patch?

Ah, I see, your patch changes attribute unused handling for local
variables from tracking TREE_USED to lookup_attribute.  I'm not
opposed to this change, but I'd like to understand why the TREE_USED
handling wasn't working.

Jason

  reply	other threads:[~2017-02-21 20:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-18  2:29 Martin Sebor
2017-02-21 18:10 ` Jason Merrill
2017-02-21 19:09   ` Martin Sebor
2017-02-21 20:35     ` Jason Merrill [this message]
2017-02-22  7:48       ` Martin Sebor
2017-02-22 18:10         ` Jason Merrill
2017-02-22 23:52           ` Martin Sebor
2017-02-23  1:11             ` Jason Merrill
2017-02-23 21:06               ` Martin Sebor
2017-02-23 23:48                 ` Jason Merrill
2017-03-20 21:12                   ` Jason Merrill
2017-03-21 18:40                     ` Martin Sebor
2017-03-21 19:28                       ` Jason Merrill
2017-03-21 22:37                         ` Martin Sebor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADzB+2=D+cU8Lnq5eGqJzo=cXjL_YPHaj1M_RtnwxR97keYsug@mail.gmail.com' \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=msebor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).