public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: Volker Reichelt <v.reichelt@netcologne.de>, gcc-patches@gcc.gnu.org
Subject: Re: [C++ PATCH] New warning for extra semicolons after in-class function definitions
Date: Fri, 07 Apr 2017 21:22:00 -0000	[thread overview]
Message-ID: <1491600152.9106.9.camel@redhat.com> (raw)
In-Reply-To: <tkrat.0593279bc816987c@netcologne.de>

On Fri, 2017-04-07 at 21:55 +0200, Volker Reichelt wrote:
> Hi,
> 
> with the following patch I suggest to add a diagnostic for extra
> semicolons after in-class member function definitions:
> 
>   struct A
>   {
>     A() {};
>     void foo() {};
>     friend void bar() {};
>   };
> 
> Although they are allowed in the C++ standard, people (including me)
> often like to get rid of them for stylistic/consistency reasons.
> In fact clang has a warning -Wextra-semi for this.
> 
> Also in GCC (almost exactly 10 years ago) there was a patch
> https://gcc.gnu.org/ml/gcc-cvs/2007-03/msg00841.html
> to issue a pedwarn (which had to be reverted as GCC would reject
> valid
> code because of the pedwarn).
> 
> Instead of using pewarn the patch below adds a new warning (named
> like
> clang's) to warn about these redundant semicolons.
> Btw, clang's warning message "extra ';' after member function
> definition"
> is slightly incorrect because it is also emitted for friend functions
> which are not member-functions. That's why I suggest a different
> wording:
> 
>   Wextra-semi.C:3:9: warning: extra ';' after in-class function
> definition [-Wextra-semi]
>      A() {};
>            ^
>   Wextra-semi.C:4:16: warning: extra ';' after in-class function
> definition [-Wextra-semi]
>      void foo() {};
>                   ^
>   Wextra-semi.C:5:23: warning: extra ';' after in-class function
> definition [-Wextra-semi]
>      friend void bar() {};
>                          ^
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu.
> 
> OK for stage 1, once GCC 7 has branched?
> Regards,
> Volker
> 
> 
> 2017-04-07  Volker Reichelt  <v.reichelt@netcologne.de>
> 
>         * c.opt (Wextra-semi): New C++ warning flag.
> 
> Index: gcc/c-family/c.opt
> ===================================================================
> --- gcc/c-family/c.opt  (revision 246752)
> +++ gcc/c-family/c.opt  (working copy)
> @@ -504,6 +504,10 @@
>  C ObjC C++ ObjC++ Warning
>  ; in common.opt
>  
> +Wextra-semi
> +C++ Var(warn_extra_semi) Warning
> +Warn about semicolon after in-class function definition.
> +
>  Wfloat-conversion
>  C ObjC C++ ObjC++ Var(warn_float_conversion) Warning LangEnabledBy(C
> ObjC C++ ObjC++,Wconversion)
>  Warn for implicit type conversions that cause loss of floating point
> precision.
> 
> 2017-04-07  Volker Reichelt  <v.reichelt@netcologne.de>
> 
>         * parser.c (cp_parser_member_declaration): Add warning for
>         extra semicolon after in-class function definition.
> 
> Index: gcc/cp/parser.c
> ===================================================================
> --- gcc/cp/parser.c     (revision 246752)
> +++ gcc/cp/parser.c     (working copy)
> @@ -23386,7 +23386,11 @@
>                   token = cp_lexer_peek_token (parser->lexer);
>                   /* If the next token is a semicolon, consume it. 
> */
>                   if (token->type == CPP_SEMICOLON)
> -                   cp_lexer_consume_token (parser->lexer);
> +                   {
> +                     cp_lexer_consume_token (parser->lexer);
> +                     warning (OPT_Wextra_semi, "extra %<;%> "
> +                              "after in-class function definition");
> +                   }

Thanks for posting this.

I'm not a C++ maintainer, but I like the idea (though the patch is
missing at least a doc/invoke.texi change).

A small improvement to this would be to emit a deletion fix-it hint
about the redundant token (so that IDEs have a change of fixing it
easily).

This could be done something like this:

    location_t semicolon_loc
       = cp_lexer_consume_token (parser->lexer)->location;
    gcc_rich_location richloc (semicolon_loc);
    richloc.add_fixit_remove ();
    warning_at_richloc (&richloc, OPT_Wextra_semi,
                        "extra %<;%> after in-class function
definition");


>                   goto out;
>                 }
>               else
> 
> 2017-04-07  Volker Reichelt  <v.reichelt@netcologne.de>
> 
>         * g++.dg/warn/Wextra-semi.C: New test.
> 
> Index: gcc/testsuite/g++.dg/warn/Wextra-semi.C
> ===================================================================
> --- gcc/testsuite/g++.dg/warn/Wextra-semi.C     2017-04-07
> +++ gcc/testsuite/g++.dg/warn/Wextra-semi.C     2017-04-07
> @@ -0,0 +1,8 @@
> +// { dg-options "-Wextra-semi" }
> +
> +struct A
> +{
> +  A() {};              // { dg-warning "after in-class function
> definition" }
> +  void foo() {};       // { dg-warning "after in-class function
> definition" }
> +  friend void bar() {};        // { dg-warning "after in-class
> function definition" }
> +};
> 

If you implement the fix-it idea, then you can add 
-fdiagnostics-show-caret to the dg-options, and use something like:

/* { dg-begin-multiline-output "" }
  A() {};
        ^
        -
   { dg-end-multiline-output "" } */

to express the expected output (copied and pasted from GCC's output). 
 You should omit the commented parts of the lines if you do (otherwise
DejaGnu gets very confused due to them containing dg- directives).

Hope this is constructive
Dave

  reply	other threads:[~2017-04-07 21:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-07 19:55 Volker Reichelt
2017-04-07 21:22 ` David Malcolm [this message]
2017-04-09 20:53   ` Volker Reichelt
2017-04-10 16:32     ` Mike Stump
2017-04-10 16:54       ` Jason Merrill
2017-04-07 19:58 Volker Reichelt

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=1491600152.9106.9.camel@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=v.reichelt@netcologne.de \
    /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).