From patchwork Thu Aug 25 17:33:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 769 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp344782wro; Thu, 25 Aug 2022 10:33:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR6/ACPHJFQaRqSCqyZwHgMumYDEVaG0AZoQJyOjHSfeO5607woSSMgWAxfDdgEs/RhZjBcu X-Received: by 2002:a17:906:b303:b0:73d:6c5b:65d2 with SMTP id n3-20020a170906b30300b0073d6c5b65d2mr3070543ejz.414.1661448829371; Thu, 25 Aug 2022 10:33:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661448829; cv=none; d=google.com; s=arc-20160816; b=XPTMJBrbiBbH6sKFEx7UvAPOtzzuOU+td0iQzgJVE2J9VDe8cuAN0niErX2sJZlEnL c/Pcc0ufa4CBMJVgnKeYYDG+ZbYtkkq8wshB/omiDuclrIht4D3mbNxg5/J397ado5mA CeQxc49sElNE2Icb4LJh/xMZUEA7hufTQghaEKPFZjUOTKLoIEVC9GPKnKRxG6RIhnTo JYvDhvJUuDeGBnT7E2iWK4DuI6QU8KVptf/LLjrmfi0r3DEIFVLZogLRxX5cgAkz2DSO wtIcPeeIQEJvVGVHKPxXNmjxMhLmGuM5oDXG8T3X8VkPCNifTCWeC0OGFW2C0MDBUDVI M6EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :message-id:subject:to:from:date:ironport-sdr:dmarc-filter :delivered-to; bh=VxsQdsiowD+nzQRna51jDNTelEDY6cXLSoesn2jihzk=; b=II4M4UVUQNM1SlZWk08173eYf3O36usdIIb/q0uxRKcEN4bXG/P2bQa3j5Yo7HzhCJ 6uBTIYgTAM13GBOQbckgHJQ8Zn8Qjtoo/vqe3Mn1F943lgzxozEEfmc10TkqqQcNDTG7 nHTxK+utW0bBykgYWdd5oHbBt9coXJsSbxjNB/Z1r38/P/0KPgLk/rW5d/Xug87q2oBD nVs9MVzEclxMKPVN9Bcp0c1dZcwC1EclnzSRMEubJZ12ZSuS/8qOepzkAYuz0CmUM0aE P1+4E5EY1NP1nO6FPbI1eVXne7xI+AJKoatPGM4m79N1cSlbScvooBb/7rY399WJ/hxs STyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i25-20020a508719000000b004479e742dc8si33031edb.344.2022.08.25.10.33.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 10:33:49 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 205A03853804 for ; Thu, 25 Aug 2022 17:33:38 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id A5D9D3858C2F for ; Thu, 25 Aug 2022 17:33:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A5D9D3858C2F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.93,263,1654588800"; d="scan'208";a="81817917" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 25 Aug 2022 09:33:07 -0800 IronPort-SDR: dgSNNqouwuP2AXhQTbYULr/T1NPaq8J2Gear45DnnHum1Gd4REoir5wTU3JEq7Kvmr+77BMxnX MsOwY88BkYTbIQCgbtelCq1BgV1tf2vdGK+KSC0tqCFaygc+n04/nTQa6u57ByBjujiSCJFThv cuBkBVgPHF/84i/QzzT2jcw/Kam9wwLaQ5ZfmPidsgixS3CINSGxhieb6fGHmtvCRIlFzFXGGo yQzYHBjvQcxJWglEFXEWUEFd6IbPsElh3y+H0Pk7VTDeH//PKG+O7VNh1xgUWJItM2FG8nGLk1 l80= Date: Thu, 25 Aug 2022 17:33:02 +0000 From: Joseph Myers X-X-Sender: jsm28@digraph.polyomino.org.uk To: Subject: c: Support C2x empty initializer braces Message-ID: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-3117.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742155367291079336?= X-GMAIL-MSGID: =?utf-8?q?1742155367291079336?= [Middle-end maintainers / global reviewers, note that there is a gimplify.cc change to be reviewed here.] ISO C2x standardizes empty initializer braces {}. Implement this feature accordingly. The basic case was already supported and so just needed diagnostic adjustments. However, the standard feature also includes two cases that were not previously supported: empty initializer braces for scalars, and empty initializer braces for VLAs. Thus, add support for those features as well, updating existing tests that expected them to be diagnosed. There was already some gimplifier support for converting variable-sized initializations with empty CONSTRUCTORs to memset. However, it didn't apply here; code earlier in gimplify_modify_expr ended up calling gimplify_init_constructor via gimplify_modify_expr_rhs, which ended up handling the CONSTRUCTOR in a way that generated an ICE later. Add a check for this case earlier in gimplify_modify_expr to avoid that issue. Bootstrapped with no regressions for x86_64-pc-linux-gnu. OK to commit (the gimplify.cc changes)? gcc/ * gimplify.cc (gimplify_modify_expr): Convert initialization from a variable-size CONSTRUCTOR to memset before call to gimplify_modify_expr_rhs. gcc/c/ * c-decl.cc (start_decl): Do not diagnose initialization of variable-sized objects here. * c-parser.cc (c_parser_braced_init): Add argument DECL. All callers changed. (c_parser_initializer): Diagnose initialization of variable-sized objects other than with braced initializer. (c_parser_braced_init): Use pedwarn_c11 for empty initializer braces and update diagnostic text. Diagnose initialization of variable-sized objects with nonempty braces. * c-typeck.cc (digest_init): Update diagnostic for initialization of variable-sized objects. (really_start_incremental_init, set_designator) (process_init_element): Update comments. (pop_init_level): Allow scalar empty initializers. gcc/testsuite/ * gcc.dg/c11-empty-init-1.c, gcc.dg/c11-empty-init-2.c, gcc.dg/c11-empty-init-3.c, gcc.dg/c2x-empty-init-1.c, gcc.dg/c2x-empty-init-2.c, gcc.dg/c2x-empty-init-3.c, gcc.dg/gnu2x-empty-init-1.c, gcc.dg/gnu2x-empty-init-2.c: New tests. * gcc.dg/torture/dfp-default-init-1.c: Also test empty initializers. * gcc.dg/init-bad-1.c, gcc.dg/noncompile/pr71583.c, gcc.dg/pr61096-1.c, gcc.dg/vla-init-2.c, gcc.dg/vla-init-3.c, gcc.target/i386/sse2-bfloat16-scalar-typecheck.c: Update expected diagnostics. * gcc.dg/ubsan/c-shift-1.c: Use nonempty initializers for VLA initializations expected to be diagnosed. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 9e590c66dae..1fe31e01132 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5180,29 +5180,15 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, initialized = false; else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) { - /* A complete type is ok if size is fixed. */ - - if (!poly_int_tree_p (TYPE_SIZE (TREE_TYPE (decl))) - || C_DECL_VARIABLE_SIZE (decl)) - { - error ("variable-sized object may not be initialized"); - initialized = false; - } + /* A complete type is ok if size is fixed. If the size is + variable, an empty initializer is OK and nonempty + initializers will be diagnosed in the parser. */ } else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) { error ("variable %qD has initializer but incomplete type", decl); initialized = false; } - else if (C_DECL_VARIABLE_SIZE (decl)) - { - /* Although C99 is unclear about whether incomplete arrays - of VLAs themselves count as VLAs, it does not make - sense to permit them to be initialized given that - ordinary VLAs may not be initialized. */ - error ("variable-sized object may not be initialized"); - initialized = false; - } } if (initialized) diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 759f200a7eb..1e8d9dca749 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -1523,7 +1523,7 @@ static tree c_parser_simple_asm_expr (c_parser *); static tree c_parser_gnu_attributes (c_parser *); static struct c_expr c_parser_initializer (c_parser *, tree); static struct c_expr c_parser_braced_init (c_parser *, tree, bool, - struct obstack *); + struct obstack *, tree); static void c_parser_initelt (c_parser *, struct obstack *); static void c_parser_initval (c_parser *, struct c_expr *, struct obstack *); @@ -5220,11 +5220,15 @@ static struct c_expr c_parser_initializer (c_parser *parser, tree decl) { if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) - return c_parser_braced_init (parser, NULL_TREE, false, NULL); + return c_parser_braced_init (parser, NULL_TREE, false, NULL, decl); else { struct c_expr ret; location_t loc = c_parser_peek_token (parser)->location; + if (decl != error_mark_node && C_DECL_VARIABLE_SIZE (decl)) + error_at (loc, + "variable-sized object may not be initialized except " + "with an empty initializer"); ret = c_parser_expr_no_commas (parser, NULL); /* This is handled mostly by gimplify.cc, but we have to deal with not warning about int x = x; as it is a GCC extension to turn off @@ -5251,11 +5255,12 @@ location_t last_init_list_comma; compound literal, and NULL_TREE for other initializers and for nested braced lists. NESTED_P is true for nested braced lists, false for the list of a compound literal or the list that is the - top-level initializer in a declaration. */ + top-level initializer in a declaration. DECL is the declaration for + the top-level initializer for a declaration, otherwise NULL_TREE. */ static struct c_expr c_parser_braced_init (c_parser *parser, tree type, bool nested_p, - struct obstack *outer_obstack) + struct obstack *outer_obstack, tree decl) { struct c_expr ret; struct obstack braced_init_obstack; @@ -5273,10 +5278,15 @@ c_parser_braced_init (c_parser *parser, tree type, bool nested_p, really_start_incremental_init (type); if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) { - pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces"); + pedwarn_c11 (brace_loc, OPT_Wpedantic, + "ISO C forbids empty initializer braces before C2X"); } else { + if (decl && decl != error_mark_node && C_DECL_VARIABLE_SIZE (decl)) + error_at (brace_loc, + "variable-sized object may not be initialized except " + "with an empty initializer"); /* Parse a non-empty initializer list, possibly with a trailing comma. */ while (true) @@ -5532,7 +5542,7 @@ c_parser_initval (c_parser *parser, struct c_expr *after, if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after) init = c_parser_braced_init (parser, NULL_TREE, true, - braced_init_obstack); + braced_init_obstack, NULL_TREE); else { init = c_parser_expr_no_commas (parser, after); @@ -10307,7 +10317,7 @@ c_parser_postfix_expression_after_paren_type (c_parser *parser, error_at (type_loc, "compound literal has variable size"); type = error_mark_node; } - init = c_parser_braced_init (parser, type, false, NULL); + init = c_parser_braced_init (parser, type, false, NULL, NULL_TREE); finish_init (); maybe_warn_string_init (type_loc, type, init); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index de8780a1502..0e37ab8695f 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -8291,7 +8291,9 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) { - error_init (init_loc, "variable-sized object may not be initialized"); + error_init (init_loc, + "variable-sized object may not be initialized except " + "with an empty initializer"); return error_mark_node; } @@ -8641,8 +8643,9 @@ really_start_incremental_init (tree type) constructor_max_index = integer_minus_one_node; /* constructor_max_index needs to be an INTEGER_CST. Attempts - to initialize VLAs will cause a proper error; avoid tree - checking errors as well by setting a safe value. */ + to initialize VLAs with a nonempty initializer will cause a + proper error; avoid tree checking errors as well by setting a + safe value. */ if (constructor_max_index && TREE_CODE (constructor_max_index) != INTEGER_CST) constructor_max_index = integer_minus_one_node; @@ -9024,12 +9027,14 @@ pop_init_level (location_t loc, int implicit, && !gnu_vector_type_p (constructor_type)) { /* A nonincremental scalar initializer--just return - the element, after verifying there is just one. */ + the element, after verifying there is just one. + Empty scalar initializers are supported in C2X. */ if (vec_safe_is_empty (constructor_elements)) { - if (!constructor_erroneous && constructor_type != error_mark_node) - error_init (loc, "empty scalar initializer"); - ret.value = error_mark_node; + if (constructor_erroneous || constructor_type == error_mark_node) + ret.value = error_mark_node; + else + ret.value = build_zero_cst (constructor_type); } else if (vec_safe_length (constructor_elements) != 1) { @@ -9114,7 +9119,7 @@ set_designator (location_t loc, bool array, return true; /* Likewise for an initializer for a variable-size type. Those are - diagnosed in digest_init. */ + diagnosed in the parser, except for empty initializer braces. */ if (COMPLETE_TYPE_P (constructor_type) && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST) return true; @@ -10275,7 +10280,7 @@ process_init_element (location_t loc, struct c_expr value, bool implicit, return; /* Ignore elements of an initializer for a variable-size type. - Those are diagnosed in digest_init. */ + Those are diagnosed in the parser (empty initializer braces are OK). */ if (COMPLETE_TYPE_P (constructor_type) && !poly_int_tree_p (TYPE_SIZE (constructor_type))) return; diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index f9e7060b2d8..69a0faf43c3 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -6031,6 +6031,21 @@ gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p, return GS_ALL_DONE; } + /* Convert initialization from an empty variable-size CONSTRUCTOR to + memset. */ + if (TREE_TYPE (*from_p) != error_mark_node + && TYPE_SIZE_UNIT (TREE_TYPE (*from_p)) + && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p))) + && TREE_CODE (*from_p) == CONSTRUCTOR + && vec_safe_is_empty (CONSTRUCTOR_ELTS (*from_p))) + { + maybe_with_size_expr (from_p); + gcc_assert (TREE_CODE (*from_p) == WITH_SIZE_EXPR); + return gimplify_modify_expr_to_memset (expr_p, + TREE_OPERAND (*from_p, 1), + want_value, pre_p); + } + /* Insert pointer conversions required by the middle-end that are not required by the frontend. This fixes middle-end type checking for for example gcc.dg/redecl-6.c. */ diff --git a/gcc/testsuite/gcc.dg/c11-empty-init-1.c b/gcc/testsuite/gcc.dg/c11-empty-init-1.c new file mode 100644 index 00000000000..120c282258e --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-empty-init-1.c @@ -0,0 +1,25 @@ +/* Test C11 does not support empty initializers. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +struct s { int a; }; +struct s s = {}; /* { dg-error "empty initializer" } */ +int x = {}; /* { dg-error "empty initializer" } */ +float y = {}; /* { dg-error "empty initializer" } */ +void *p = {}; /* { dg-error "empty initializer" } */ +union u { int a; long b; }; +union u z = {}; /* { dg-error "empty initializer" } */ +int aa[2] = {}; /* { dg-error "empty initializer" } */ + +void +f (int a) +{ + int vla[a] = {}; /* { dg-error "empty initializer" } */ + struct s as = {}; /* { dg-error "empty initializer" } */ + int ax = {}; /* { dg-error "empty initializer" } */ + float ay = {}; /* { dg-error "empty initializer" } */ + void *ap = {}; /* { dg-error "empty initializer" } */ + union u az = {}; /* { dg-error "empty initializer" } */ + int aaa[2] = {}; /* { dg-error "empty initializer" } */ + int t = (int) {}; /* { dg-error "empty initializer" } */ +} diff --git a/gcc/testsuite/gcc.dg/c11-empty-init-2.c b/gcc/testsuite/gcc.dg/c11-empty-init-2.c new file mode 100644 index 00000000000..3ec7c512a84 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-empty-init-2.c @@ -0,0 +1,25 @@ +/* Test C11 does not support empty initializers. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic" } */ + +struct s { int a; }; +struct s s = {}; /* { dg-warning "empty initializer" } */ +int x = {}; /* { dg-warning "empty initializer" } */ +float y = {}; /* { dg-warning "empty initializer" } */ +void *p = {}; /* { dg-warning "empty initializer" } */ +union u { int a; long b; }; +union u z = {}; /* { dg-warning "empty initializer" } */ +int aa[2] = {}; /* { dg-warning "empty initializer" } */ + +void +f (int a) +{ + int vla[a] = {}; /* { dg-warning "empty initializer" } */ + struct s as = {}; /* { dg-warning "empty initializer" } */ + int ax = {}; /* { dg-warning "empty initializer" } */ + float ay = {}; /* { dg-warning "empty initializer" } */ + void *ap = {}; /* { dg-warning "empty initializer" } */ + union u az = {}; /* { dg-warning "empty initializer" } */ + int aaa[2] = {}; /* { dg-warning "empty initializer" } */ + int t = (int) {}; /* { dg-warning "empty initializer" } */ +} diff --git a/gcc/testsuite/gcc.dg/c11-empty-init-3.c b/gcc/testsuite/gcc.dg/c11-empty-init-3.c new file mode 100644 index 00000000000..fd43fa7890b --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-empty-init-3.c @@ -0,0 +1,25 @@ +/* Test C11 does not support empty initializers. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -Wc11-c2x-compat" } */ + +struct s { int a; }; +struct s s = {}; /* { dg-warning "empty initializer" } */ +int x = {}; /* { dg-warning "empty initializer" } */ +float y = {}; /* { dg-warning "empty initializer" } */ +void *p = {}; /* { dg-warning "empty initializer" } */ +union u { int a; long b; }; +union u z = {}; /* { dg-warning "empty initializer" } */ +int aa[2] = {}; /* { dg-warning "empty initializer" } */ + +void +f (int a) +{ + int vla[a] = {}; /* { dg-warning "empty initializer" } */ + struct s as = {}; /* { dg-warning "empty initializer" } */ + int ax = {}; /* { dg-warning "empty initializer" } */ + float ay = {}; /* { dg-warning "empty initializer" } */ + void *ap = {}; /* { dg-warning "empty initializer" } */ + union u az = {}; /* { dg-warning "empty initializer" } */ + int aaa[2] = {}; /* { dg-warning "empty initializer" } */ + int t = (int) {}; /* { dg-warning "empty initializer" } */ +} diff --git a/gcc/testsuite/gcc.dg/c2x-empty-init-1.c b/gcc/testsuite/gcc.dg/c2x-empty-init-1.c new file mode 100644 index 00000000000..1487a2b23ab --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-empty-init-1.c @@ -0,0 +1,80 @@ +/* Test C2X support for empty initializers: valid use cases. */ +/* { dg-do run } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +extern void exit (int); +extern void abort (void); + +struct s { int a; }; +struct s s = {}; +int x = {}; +float y = {}; +void *p = {}; +union u { int a; long b; }; +union u z = {}; +int aa[2] = {}; + +void +f (int a) +{ + volatile int vla[a] = {}; + struct s as = {}; + int ax = {}; + float ay = {}; + void *ap = {}; + union u az = {}; + int aaa[2] = {}; + for (int i = 0; i < a; i++) + if (vla[i] != 0) + abort (); + if (as.a != 0) + abort (); + if (ax != 0) + abort (); + if (ay != 0) + abort (); + if (ap != 0) + abort (); + if (az.a != 0) + abort (); + if (aaa[0] != 0) + abort (); + if (aaa[1] != 0) + abort (); + if ((int) {} != 0) + abort (); + if ((float) {} != 0) + abort (); + if ((struct s) {}.a != 0) + abort (); + if ((union u) {}.a != 0) + abort (); + if ((int [5]) {}[2] != 0) + abort (); + /* Overwrite contents of vla before second call to make it more likely stack + contents are nonzero if proper initialization did not occur. */ + for (int i = 0; i < a; i++) + vla[i] = -1; +} + +int +main (void) +{ + f (100); + f (100); + if (s.a != 0) + abort (); + if (x != 0) + abort (); + if (y != 0) + abort (); + if (p != 0) + abort (); + if (z.a != 0) + abort (); + if (aa[0] != 0) + abort (); + if (aa[1] != 0) + abort (); + exit (0); +} diff --git a/gcc/testsuite/gcc.dg/c2x-empty-init-2.c b/gcc/testsuite/gcc.dg/c2x-empty-init-2.c new file mode 100644 index 00000000000..0dc81ce5b8e --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-empty-init-2.c @@ -0,0 +1,18 @@ +/* Test C2X support for empty initializers: invalid use cases. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +/* Empty initialization is invalid for arrays of unknown size. This is + diagnosed via the diagnostic for zero-size arrays. */ +int x[] = {}; /* { dg-error "zero or negative size array" } */ + +void +f (int a) +{ + int x1[] = {}; /* { dg-error "zero or negative size array" } */ + int x2[][a] = {}; /* { dg-error "zero or negative size array" } */ + /* Nonempty VLA initializers are still invalid. */ + int x3[a] = { 0 }; /* { dg-error "variable-sized object may not be initialized except with an empty initializer" } */ + /* Variable-size compound literals are still invalid. */ + (void) (int [a]) {}; /* { dg-error "compound literal has variable size" } */ +} diff --git a/gcc/testsuite/gcc.dg/c2x-empty-init-3.c b/gcc/testsuite/gcc.dg/c2x-empty-init-3.c new file mode 100644 index 00000000000..472f8169c41 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-empty-init-3.c @@ -0,0 +1,25 @@ +/* Test empty initializers diagnosed in C2X mode with -Wc11-c2x-compat. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -Wc11-c2x-compat" } */ + +struct s { int a; }; +struct s s = {}; /* { dg-warning "empty initializer" } */ +int x = {}; /* { dg-warning "empty initializer" } */ +float y = {}; /* { dg-warning "empty initializer" } */ +void *p = {}; /* { dg-warning "empty initializer" } */ +union u { int a; long b; }; +union u z = {}; /* { dg-warning "empty initializer" } */ +int aa[2] = {}; /* { dg-warning "empty initializer" } */ + +void +f (int a) +{ + int vla[a] = {}; /* { dg-warning "empty initializer" } */ + struct s as = {}; /* { dg-warning "empty initializer" } */ + int ax = {}; /* { dg-warning "empty initializer" } */ + float ay = {}; /* { dg-warning "empty initializer" } */ + void *ap = {}; /* { dg-warning "empty initializer" } */ + union u az = {}; /* { dg-warning "empty initializer" } */ + int aaa[2] = {}; /* { dg-warning "empty initializer" } */ + int t = (int) {}; /* { dg-warning "empty initializer" } */ +} diff --git a/gcc/testsuite/gcc.dg/gnu2x-empty-init-1.c b/gcc/testsuite/gcc.dg/gnu2x-empty-init-1.c new file mode 100644 index 00000000000..e7dc9dfde23 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gnu2x-empty-init-1.c @@ -0,0 +1,29 @@ +/* Test C2X support for empty initializers: valid use cases with GNU + extensions. */ +/* { dg-do run } */ +/* { dg-options "-std=gnu2x" } */ + +extern void exit (int); +extern void abort (void); + +void +f (int a) +{ + struct s { volatile int x[a]; }; + struct s b = {}; + for (int i = 0; i < a; i++) + if (b.x[i] != 0) + abort (); + /* Overwrite contents of b.x before second call to make it more likely stack + contents are nonzero if proper initialization did not occur. */ + for (int i = 0; i < a; i++) + b.x[i] = -1; +} + +int +main (void) +{ + f (100); + f (100); + exit (0); +} diff --git a/gcc/testsuite/gcc.dg/gnu2x-empty-init-2.c b/gcc/testsuite/gcc.dg/gnu2x-empty-init-2.c new file mode 100644 index 00000000000..69ee4e36b11 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gnu2x-empty-init-2.c @@ -0,0 +1,16 @@ +/* Test C2X support for empty initializers: invalid use cases with GNU + extensions. */ +/* { dg-do compile } */ +/* { dg-options "-std=gnu2x" } */ + +void +f (int a) +{ + /* Make sure a non-braced initializer for a VLA-in-struct is still not + allowed. */ + struct s { int x[a]; }; + struct s b; + for (int i = 0; i < a; i++) + b.x[i] = 0; + struct s c = b; /* { dg-error "variable-sized object may not be initialized except with an empty initializer" } */ +} diff --git a/gcc/testsuite/gcc.dg/init-bad-1.c b/gcc/testsuite/gcc.dg/init-bad-1.c index 61734045f2b..0da10c31539 100644 --- a/gcc/testsuite/gcc.dg/init-bad-1.c +++ b/gcc/testsuite/gcc.dg/init-bad-1.c @@ -21,8 +21,7 @@ char t1[1] = { "xy" }; /* { dg-warning "initializer-string for array of 'char' i char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char' array initializer" } */ /* { dg-message "near init" "near" { target *-*-* } .-1 } */ -int i = { }; /* { dg-error "empty scalar initializer" } */ -/* { dg-message "near init" "near" { target *-*-* } .-1 } */ +int i = { }; int j = { 1 }; diff --git a/gcc/testsuite/gcc.dg/noncompile/pr71583.c b/gcc/testsuite/gcc.dg/noncompile/pr71583.c index 5045b88b6af..fe6e556adfc 100644 --- a/gcc/testsuite/gcc.dg/noncompile/pr71583.c +++ b/gcc/testsuite/gcc.dg/noncompile/pr71583.c @@ -5,7 +5,7 @@ void f (int i) { (int (*)[++i]) { int }; /* { dg-error "expected" } */ - (int (*)[++i]) { }; /* { dg-error "empty" } */ + (int (*)[++i]) { }; (int (*)[++i]) { , }; /* { dg-error "expected" } */ (int (*)[++i]) { f () }; /* { dg-error "too few" } */ } diff --git a/gcc/testsuite/gcc.dg/pr61096-1.c b/gcc/testsuite/gcc.dg/pr61096-1.c index e707904c087..f41789c5f52 100644 --- a/gcc/testsuite/gcc.dg/pr61096-1.c +++ b/gcc/testsuite/gcc.dg/pr61096-1.c @@ -36,7 +36,7 @@ struct S s = { { 1 }, { 3 } }; /* { dg-error "23:extra brace group at end of ini struct g g1 = { {0, { 1 } } }; /* { dg-error "21:initialization of flexible array member in a nested context" } */ struct g g2 = { .f[0] = 1 }; /* { dg-error "20:array index in non-array initializer" } */ -__extension__ int a8 = { }; /* { dg-error "24:empty scalar initializer" } */ +__extension__ int a8 = { }; int a9[10] = {[1.2] = 2 }; /* { dg-error "16:array index in initializer not of integer type" } */ int a10[10] = {[e] = 2 }; /* { dg-error "17:nonconstant array index in initializer" } */ __extension__ int a11[10] = {[1 ... e] = 1 }; /* { dg-error "31:nonconstant array index in initializer" } */ diff --git a/gcc/testsuite/gcc.dg/torture/dfp-default-init-1.c b/gcc/testsuite/gcc.dg/torture/dfp-default-init-1.c index f893ddb52b9..7bb903bc83b 100644 --- a/gcc/testsuite/gcc.dg/torture/dfp-default-init-1.c +++ b/gcc/testsuite/gcc.dg/torture/dfp-default-init-1.c @@ -19,6 +19,7 @@ int memcmp (const void *, const void *, __SIZE_TYPE__); TYPE zero_int = 0; TYPE zero_fp = ZEROFP; TYPE default_init; +TYPE empty_init = {}; TYPE zero_bytes; TYPE x; @@ -64,6 +65,7 @@ main (void) abort (); CHECK_ZERO_BYTES (&zero_fp); CHECK_ZERO_BYTES (&default_init); + CHECK_ZERO_BYTES (&empty_init); CHECK_ZERO_BYTES (&s_default_init.a); CHECK_ZERO_BYTES (&s_default_init.b); CHECK_ZERO_BYTES (&s_empty_init.a); diff --git a/gcc/testsuite/gcc.dg/ubsan/c-shift-1.c b/gcc/testsuite/gcc.dg/ubsan/c-shift-1.c index 9d561016f95..f88ee2de379 100644 --- a/gcc/testsuite/gcc.dg/ubsan/c-shift-1.c +++ b/gcc/testsuite/gcc.dg/ubsan/c-shift-1.c @@ -7,12 +7,12 @@ int main (void) { /* None of the following should pass. */ - int A[1 >> -1] = {}; /* { dg-error "variable-sized object may not be initialized" } */ - int B[-1 >> -1] = {}; /* { dg-error "variable-sized object may not be initialized" } */ - int D[1 << -1] = {}; /* { dg-error "variable-sized object may not be initialized" } */ - int E[-1 << -1] = {}; /* { dg-error "variable-sized object may not be initialized" } */ - int F[-1 >> 200] = {}; /* { dg-error "variable-sized object may not be initialized" } */ - int G[1 << 200] = {}; /* { dg-error "variable-sized object may not be initialized" } */ + int A[1 >> -1] = { 0 }; /* { dg-error "variable-sized object may not be initialized" } */ + int B[-1 >> -1] = { 0 }; /* { dg-error "variable-sized object may not be initialized" } */ + int D[1 << -1] = { 0 }; /* { dg-error "variable-sized object may not be initialized" } */ + int E[-1 << -1] = { 0 }; /* { dg-error "variable-sized object may not be initialized" } */ + int F[-1 >> 200] = { 0 }; /* { dg-error "variable-sized object may not be initialized" } */ + int G[1 << 200] = { 0 }; /* { dg-error "variable-sized object may not be initialized" } */ return 0; } diff --git a/gcc/testsuite/gcc.dg/vla-init-2.c b/gcc/testsuite/gcc.dg/vla-init-2.c index 19fbffc2640..f23630a3657 100644 --- a/gcc/testsuite/gcc.dg/vla-init-2.c +++ b/gcc/testsuite/gcc.dg/vla-init-2.c @@ -7,4 +7,3 @@ const int i = 1; void foo() { char a[][i] = {""}; } /* { dg-error "variable-sized object may not be initialized" } */ -/* { dg-error "array size missing in 'a'" "extra error" { target *-*-* } .-1 } */ diff --git a/gcc/testsuite/gcc.dg/vla-init-3.c b/gcc/testsuite/gcc.dg/vla-init-3.c index 55e1de69ca7..a854f1268e0 100644 --- a/gcc/testsuite/gcc.dg/vla-init-3.c +++ b/gcc/testsuite/gcc.dg/vla-init-3.c @@ -6,4 +6,3 @@ /* { dg-options "" } */ void foo(int i) { char a[][i] = {""}; } /* { dg-error "variable-sized object may not be initialized" } */ -/* { dg-error "array size missing in 'a'" "extra error" { target *-*-* } .-1 } */ diff --git a/gcc/testsuite/gcc.target/i386/sse2-bfloat16-scalar-typecheck.c b/gcc/testsuite/gcc.target/i386/sse2-bfloat16-scalar-typecheck.c index f76d5547758..52b53b9ae85 100644 --- a/gcc/testsuite/gcc.target/i386/sse2-bfloat16-scalar-typecheck.c +++ b/gcc/testsuite/gcc.target/i386/sse2-bfloat16-scalar-typecheck.c @@ -36,7 +36,7 @@ __bf16 footest (__bf16 scalar0) short initi_1_4 = glob_bfloat; /* { dg-error {invalid conversion from type '__bf16'} } */ double initi_1_5 = glob_bfloat; /* { dg-error {invalid conversion from type '__bf16'} } */ - __bf16 scalar2_1 = {}; /* { dg-error {empty scalar initializer} } */ + __bf16 scalar2_1 = {}; __bf16 scalar2_2 = { glob_bfloat }; __bf16 scalar2_3 = { 0 }; /* { dg-error {invalid conversion to type '__bf16'} } */ __bf16 scalar2_4 = { 0.1 }; /* { dg-error {invalid conversion to type '__bf16'} } */ @@ -88,7 +88,7 @@ __bf16 footest (__bf16 scalar0) /* Compound literals. */ - (__bf16) {}; /* { dg-error {empty scalar initializer} } */ + (__bf16) {}; (__bf16) { glob_bfloat }; (__bf16) { 0 }; /* { dg-error {invalid conversion to type '__bf16'} } */ (__bf16) { 0.1 }; /* { dg-error {invalid conversion to type '__bf16'} } */