From patchwork Sun Jun 23 18:42:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1951303 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=tugraz.at header.i=@tugraz.at header.a=rsa-sha256 header.s=mailrelay header.b=tKRw+F/l; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W6g2j3qlTz20WR for ; Mon, 24 Jun 2024 04:43:08 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 808AA385F025 for ; Sun, 23 Jun 2024 18:43:06 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id D73FB385E82F for ; Sun, 23 Jun 2024 18:42:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D73FB385E82F Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D73FB385E82F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719168169; cv=none; b=xskk408fsVAHAdYelH6NeIYk0N/P/Edguzr/soC1eBtmdfRFJN3+SyAkNaS2UZ2kAuQ6zKhnttXQUTQX4fuO4ThJYBFZ95sjg4+coHETnS6/p5DKlent9DvG0nXyIc9bZ6ywNB5dLVlxONArJiE2Y8Tyfya5vXsfyvCR48xdj88= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719168169; c=relaxed/simple; bh=fChDd0a6WEUbJAkgXPFGY+FSsLZe7OpfUbuvGrsUwbA=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=AxDd2GQJttAZC91PMu5Ql0VwZyDD+v2zizEUcrCR2R4E5bgBig3lR0ZchG6dm7EwjNyC2LAJjn0s+PP6c8RiAgxrxfQyMoMvVNBtPmdUbKGkYZem4KfV4+JzqDXWGAHAhdOonhv7+OZOrFjAJaVPSE4/OMfpD1fo6wTAtN4wo14= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-169-53.tugraz.at (vra-169-53.tugraz.at [129.27.169.53]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4W6g2438F3z1LLyW; Sun, 23 Jun 2024 20:42:36 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4W6g2438F3z1LLyW DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1719168156; bh=1t1YLCk7Fp0WSYZqg5nci7Jca0N0Qk9dBzfkxTgmKnw=; h=Subject:From:To:Cc:Date:From; b=tKRw+F/lXg6pXw4PmeVC1oByDU1NEcV7t6HbSlI3paOXVidRqIYW0VGelXXnDecOT 853xLnf7aeGUFiE2CMeyJsbJ8wWpKP/1/56CfMAwY9X9bJGF9fYo8htjCv1OT7u8YI 4RNtGkeO5UGBsW6gtzA+ECwg9QvWLYUp/1VdDUtc= Message-ID: Subject: [C PATCH] C: Error message for incorrect use of static in array declarations From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Sun, 23 Jun 2024 20:42:36 +0200 User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This adds an explicit error message for [static] and [static*]  (the same as clang has) instead of the generic "error: expected expression before ']' token", which is not entirely accurate. For function definitions the subsequent error "[*] can not be used outside function prototype scope" is then suppressed. Bootstrapped and regression tested on x86_64. commit 1157d04764eeeb51fa1098727813dbc092e11dd2 Author: Martin Uecker Date: Sat Nov 4 14:39:19 2023 +0100 C: Error message for incorrect use of static in array declarations. Add an explicit error messages when c99's static is used without a size expression in an array declarator. gcc/: c/c-parser.cc (c_parser_direct_declarator_inner): Add error message. gcc/testsuite: gcc.dg/c99-arraydecl-4.c: New test. diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index e83e9c683f7..91b8d24ca78 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -4732,41 +4732,29 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present, false, false, false, false, cla_prefer_id); if (!quals_attrs->declspecs_seen_p) quals_attrs = NULL; - /* If "static" is present, there must be an array dimension. - Otherwise, there may be a dimension, "*", or no - dimension. */ - if (static_seen) + + star_seen = false; + if (c_parser_next_token_is (parser, CPP_MULT) + && c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE) { - star_seen = false; - dimen = c_parser_expr_no_commas (parser, NULL); + star_seen = true; + c_parser_consume_token (parser); } - else + else if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) + dimen = c_parser_expr_no_commas (parser, NULL); + + if (static_seen && star_seen) { - if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) - { - dimen.value = NULL_TREE; - star_seen = false; - } - else if (c_parser_next_token_is (parser, CPP_MULT)) - { - if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE) - { - dimen.value = NULL_TREE; - star_seen = true; - c_parser_consume_token (parser); - } - else - { - star_seen = false; - dimen = c_parser_expr_no_commas (parser, NULL); - } - } - else - { - star_seen = false; - dimen = c_parser_expr_no_commas (parser, NULL); - } + error_at (c_parser_peek_token (parser)->location, + "% may not be used with an unspecified " + "variable length array size"); + /* Prevent further errors. */ + star_seen = false; } + else if (static_seen && NULL_TREE == dimen.value) + error_at (c_parser_peek_token (parser)->location, + "% may not be used without an array size"); + if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) c_parser_consume_token (parser); else diff --git a/gcc/testsuite/gcc.dg/c99-arraydecl-4.c b/gcc/testsuite/gcc.dg/c99-arraydecl-4.c new file mode 100644 index 00000000000..bfc26196433 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-arraydecl-4.c @@ -0,0 +1,15 @@ +/* { dg-do "compile" } */ +/* { dg-options "-std=c99 -pedantic-errors" } */ + +void fo(char buf[static]); /* { dg-error "'static' may not be used without an array size" } */ +void fo(char buf[static]) { } /* { dg-error "'static' may not be used without an array size" } */ + +void fu(char buf[static *]); /* { dg-error "'static' may not be used with an unspecified variable length array size" } */ +void fu(char buf[static *]) { } /* { dg-error "'static' may not be used with an unspecified variable length array size" } */ + +void fe(int n, char buf[static n]); +void fe(int n, char buf[static *]) { } /* { dg-error "'static' may not be used with an unspecified variable length array size" } */ + +void fa(int *n, char buf[static *n]); +void fa(int *n, char buf[static *n]) { } +