public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>,
	 Joseph Myers <joseph@codesourcery.com>,
	Martin Sebor <msebor@redhat.com>,
	Jason Merrill <jason@redhat.com>
Subject: Re: C/C++ PATCH to implement -Wmultistatement-macros (PR c/80116)
Date: Thu, 08 Jun 2017 17:24:00 -0000	[thread overview]
Message-ID: <1496942649.7551.150.camel@redhat.com> (raw)
In-Reply-To: <20170608164936.GV3413@redhat.com>

On Thu, 2017-06-08 at 18:49 +0200, Marek Polacek wrote:
> This is the hopefully last incarnation of the patch.  The change from
> the
> last time[0] is simpy that I've added a new test and the warning has
> been
> renamed to -Wmultistatement-macros.
> 
> David - any another comments?

Thanks for working on this; looks useful.

The new name is more accurate, but is rather long; oh well.  As part of
-Wall, users won't typically have to type it, so that's OK.

[...]

> diff --git gcc/c-family/c-warn.c gcc/c-family/c-warn.c
> index 35321a6..d883330 100644
> --- gcc/c-family/c-warn.c
> +++ gcc/c-family/c-warn.c
[...]

> +  if (warning_at (body_loc, OPT_Wmultistatement_macros,
> +		  "macro expands to multiple statements"))
> +    inform (guard_loc, "some parts of macro expansion are not
> guarded by "
> +	    "this conditional");

Is the guard necessarily a "conditional"?  I take a "conditional" to
mean an "if"; the guard could be a "for" or a "while" (or an "else",
which still seems something of a stretch to me to call a
"conditional").

Suggestion: word "this conditional" as "this %qs clause" and either (a)
rework the code in c-indentation.c's guard_tinfo_to_string so that it's
shared between these two warnings (i.e. to go from a RID_ to a const
char *), or (b) just pass in a const char * identifying the guard
clause token.

> diff --git gcc/c-family/c.opt gcc/c-family/c.opt
> index 37bb236..9dbe211 100644
> --- gcc/c-family/c.opt
> +++ gcc/c-family/c.opt
> @@ -698,6 +698,10 @@ Wmissing-field-initializers
>  C ObjC C++ ObjC++ Var(warn_missing_field_initializers) Warning
> EnabledBy(Wextra)
>  Warn about missing fields in struct initializers.
>  
> +Wmultistatement-macros
> +C ObjC C++ ObjC++ Var(warn_multistatement_macros) Warning
> LangEnabledBy(C ObjC C++ ObjC++,Wall)
> +Warn about macros expanding to multiple statements in a body of a
> conditional such as if, else, while, or for.

Likewise; is "conditional" the right word here?  Also, whether of not
the statements are actually "in" the body of the guard is the issue
here.

How about:

"Warn about unsafe multiple statement macros that appear to be guarded
by a clause such as if, else, while, or for, in which only the first
statement is actually guarded after the macro is expanded."

or somesuch?


> diff --git gcc/doc/invoke.texi gcc/doc/invoke.texi
> index c116882..2fe16dd 100644
> --- gcc/doc/invoke.texi
> +++ gcc/doc/invoke.texi
> @@ -4496,6 +4497,29 @@ This warning is enabled by @option{-Wall}.
>  @opindex Wno-missing-include-dirs
>  Warn if a user-supplied include directory does not exist.
>  
> +@item -Wmultistatement-macros
> +@opindex Wmultistatement-macros
> +@opindex Wno-multistatement-macros
> +Warn about macros expanding to multiple statements in a body of a
> conditional,
> +such as @code{if}, @code{else}, @code{for}, or @code{while}.

(as above).

> +For example:
> +
> +@smallexample
> +#define DOIT x++; y++
> +if (c)
> +  DOIT;
> +@end smallexample
> +
> +will increment @code{y} unconditionally, not just when @code{c}
> holds.
> +The can usually be fixed by wrapping the macro in a do-while loop:
> +@smallexample
> +#define DOIT do @{ x++; y++; @} while (0)
> +if (c)
> +  DOIT;
> +@end smallexample
> +
> +This warning is enabled by @option{-Wall} in C and C++.
> +
>  @item -Wparentheses
>  @opindex Wparentheses
>  @opindex Wno-parentheses

Hope this is constructive
Dave

  reply	other threads:[~2017-06-08 17:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-08 16:49 Marek Polacek
2017-06-08 17:24 ` David Malcolm [this message]
2017-06-08 18:10   ` Martin Sebor
2017-06-09 22:03     ` Gerald Pfeifer
2017-06-13 13:46       ` Marek Polacek
2017-07-17  8:15       ` Gerald Pfeifer
2017-07-17  9:26         ` Marek Polacek
2017-06-13 10:05   ` Marek Polacek
2017-06-13 15:29     ` Joseph Myers
2017-06-19 10:01       ` Marek Polacek
2017-06-26  9:40         ` Marek Polacek
2017-06-26 13:13           ` David Malcolm
2017-06-10  0:47 ` Jason Merrill

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=1496942649.7551.150.camel@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=msebor@redhat.com \
    --cc=polacek@redhat.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).