public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <patrick@parcs.ath.cx>
To: David Malcolm <dmalcolm@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH 3/3] Improve -Wmissing-indentation heuristics
Date: Thu, 18 Jun 2015 17:15:00 -0000	[thread overview]
Message-ID: <CA+C-WL9GdHY5KnFSjOe_zRuBsaLss3p=gOvYB1pa8ARHTd22HQ@mail.gmail.com> (raw)
In-Reply-To: <CA+C-WL_ixg=QVU+RLfTPeJDKuUZrPEXsNygRrUWaPbPkVnNGeA@mail.gmail.com>

On Thu, Jun 18, 2015 at 12:57 PM, Patrick Palka <patrick@parcs.ath.cx> wrote:
> On Thu, Jun 18, 2015 at 12:31 PM, David Malcolm <dmalcolm@redhat.com> wrote:
>> On Tue, 2015-06-09 at 13:31 -0400, Patrick Palka wrote:
>>> This patch improves the heuristics of the warning in a number of ways.
>>> The improvements are hopefully adequately documented in the code
>>> comments.
>>>
>>> The additions to the test case also highlight the improvements.
>>>
>>> I tested an earlier version of this patch on more than a dozen C code
>>> bases.  I only found one class of bogus warnings yet emitted, in the
>>> libpng and bdwgc projects.  These projects have a coding style which
>>> indents code inside #ifdefs as if this code was guarded by an if(), e.g.
>>>
>>>   if (foo != 0)
>>>     x = 10;
>>>   else       // GUARD
>>>     y = 100; // BODY
>>>
>>>   #ifdef BAR
>>>     blah ();  // NEXT
>>>   #endif
>>
>> We have detect_preprocessor_logic which suppresses warnings when there's
>> preprocessor logic between BODY_EXPLOC and NEXT_STMT_EXPLOC, for cases
>> like this:
>>
>>         if (flagA)
>>           foo ();
>>           ^ BODY_EXPLOC
>>       #if SOME_CONDITION_THAT_DOES_NOT_HOLD
>>         if (flagB)
>>       #endif
>>           bar ();
>>           ^ NEXT_STMT_EXPLOC
>>
>> This currently requires that it be multiline preprocessor logic: there
>> must be 3 or more lines between BODY_EXPLOC and NEXT_STMT_EXPLOC for
>> this rejection heuristic to fire.
>
> Oh I now see why it requires 3 or more lines: one line each for the
> #if, #endif and for the
>
>>
>> Perhaps we could tweak or reuse that heuristic, perhaps if there's an
>> entirely blank (or all whitespace) line separating them (given that this
>> warning is all about the "look" of the code).
>
> That makes sense.  What about just checking in
> detect_preprocessor_logic() if there is > 1 line (instead of >= 3
> lines) between the body and the next statement?  When that's the case,
> then whatever is in between the start of the body must either be more
> of the body (if it's a multi-line compound statement) or whitespace.
> In either case we should not warn if the next statement is aligned
> with the body.

Actually, the body cannot be a compound statement because (with this
patch) we have already bailed out in that case.  So if there is > 1
line between the body and the next statement then there must be
whitespace... unless there are just more #ifdefs, e.g.

    if (foo)
      bar ();
    #ifdef A
    #ifdef B
      baz ();
    #endif
    #endif

We would want to warn here even though there are 2 lines, not 1 line,
in between the body and the next statement.  So we still have to
directly check for a whitespace line,  I think.

> Yet we will still rightfully warn on the following
> code:

>
>     if (foo)  // GUARD
>       bar ();  // BODY
>     #ifdef BAZ
>       baz ();  // NEXT
>     #endif
>
> because there is just one line between the body and the next
> statement. The user can add a line between the body and the next
> statement to suppress the warning if it's bogus.

I meant to say, a (empty) line between the body and the #ifdef.

>
>>
>>> These bogus warnings are pre-existing, however (i.e. not caused by this
>>> patch).
>>
>> (nods)   Fixing the false positives from libpng/bdwgc sounds like a
>> separate issue and thus a separate patch then.
>>
>> [...snip...]
>>
>>

  reply	other threads:[~2015-06-18 17:10 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-09 17:31 [PATCH 1/3] Refactor entry point to -Wmisleading-indentation Patrick Palka
2015-06-09 17:31 ` [PATCH 2/3] Remove is_first_nonwhitespace_on_line(), instead improve get_visual_column() Patrick Palka
2015-06-22 17:37   ` Jeff Law
2015-06-09 18:01 ` [PATCH 3/3] Improve -Wmissing-indentation heuristics Patrick Palka
2015-06-18 16:46   ` David Malcolm
2015-06-18 16:58     ` Patrick Palka
2015-06-18 17:15       ` Patrick Palka [this message]
2015-06-22 16:51       ` Jeff Law
2015-06-22 17:38   ` Jeff Law
2015-06-18 15:45 ` [PATCH 1/3] Refactor entry point to -Wmisleading-indentation Patrick Palka
2015-06-18 16:48   ` David Malcolm
2015-06-18 17:09     ` Patrick Palka
2015-06-22 16:37     ` Jeff Law
2015-06-22 17:32 ` Jeff Law
2015-06-22 19:03   ` Patrick Palka
2015-06-23 19:14     ` Patrick Palka
2015-07-28  2:36       ` Patrick Palka
2015-07-31 16:56         ` Jeff Law

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='CA+C-WL9GdHY5KnFSjOe_zRuBsaLss3p=gOvYB1pa8ARHTd22HQ@mail.gmail.com' \
    --to=patrick@parcs.ath.cx \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).