From patchwork Tue Jul 1 22:39:53 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 366221 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 3E3C2140077 for ; Wed, 2 Jul 2014 08:40: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 :to:cc:subject:references:date:in-reply-to:message-id :mime-version:content-type; q=dns; s=default; b=heElhOfbg+87Ngp7 QaY3c8/Az3CI5/hedZhckvrWvc1gi27beZYl5J52R//+O5qoaPEjRyRGKUlUNDux +iLNYPH8SMYEPVCT0bX/rAhXhqRfHqnhiwPCfWAgF9nCE3Zpq7J55CnyE8EOmEYc 0+VLaBXSw6wBMpOudos7Pgt/pfo= 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:references:date:in-reply-to:message-id :mime-version:content-type; s=default; bh=G9ukkuUCIByRU+AFeKM7nt hSSqY=; b=j+g9Dw18mX6iU4d9ZmrhfLr35vaCki4EgbIgLIlgPAZHg53KoXq8H4 dJM3dyyFQ3nb40je4R5zzQ4gsoXgAoDQrqIoMlaJuUgY86sgtcN4xjAnzocsLtr9 BcGlUgEtmsRK9IIfpFQy+LmQ/HV3sUeS8IAAiAfHIU75i0e7bgVy8= Received: (qmail 20647 invoked by alias); 1 Jul 2014 22:40: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 20629 invoked by uid 89); 1 Jul 2014 22:40:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 01 Jul 2014 22:39:58 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s61Mdt4Q022737 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 1 Jul 2014 18:39:56 -0400 Received: from barimba (ovpn-113-95.phx2.redhat.com [10.3.113.95]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s61MdrBK015973 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Tue, 1 Jul 2014 18:39:54 -0400 From: Tom Tromey To: "Joseph S. Myers" Cc: Subject: Re: [PATCH] add __attribute__ ((designated_init)) References: <874n4ygzay.fsf@fleche.redhat.com> <87r47y1bta.fsf@fleche.redhat.com> Date: Tue, 01 Jul 2014 16:39:53 -0600 In-Reply-To: (Joseph S. Myers's message of "Fri, 24 Jan 2014 17:07:36 +0000") Message-ID: <87egy4r9dy.fsf@fleche.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 >>>>> "Joseph" == Joseph S Myers writes: [...] Joseph> Is there a reason someone using the attribute might not want the Joseph> warning? That is, why isn't -Wdesignated-init enabled by Joseph> default, given that it's only relevant to people using an Joseph> attribute whose sole function relates to the warning? I've finally updated this patch. I think I've addressed all the review comments. For reference the original thread is here: https://gcc.gnu.org/ml/gcc-patches/2014-01/msg01284.html This adds __attribute__((designated_init)) and -Wdesignated-init, a feature from sparse. This is also PR c/59855. Tom 2014-07-01 Tom Tromey PR c/59855 * doc/invoke.texi (Warning Options): Document -Wdesignated-init. * doc/extend.texi (Type Attributes): Document designated_init attribute. 2014-07-01 Tom Tromey PR c/59855 * c.opt (Wdesignated-init): New option. * c-common.c (c_common_attribute_table): Add "designated_init". (handle_designated_init): New function. 2014-07-01 Tom Tromey * c-typeck.c (struct constructor_stack) : New field. (really_start_incremental_init, push_init_level): Initialize designator_depth. (pop_init_level): Set global designator_depth. (process_init_element): Check for designated_init attribute. 2014-07-01 Tom Tromey PR c/59855 * gcc.dg/Wdesignated-init.c: New file. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index ee89fca..79d0f2f 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -380,6 +380,7 @@ static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *); static tree handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *); +static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *); static void check_function_nonnull (tree, int, tree *); static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); @@ -773,6 +774,8 @@ const struct attribute_spec c_common_attribute_table[] = handle_alloc_align_attribute, false }, { "assume_aligned", 1, 2, false, true, true, handle_assume_aligned_attribute, false }, + { "designated_init", 0, 0, false, true, false, + handle_designated_init_attribute, false }, { NULL, 0, 0, false, false, false, NULL, false } }; @@ -9275,6 +9278,21 @@ handle_returns_nonnull_attribute (tree *node, tree, tree, int, return NULL_TREE; } +/* Handle a "designated_init" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_designated_init_attribute (tree *node, tree name, tree, int, + bool *no_add_attrs) +{ + if (TREE_CODE (*node) != RECORD_TYPE) + { + error ("%qE attribute is only valid on % type", name); + *no_add_attrs = true; + } + return NULL_TREE; +} + /* Check for valid arguments being passed to a function with FNTYPE. There are NARGS arguments in the array ARGARRAY. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index c89040a..6517790 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -359,6 +359,10 @@ Wdeprecated C C++ ObjC ObjC++ Var(warn_deprecated) Init(1) Warning Warn if a deprecated compiler feature, class, method, or field is used +Wdesignated-init +C ObjC Var(warn_designated_init) Init(1) Warning +Warn about positional initialization of structs requiring designated initializers + Wdiscarded-qualifiers C ObjC Var(warn_discarded_qualifiers) Init(1) Warning Warn if type qualifiers on pointers are discarded diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 35bfd14..c0f3912 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -6946,6 +6946,7 @@ struct constructor_stack char outer; char incremental; char designated; + int designator_depth; }; static struct constructor_stack *constructor_stack; @@ -7117,6 +7118,7 @@ really_start_incremental_init (tree type) p->outer = 0; p->incremental = constructor_incremental; p->designated = constructor_designated; + p->designator_depth = designator_depth; p->next = 0; constructor_stack = p; @@ -7266,6 +7268,7 @@ push_init_level (location_t loc, int implicit, p->outer = 0; p->incremental = constructor_incremental; p->designated = constructor_designated; + p->designator_depth = designator_depth; p->next = constructor_stack; p->range_stack = 0; constructor_stack = p; @@ -7573,6 +7576,7 @@ pop_init_level (location_t loc, int implicit, constructor_erroneous = p->erroneous; constructor_incremental = p->incremental; constructor_designated = p->designated; + designator_depth = p->designator_depth; constructor_pending_elts = p->pending_elts; constructor_depth = p->depth; if (!p->implicit) @@ -8642,6 +8646,15 @@ process_init_element (location_t loc, struct c_expr value, bool implicit, if (constructor_type == 0) return; + if (!implicit && warn_designated_init && !was_designated + && TREE_CODE (constructor_type) == RECORD_TYPE + && lookup_attribute ("designated_init", + TYPE_ATTRIBUTES (constructor_type))) + warning_init (loc, + OPT_Wdesignated_init, + "positional initialization of field " + "in % declared with % attribute"); + /* If we've exhausted any levels that didn't have braces, pop them now. */ while (constructor_stack->implicit) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index cdfcce7..889cc0b 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -5886,6 +5886,17 @@ and caught in another, the class must have default visibility. Otherwise the two shared objects are unable to use the same typeinfo node and exception handling will break. +@item designated_init +This attribute may only be applied to structure types. It indicates +that any initialization of an object of this type must use designated +initializers rather than positional initializers. The intent of this +attribute is to allow the programmer to indicate that a structure's +layout may change, and that therefore relying on positional +initialization will result in future breakage. + +GCC emits warnings based on this attribute by default; use +@option{-Wno-designated-init} to suppress them. + @end table To specify multiple attributes, separate them by commas within the diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index b1f6f4b..7fedd37 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -243,8 +243,8 @@ Objective-C and Objective-C++ Dialects}. -Wc++-compat -Wc++11-compat -Wcast-align -Wcast-qual @gol -Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wdate-time -Wdelete-incomplete -Wno-cpp @gol --Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization @gol --Wno-discarded-qualifiers @gol +-Wno-deprecated -Wno-deprecated-declarations -Wno-designated-init @gol +-Wdisabled-optimization -Wno-discarded-qualifiers @gol -Wno-div-by-zero -Wdouble-promotion -Wempty-body -Wenum-compare @gol -Wno-endif-labels -Werror -Werror=* @gol -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 @gol @@ -5144,6 +5144,12 @@ a suffix. When used together with @option{-Wsystem-headers} it warns about such constants in system header files. This can be useful when preparing code to use with the @code{FLOAT_CONST_DECIMAL64} pragma from the decimal floating-point extension to C99. + +@item -Wno-designated-init @r{(C and Objective-C only)} +Suppress warnings when a positional initializer is used to initialize +a structure that has been marked with the @code{designated_init} +attribute. + @end table @node Debugging Options diff --git a/gcc/testsuite/gcc.dg/Wdesignated-init.c b/gcc/testsuite/gcc.dg/Wdesignated-init.c new file mode 100644 index 0000000..b9ca572 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wdesignated-init.c @@ -0,0 +1,107 @@ +/* PR c/59855 */ +/* { dg-do compile } */ +/* { dg-options "-std=gnu99" } */ + +typedef int vvv __attribute__((designated_init)); /* { dg-error "only valid" } */ + +union U { + int a; + double b; +} __attribute__((designated_init)); /* { dg-error "only valid" } */ + +enum E { ONE, TWO } __attribute__((designated_init)); /* { dg-error "only valid" } */ + +struct Pok { + int x; + int y; +}; + +struct Des { + int x; + int y; +} __attribute__ ((designated_init)); + +struct Des d1 = { 5, 5 }; /* { dg-warning "(positional|near initialization)" } */ +struct Des d2 = { .x = 5, .y = 5 }; +struct Des d3 = { .x = 5, 5 }; /* { dg-warning "(positional|near initialization)" } */ + +struct Des fd1 (void) +{ + return (struct Des) { 5, 5 }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Des fd2 (void) +{ + return (struct Des) { .x = 5, .y = 5 }; +} + +struct Des fd3 (void) +{ + return (struct Des) { .x = 5, 5 }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Wrap { + struct Pok p; + struct Des d; +} __attribute__ ((designated_init)); + +struct Wrap w1 = { { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +struct Wrap w2 = { .p = { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +struct Wrap w3 = { .p = { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +struct Wrap w4 = { { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +struct Wrap w5 = { .p = { 0, 1 }, .d = { .x = 2, .y = 3} }; + +struct Wrap w6 = { { 0, 1 }, .d.x = 2, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */ +struct Wrap w7 = { .p = { 0, 1 }, .d.x = 2, .d.y = 3 }; +struct Wrap w8 = { .p = { 0, 1 }, .d = { 2, 0 }, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */ +struct Wrap w9 = { .p = { 0, 1 }, .d = { .x = 2 }, .d.y = 3 }; + +struct Wrap fw1 (void) +{ + return (struct Wrap) { { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +}; + +struct Wrap fw2 (void) +{ + return (struct Wrap) { .p = { 0, 1 }, { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Wrap fw3 (void) +{ + return (struct Wrap) { .p = { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Wrap fw4 (void) +{ + return (struct Wrap) { { 0, 1 }, .d = { 2, 3} }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Wrap fw5 (void) +{ + return (struct Wrap) { .p = { 0, 1 }, .d = { .x = 2, .y = 3} }; +} + +struct Wrap fw6 (void) +{ + return (struct Wrap) { { 0, 1 }, .d.x = 2, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Wrap fw7 (void) +{ + return (struct Wrap) { .p = { 0, 1 }, .d.x = 2, .d.y = 3 }; +} + +struct Wrap fw8 (void) +{ + return (struct Wrap) { .p = { 0, 1 }, .d = { 2, 0 }, .d.y = 3 }; /* { dg-warning "(positional|near initialization)" } */ +} + +struct Wrap fw9 (void) +{ + return (struct Wrap) { .p = { 0, 1 }, .d = { .x = 2 }, .d.y = 3 }; +} + +struct Des da[] = { + { .x = 1, .y = 2 }, + { 5, 5 } /* { dg-warning "(positional|near initialization)" } */ +};