From patchwork Thu Apr 21 17:20:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Monakov X-Patchwork-Id: 92432 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]) by ozlabs.org (Postfix) with SMTP id 708971007DA for ; Fri, 22 Apr 2011 03:20:41 +1000 (EST) Received: (qmail 3769 invoked by alias); 21 Apr 2011 17:20:39 -0000 Received: (qmail 3760 invoked by uid 22791); 21 Apr 2011 17:20:37 -0000 X-SWARE-Spam-Status: No, hits=-0.7 required=5.0 tests=AWL, BAYES_00, FSL_RU_URL, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from smtp.ispras.ru (HELO smtp.ispras.ru) (83.149.198.202) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 21 Apr 2011 17:20:17 +0000 Received: from ispserv.ispras.ru (ispserv.ispras.ru [83.149.198.72]) by smtp.ispras.ru (Postfix) with ESMTP id 7B0045D40DE for ; Thu, 21 Apr 2011 21:18:47 +0400 (MSD) Received: from monoid.intra.ispras.ru (winnie.ispras.ru [83.149.198.236]) by ispserv.ispras.ru (Postfix) with ESMTP id 5F5BC3FC48 for ; Thu, 21 Apr 2011 21:20:15 +0400 (MSD) Date: Thu, 21 Apr 2011 21:20:15 +0400 (MSD) From: Alexander Monakov To: gcc-patches@gcc.gnu.org Subject: [PATCH] PR c/36750: Suppress missing field initializer warning for '= {0}' Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 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 Hello, This patch suppresses the "missing field initializer" warning when a structure is initialized with ` = { 0 }' in C. Even though the PR author asks specifically to suppress (at least) only when a trailing comma is included, results from Google code search suggest that spelling without a comma is more common, so the patch does not distinguish these variants. Behavior of C++ front-end is unchanged. Bootstrapped and regtested on x86_64-linux, OK for trunk? 2011-04-21 Alexander Monakov PR c/36750 * c-typeck.c (pop_init_level): Do not warn about initializing with ` = {0}'. testsuite: * gcc.dg/missing-field-init-2.c: Update testcase. diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 15b7755..d8609d2 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -6934,15 +6934,23 @@ pop_init_level (int implicit, struct obstack * braced_init_obstack) && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_unfilled_fields) { + bool constructor_zeroinit = + (VEC_length (constructor_elt, constructor_elements) == 1 + && integer_zerop + (VEC_index (constructor_elt, constructor_elements, 0)->value)); + /* Do not warn for flexible array members or zero-length arrays. */ while (constructor_unfilled_fields && (!DECL_SIZE (constructor_unfilled_fields) || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); - /* Do not warn if this level of the initializer uses member - designators; it is likely to be deliberate. */ - if (constructor_unfilled_fields && !constructor_designated) + if (constructor_unfilled_fields + /* Do not warn if this level of the initializer uses member + designators; it is likely to be deliberate. */ + && !constructor_designated + /* Do not warn about initializing with ` = {0}'. */ + && !constructor_zeroinit) { push_member_name (constructor_unfilled_fields); warning_init (OPT_Wmissing_field_initializers, diff --git a/gcc/testsuite/gcc.dg/missing-field-init-2.c b/gcc/testsuite/gcc.dg/missing-field-init-2.c index 581eb30..c5a3f49 100644 --- a/gcc/testsuite/gcc.dg/missing-field-init-2.c +++ b/gcc/testsuite/gcc.dg/missing-field-init-2.c @@ -9,3 +9,6 @@ struct s s4[] = { 1, 2, 3, 4, 5 }; /* { dg-warning "(missing initializer)|(near struct s s5[] = { 1, 2, 3, 4, 5, 6 }; /* Designated initializers produce no warning. */ struct s s6 = { .a = 1 }; /* { dg-bogus "missing initializer" } */ +/* Allow zero-initializing with "= { 0 }". */ +struct s s7 = { 0 }; /* { dg-bogus "missing initializer" } */ +struct s s8 = { 1 }; /* { dg-warning "(missing initializer)|(near initialization)" } */