From patchwork Tue Aug 30 14:38:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 664158 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 3sNr4M724Xz9sBg for ; Wed, 31 Aug 2016 00:09:39 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=gU8jzZfN; 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:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=nL0Of9s6oPIfaQNB 710ge3DjQhqlu57T+0LhdjJNxyo36GShp6H3R2j60T31I7fZhgiiXMVoQP2JH8/q DUVxV4cdMtjvH27wyF6ToCgpsTDH3JRckTAYM7t1RDXD/jjkiWOjV/DDQ5HsPYE/ n+1uyNSGiC0pOMDgQzIKS/HpfEI= 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:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=lNvCkV4PArt75QfStl9yKq SQRB8=; b=gU8jzZfN3Dp2DI2TIo0RBZ75NHcWqcVJOd5gWpGIzCGqrpYs0Q+wQ/ VxLHmtbE/fD5KbEU1TZKnA2MDOhhsUHjdBssdBn1KEmmNrHFEC/IgBsPFYFt+r7M +XGpQ28JmiNKLoInAKU+ZuqvS/1IY+XL74FoMUlPoNIzyES4CYfmQ= Received: (qmail 4436 invoked by alias); 30 Aug 2016 14:09:32 -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 4418 invoked by uid 89); 30 Aug 2016 14:09:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.4 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=1, 39, our X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 30 Aug 2016 14:09:30 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CF63AC0641F1 for ; Tue, 30 Aug 2016 14:09:28 +0000 (UTC) Received: from c64.redhat.com (vpn-230-199.phx2.redhat.com [10.3.230.199]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u7UE9SM4021264; Tue, 30 Aug 2016 10:09:28 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH] C: fixits for modernizing structure member designators Date: Tue, 30 Aug 2016 10:38:46 -0400 Message-Id: <1472567926-12024-1-git-send-email-dmalcolm@redhat.com> MIME-Version: 1.0 X-IsSubscribed: yes This patch adds fix-it hints to our warning for old-style structure member designators, showing how to modernize them to C99 form. For example: modernize-named-inits.c:19:5: warning: obsolete use of designated initializer with ‘:’ [-Wpedantic] foo: 1, ^ . = In conjunction with the not-yet-in-trunk -fdiagnostics-generate-patch, this can generate patches like this: --- modernize-named-inits.c +++ modernize-named-inits.c @@ -16,7 +16,7 @@ /* Old-style named initializers. */ struct foo old_style_f = { - foo: 1, - bar: 2, + .foo= 1, + .bar= 2, }; Successfully bootstrapped®rtested on x86_64-pc-linux-gnu. OK for trunk? gcc/c/ChangeLog: * c-parser.c (c_parser_initelt): Provide fix-it hints for modernizing old-style structure member designator to C99 style. gcc/testsuite/ChangeLog: * gcc.dg/modernize-named-inits.c: New test case. --- gcc/c/c-parser.c | 17 +++++++----- gcc/testsuite/gcc.dg/modernize-named-inits.c | 39 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/modernize-named-inits.c diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index c245e70..a6281fc 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -4459,13 +4459,18 @@ c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack) && c_parser_peek_2nd_token (parser)->type == CPP_COLON) { /* Old-style structure member designator. */ - set_init_label (c_parser_peek_token (parser)->location, - c_parser_peek_token (parser)->value, - c_parser_peek_token (parser)->location, - braced_init_obstack); + location_t name_loc = c_parser_peek_token (parser)->location; + tree fieldname = c_parser_peek_token (parser)->value; + location_t colon_loc = c_parser_peek_2nd_token (parser)->location; + set_init_label (name_loc, fieldname, name_loc, braced_init_obstack); /* Use the colon as the error location. */ - pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic, - "obsolete use of designated initializer with %<:%>"); + rich_location richloc (line_table, colon_loc); + /* Provide fix-it hints for converting to C99 style i.e. + from "NAME:" to ".NAME =". */ + richloc.add_fixit_insert (name_loc, "."); + richloc.add_fixit_replace (colon_loc, "="); + pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, + "obsolete use of designated initializer with %<:%>"); c_parser_consume_token (parser); c_parser_consume_token (parser); } diff --git a/gcc/testsuite/gcc.dg/modernize-named-inits.c b/gcc/testsuite/gcc.dg/modernize-named-inits.c new file mode 100644 index 0000000..4e16494 --- /dev/null +++ b/gcc/testsuite/gcc.dg/modernize-named-inits.c @@ -0,0 +1,39 @@ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-show-caret -std=c99 -pedantic" } */ + +struct foo +{ + int foo; + int bar; +}; + +union u +{ + int color; + int shape; +}; + +/* Old-style named initializers. */ + +struct foo old_style_f = { + foo: 1, /* { dg-warning "5: obsolete use of designated initializer with ':'" } */ +/* { dg-begin-multiline-output "" } + foo: 1, + ^ + . = + { dg-end-multiline-output "" } */ + + bar : 1, /* { dg-warning "9: obsolete use of designated initializer with ':'" } */ + /* { dg-begin-multiline-output "" } + bar : 1, + ^ + . = + { dg-end-multiline-output "" } */ +}; + +union u old_style_u = { color: 3 }; /* { dg-warning "30: obsolete use of designated initializer with ':'" } */ +/* { dg-begin-multiline-output "" } + union u old_style_u = { color: 3 }; + ^ + . = + { dg-end-multiline-output "" } */