From patchwork Mon Jun 2 04:58:53 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "S. Gilles" X-Patchwork-Id: 354761 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 60BE51400AB for ; Mon, 2 Jun 2014 14:59:13 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :date:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=Ul0vgW5ug4VfxHtZ3NvJ3RTKtbNzCDWu0eIXzqB0ums6Ge6RBy86V AAKFLPXo+okw+9egyxYOlqTCZdOG8yD7VxOFeFv7g+z32BgsvXskOeUt9uzMg6mm ddod66dw4xjitr7m5ih/9Us+mfDxtQC68i5fCiJ9xBD9Qt3fj79684= 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 :date:to:subject:message-id:mime-version:content-type; s= default; bh=FAOJtu2TENrDk8VcoOrIHdDGCtk=; b=lMG6arqr/RSRRnOtuLhL /jeXgsXK/i3/5VsdGVIjov5RprNC61xgVv2kWOKbuU6q7/96NA5KNWvE8RdHLDJg N0Mb+BYnRP2vYA1y+4aMEYi2ACEIMKZI1WdVWTMZ+HUQbs1Ffww3gych7vh44jBO OZNijZYNJ/dYXn0SZhmu9HA= Received: (qmail 23773 invoked by alias); 2 Jun 2014 04:59:06 -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 23753 invoked by uid 89); 2 Jun 2014 04:59:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-qa0-f51.google.com Received: from mail-qa0-f51.google.com (HELO mail-qa0-f51.google.com) (209.85.216.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 02 Jun 2014 04:59:02 +0000 Received: by mail-qa0-f51.google.com with SMTP id w8so2072994qac.10 for ; Sun, 01 Jun 2014 21:59:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:date:to:subject:message-id:mime-version :content-type:content-disposition:user-agent; bh=7WvnsrtqmQ3Pfxmj1MG2Q+BCDBX7GowR8O+mlS3PLRw=; b=UosshJ3ma6A1lsS8BfB3AFcr+JAybQQAF+PZDoC7WyLtnocN7HYrVYXp0NzJleZ47C n8xiQQH2zmMWODIuDyCeLo5wrpvgcvMQ5zeFsfnZyes/OjemDJO4gGfRcUsd6Ec5/oU5 JioNw9gYSc6PxVoAfi29BJ4ZPUJcmItFekA7GikL4f2hn3Tg9tHw8bwXIfv/9FR3Z8MZ oHARUavrcytOcMOxjL5GXK6s61fdndLnQ5Sbx0bAOckL37qEPYhJvE9SVtMDXru4XL55 RToL17tlAuQrIds4ato2gzZEdWAlnpqEgOl1sWcyWsyDk7A5cqcDRkeJASwqhTNDrqe2 IjGQ== X-Gm-Message-State: ALoCoQkKXPJBS80Y0XPolJT0zaLr4kLCVJfZ5texOZTcSnIlQ1zJWonWbdkH990DRKM0G+QrHPdc X-Received: by 10.224.165.148 with SMTP id i20mr44684122qay.41.1401685140073; Sun, 01 Jun 2014 21:59:00 -0700 (PDT) Received: from number16 (c-76-100-81-188.hsd1.md.comcast.net. [76.100.81.188]) by mx.google.com with ESMTPSA id 5sm7690486qgi.45.2014.06.01.21.58.59 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Jun 2014 21:58:59 -0700 (PDT) From: "S. Gilles" X-Google-Original-From: "S. Gilles" Date: Mon, 2 Jun 2014 00:58:53 -0400 To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix logic for detection of zero initializer (PR c/53119) Message-ID: <20140602045853.GA950@number16> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This patch addresses PR 53119 (-Wmissing-braces wrongly warns about universal zero initializer {0}). As a result, initializations in C of the form struct foo a = { 0 }; will not trigger warnings from -Wmissing-braces or -Wmissing-field-initializers. The detection for constructor_zeroinit has been changed. Instead of examining constructor_elements, which gives false negatives, each element is now examined as it is processed. The warning for missing braces is then moved to pop_init_level, where it can benefit from constructor_zeroinit. Bootstrapped and regtested on x86_64 linux, with added test case. If this is approved, could someone commit it for me, as I do not have access? S. Gilles 2014-06-01 S. Gilles gcc/ChangeLog: PR c/53119 c/ * c-typeck.c (push_init_level, process_init_element, pop_init_level): Correct check for zero initialization, move missing brace warning to respect zero initialization. * gcc.dg/pr53119.c: New testcase. Index: gcc/c/c-typeck.c =================================================================== --- gcc/c/c-typeck.c (revision 211081) +++ gcc/c/c-typeck.c (working copy) @@ -74,9 +74,9 @@ if expr.original_code == SIZEOF_EXPR. */ tree c_last_sizeof_arg; -/* Nonzero if we've already printed a "missing braces around initializer" - message within this initializer. */ -static int missing_braces_mentioned; +/* Nonzero if we might need to print a "missing braces around + initializer" message within this initializer. */ +static int found_missing_braces; static int require_constant_value; static int require_constant_elements; @@ -6858,6 +6858,9 @@ /* 1 if this constructor is erroneous so far. */ static int constructor_erroneous; +/* 1 if this constructor is the universal zero initializer { 0 } */ +static int constructor_zeroinit; + /* Structure for managing pending initializer elements, organized as an AVL tree. */ @@ -7019,7 +7022,7 @@ constructor_stack = 0; constructor_range_stack = 0; - missing_braces_mentioned = 0; + found_missing_braces = 0; spelling_base = 0; spelling_size = 0; @@ -7114,6 +7117,7 @@ constructor_type = type; constructor_incremental = 1; constructor_designated = 0; + constructor_zeroinit = 1; designator_depth = 0; designator_erroneous = 0; @@ -7317,12 +7321,8 @@ set_nonincremental_init (braced_init_obstack); } - if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) - { - missing_braces_mentioned = 1; - warning_init (input_location, OPT_Wmissing_braces, - "missing braces around initializer"); - } + if (implicit == 1) + found_missing_braces = 1; if (TREE_CODE (constructor_type) == RECORD_TYPE || TREE_CODE (constructor_type) == UNION_TYPE) @@ -7455,6 +7455,14 @@ } } + /* Warn when some structs are initialized with direct aggregation. */ + if (!implicit && found_missing_braces && warn_missing_braces + && !constructor_zeroinit) + { + warning_init (input_location, OPT_Wmissing_braces, + "missing braces around initializer"); + } + /* Warn when some struct elements are implicitly initialized to zero. */ if (warn_missing_field_initializers && constructor_type @@ -7461,10 +7469,6 @@ && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_unfilled_fields) { - bool constructor_zeroinit = - (vec_safe_length (constructor_elements) == 1 - && integer_zerop ((*constructor_elements)[0].value)); - /* Do not warn for flexible array members or zero-length arrays. */ while (constructor_unfilled_fields && (!DECL_SIZE (constructor_unfilled_fields) @@ -8594,6 +8598,11 @@ designator_depth = 0; designator_erroneous = 0; + if (!implicit && value.value && !integer_zerop (value.value)) + { + constructor_zeroinit = 0; + } + /* Handle superfluous braces around string cst as in char x[] = {"foo"}; */ if (string_flag Index: gcc/testsuite/gcc.dg/pr53119.c =================================================================== --- gcc/testsuite/gcc.dg/pr53119.c (revision 0) +++ gcc/testsuite/gcc.dg/pr53119.c (working copy) @@ -0,0 +1,29 @@ + +/* { dg-do compile } */ + +/* { dg-options "-Wmissing-braces -Wmissing-field-initializers" } */ + +struct a { + int x; + int y; +}; + +struct b { + struct a w; + struct a z; +}; + +int main (void) +{ + struct a az = { 0 }; + struct a anz = { 1 }; /* { dg-warning "missing initializer for" } */ + + struct b bz = { 0 }; + struct b bnz = { 1, 2, 3, 4 }; /* { dg-warning "missing braces" } */ + + struct b extra = { 0, 0 }; + + return 0; +} + +/* { dg-excess-errors "note" } */