From patchwork Sat May 26 13:30:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 161485 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 A36A8B6FA1 for ; Sat, 26 May 2012 23:32:24 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1338643945; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Message-ID:Date:From:User-Agent:MIME-Version: To:CC:Subject:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=BpVrAtEtupE+wMKiFUoh8a2iUfM=; b=knx+3JKhCNi7Qcw NXJqOBpS1vRdmoAVFkeoG4HIbaG2xiLEcim/zs5aQ31Xqc9aNlKs5tsxuKO8UG26 gwtcI3PKLfCrCk9DLh9v4vFWNfvNSB/icxFGLj+svLwy1SNn50LsS49IdQ+oc27R g3VbY/9xC6jynoY27fBUjkAZ9YYk= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=qS+/NiEVs4hZ7oxdLHd6qF1NqbLEz0e83PEN1s4AFB/7IXNrkrnAOhFDWLPhSm h0pMnRh6Zr0lEUWEML/rCPNlXkQhqno3o1dL8ALNMAkS7ygOSSIk3xiV6aQRsfqf KLSAUq2Xk06P0zGr0h8bBXR9r6lno6xyEO/7ftJyOYOb4=; Received: (qmail 11081 invoked by alias); 26 May 2012 13:32:21 -0000 Received: (qmail 11068 invoked by uid 22791); 26 May 2012 13:32:19 -0000 X-SWARE-Spam-Status: No, hits=-6.6 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_NO, RCVD_IN_HOSTKARMA_W, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from rcsinet15.oracle.com (HELO rcsinet15.oracle.com) (148.87.113.117) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 26 May 2012 13:32:06 +0000 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by rcsinet15.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id q4QDW4CR008602 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 26 May 2012 13:32:05 GMT Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by ucsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id q4QDW3YF002903 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 26 May 2012 13:32:04 GMT Received: from abhmt106.oracle.com (abhmt106.oracle.com [141.146.116.58]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id q4QDW3mN011145; Sat, 26 May 2012 08:32:03 -0500 Received: from [192.168.1.4] (/79.51.25.190) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 26 May 2012 06:32:02 -0700 Message-ID: <4FC0DB08.4090704@oracle.com> Date: Sat, 26 May 2012 15:30:48 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120421 Thunderbird/12.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill , Ian Lance Taylor Subject: [C++ Patch] PR 25137 X-IsSubscribed: yes 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 Hi, I found the time to return to this issue, where -Wmissing-braces is often overeager to warn, thus annoying, for example, people using -Wall together with std::array: std::array s = { 1, 2, 3 }; I handle the issue following the letter of the suggestion given by Ian at the time: do not warn when the class type has only one field and that field is an aggregate (recursively, of course). Indeed, that seems to me quite conservative. I also make sure to change nothing wrt the designated initializers extension. Bootstrapped and tested x86_64-linux. Thanks, Paolo. //////////////////////// /cp 2012-05-26 Paolo Carlini PR c++/25137 * decl.c (reshape_init_r): Add bool parameter. (reshape_init_class): If the struct has only one field and that field is an aggregate, don't warn if there is only one set of braces in the initializer. (reshape_init_array_1, reshape_init_class, reshape_init): Adjust. /testsuite 2012-05-26 Paolo Carlini PR c++/25137 * g++.dg/warn/Wbraces3.C: New. Index: testsuite/g++.dg/warn/Wbraces3.C =================================================================== --- testsuite/g++.dg/warn/Wbraces3.C (revision 0) +++ testsuite/g++.dg/warn/Wbraces3.C (revision 0) @@ -0,0 +1,34 @@ +// PR c++/25137 +// { dg-options "-Wmissing-braces" } + +struct S { int s[3]; }; +S s1 = { 1, 1, 1 }; + +struct S1 { int s[3]; }; +struct S2 { struct S1 a; }; +S2 s21 = { 1, 1, 1 }; + +struct S3 { int s[3]; }; +struct S4 { struct S3 a; int b; }; +S4 s41 = { 1, 1, 1, 1 }; // { dg-warning "missing braces around initializer for 'S3'" } + +struct S5 { int s[3]; }; +struct S6 { struct S5 a; int b; }; +S6 s61 = { { 1, 1, 1 }, 1 }; + +struct S7 { int s[3]; }; +struct S8 { int a; struct S7 b; }; +S8 s81 = { 1, { 1, 1, 1 } }; + +struct S9 { int s[2]; }; +struct S10 { struct S9 a; struct S9 b; }; +S10 s101 = { { 1, 1 }, 1, 1 }; // { dg-warning "missing braces around initializer for 'S9'" } + +struct S11 { int s[2]; }; +struct S12 { struct S11 a; struct S11 b; }; +S12 s121 = { { 1, 1 }, { 1, 1 } }; + +struct S13 { int i; }; +struct S14 { struct S13 a; }; +struct S15 { struct S14 b; }; +S15 s151 = { 1 }; Index: cp/decl.c =================================================================== --- cp/decl.c (revision 187907) +++ cp/decl.c (working copy) @@ -4954,7 +4954,7 @@ typedef struct reshape_iterator_t constructor_elt *end; } reshape_iter; -static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t); +static tree reshape_init_r (tree, reshape_iter *, bool, bool, tsubst_flags_t); /* FIELD is a FIELD_DECL or NULL. In the former case, the value returned is the next FIELD_DECL (possibly FIELD itself) that can be @@ -5014,7 +5014,7 @@ reshape_init_array_1 (tree elt_type, tree max_inde check_array_designated_initializer (d->cur, index); elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false, - complain); + /*no_warn_missing_braces=*/false, complain); if (elt_init == error_mark_node) return error_mark_node; CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), @@ -5082,6 +5082,7 @@ reshape_init_class (tree type, reshape_iter *d, bo { tree field; tree new_init; + bool no_warn_missing_braces; gcc_assert (CLASS_TYPE_P (type)); @@ -5105,6 +5106,12 @@ reshape_init_class (tree type, reshape_iter *d, bo return new_init; } + /* If the struct has only one field and that field is an aggregate, + don't warn if there is only one set of braces in the initializer. */ + no_warn_missing_braces + = (next_initializable_field (DECL_CHAIN (field)) == NULL_TREE + && CP_AGGREGATE_TYPE_P (TREE_TYPE (field))); + /* Loop through the initializable fields, gathering initializers. */ while (d->cur != d->end) { @@ -5125,7 +5132,10 @@ reshape_init_class (tree type, reshape_iter *d, bo /* We already reshaped this. */ gcc_assert (d->cur->index == field); else - field = lookup_field_1 (type, d->cur->index, /*want_type=*/false); + { + field = lookup_field_1 (type, d->cur->index, /*want_type=*/false); + no_warn_missing_braces = false; + } if (!field || TREE_CODE (field) != FIELD_DECL) { @@ -5141,7 +5151,8 @@ reshape_init_class (tree type, reshape_iter *d, bo break; field_init = reshape_init_r (TREE_TYPE (field), d, - /*first_initializer_p=*/false, complain); + /*first_initializer_p=*/false, + no_warn_missing_braces, complain); if (field_init == error_mark_node) return error_mark_node; @@ -5183,11 +5194,12 @@ has_designator_problem (reshape_iter *d, tsubst_fl a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the iterator within the CONSTRUCTOR which points to the initializer to process. FIRST_INITIALIZER_P is true if this is the first initializer of the - outermost CONSTRUCTOR node. */ + outermost CONSTRUCTOR node. If NO_WARN_MISSING_BRACES is true don't warn + about missing braces around initializer. */ static tree reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p, - tsubst_flags_t complain) + bool no_warn_missing_braces, tsubst_flags_t complain) { tree init = d->cur->value; @@ -5335,8 +5347,9 @@ reshape_init_r (tree type, reshape_iter *d, bool f } } - warning (OPT_Wmissing_braces, "missing braces around initializer for %qT", - type); + if (!no_warn_missing_braces) + warning (OPT_Wmissing_braces, + "missing braces around initializer for %qT", type); } /* Dispatch to specialized routines. */ @@ -5385,7 +5398,8 @@ reshape_init (tree type, tree init, tsubst_flags_t d.cur = VEC_index (constructor_elt, v, 0); d.end = d.cur + VEC_length (constructor_elt, v); - new_init = reshape_init_r (type, &d, true, complain); + new_init = reshape_init_r (type, &d, true, /*no_warn_missing_braces=*/false, + complain); if (new_init == error_mark_node) return error_mark_node;