From patchwork Fri Apr 7 19:58:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volker Reichelt X-Patchwork-Id: 748448 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3w09PZ5xy1z9s7F for ; Sat, 8 Apr 2017 05:58:42 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="FMq8tbtI"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:subject:to:message-id:mime-version:content-type; q=dns; s= default; b=BvXYZyuAlKnStvF/BCk+RWU9HlgUWegolLPhwWCXtZXjE2RynlrA1 k88PMhszDJovSdwc1qfGI3+R0cHVpzdexMzYG25V0ZcvPeoNwKgbgq9Mr/AI/E26 t0C8jR2UMIvO9zVu6w88a2/I1+lajkOGex/REdIFq1nkanCmiiyN7M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:subject:to:message-id:mime-version:content-type; s= default; bh=iBq3y9hAjxhKljAKBKX8zGEGjbo=; b=FMq8tbtI7gMCRc8f1EJe PvS6O8GuHsEKTSq6xhuXKCwo24eDVawIRLKX27C9VQ5cSuU74Rl/JcF6cThXW0Ue aOrvE1zxjo/mQDfY1kwuTtZKSpz1yNOz6zDJ1S6fpMHMdY/xvv7+funRzmCPtPiT gefellVbos3nit4zL2MJq0k= Received: (qmail 62412 invoked by alias); 7 Apr 2017 19:58:34 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 62401 invoked by uid 89); 7 Apr 2017 19:58:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.8 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPAM_BODY, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: cc-smtpout2.netcologne.de Received: from cc-smtpout2.netcologne.de (HELO cc-smtpout2.netcologne.de) (89.1.8.212) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 07 Apr 2017 19:58:31 +0000 Received: from cc-smtpin2.netcologne.de (cc-smtpin2.netcologne.de [89.1.8.202]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id A2EB31288C for ; Fri, 7 Apr 2017 21:58:28 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin2.netcologne.de (Postfix) with ESMTP id 95B1511DF2 for ; Fri, 7 Apr 2017 21:58:28 +0200 (CEST) Received: from [89.0.21.185] (helo=cc-smtpin2.netcologne.de) by localhost with ESMTP (eXpurgate 4.1.9) (envelope-from ) id 58e7ef64-022c-7f0000012729-7f0000019a84-1 for ; Fri, 07 Apr 2017 21:58:28 +0200 Received: from linux-w03z.fritz.box (xdsl-89-0-21-185.netcologne.de [89.0.21.185]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by cc-smtpin2.netcologne.de (Postfix) with ESMTPSA for ; Fri, 7 Apr 2017 21:58:27 +0200 (CEST) Date: Fri, 7 Apr 2017 21:58:27 +0200 (CEST) From: Volker Reichelt Subject: [C++ PATCH] New warning for extra semicolons after in-class function definitions To: gcc-patches@gcc.gnu.org Message-ID: MIME-Version: 1.0 Content-Disposition: INLINE 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 * 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 * 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"); + } goto out; } else 2017-04-07 Volker Reichelt * 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" } +};