From patchwork Tue Aug 6 12:22:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1969460 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=XYPZW4uv; 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 4WdXYL1T6Bz1ydt for ; Tue, 6 Aug 2024 22:24:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 74D8D3858282 for ; Tue, 6 Aug 2024 12:24:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by sourceware.org (Postfix) with ESMTPS id 08B31385C6CE for ; Tue, 6 Aug 2024 12:22:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 08B31385C6CE Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 08B31385C6CE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:4641:c500::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722946982; cv=none; b=Zr2Xcg82O0lgQ4Dj2fBgo3PpqaF5Djht3EmOYj99k6cAo+QrZSeK08+a/4jjlZo4DJ3MnqftenfHp7KB+M1MX77ICFietVeuM9Y+XY2jr05xsDo5BfkG+Z6proOXOIMdFyWM47LyfZ/dv61mnOrvkWbsL6Frral1IuFwrta1G7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722946982; c=relaxed/simple; bh=vbNePeiCwLLXnRzADPeS5uUABDlC3WhnClFvTsSfQlE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=bZsUMAQc1pnyF9Mc6VHjkYlsIpVSpCySsJXczd6/HH+bqKqS+QSrrpxSqjfrA1vG4sySNaTqz8RPR80mVMtEI9taq4JfoHBCBpPuEheWp9RQJL73tQji9j0fFmwchYt8xiQe/0ANUHB/pwtAkHRMseDokDf7FJpp4oUM4l2HeBk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 4948A60F85; Tue, 6 Aug 2024 12:22:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01599C4AF0C; Tue, 6 Aug 2024 12:22:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722946978; bh=vbNePeiCwLLXnRzADPeS5uUABDlC3WhnClFvTsSfQlE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=XYPZW4uvGEYWmHCXCEt9mEOhSSCH9WbSPXZNfjRaaLMRf9VADQiFP6IzhzguHW0E9 sDll6IHUFNkodkWDxyes3e0TmyVL7mYgV4NRcoYyzISDbO7KzZEOoOELLR7sHQVlKR c2RorQOSDByu0TDtF1zXuC6BOUqX7+OrAkkHMGbhw8TRugNi3a2Kb9z2zW9qWyQlWP rrJ4JEQs5INRb5U4CFPItCWkOAQnsmTY/Eh/6jfAmbYPDhvbGF/gh4XnOvSGiegheS GNcY4Biz8Y/2eK79xB8TsXTpUdeBzBnwjSnrko9QiULV0OtJ1GcVrIYLnYnAu4Zt60 eR4uTl+z5cGlA== Date: Tue, 6 Aug 2024 14:22:54 +0200 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Alejandro Colomar , Xavier Del Campo Romero , Martin Uecker , Gabriel Ravier , Joseph Myers , Jakub Jelinek , Kees Cook , Qing Zhao , Jens Gustedt Subject: [RFC v4 4/4] testsuite: Add tests for __lengthof__ Message-ID: <20240806122218.3827577-5-alx@kernel.org> X-Mailer: git-send-email 2.45.2 References: <20240728141547.302478-1-alx@kernel.org> <20240806122218.3827577-1-alx@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240806122218.3827577-1-alx@kernel.org> X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 I've compiled those files manually, and they behave as expected. But within the test-suite, they don't seem to work: FAIL: gcc.dg/lengthof-compile.c (test for excess errors) FAIL: gcc.dg/lengthof.c (test for excess errors) Signed-off-by: Alejandro Colomar --- gcc/testsuite/gcc.dg/lengthof-compile.c | 48 +++++++++ gcc/testsuite/gcc.dg/lengthof.c | 126 ++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/lengthof-compile.c create mode 100644 gcc/testsuite/gcc.dg/lengthof.c diff --git a/gcc/testsuite/gcc.dg/lengthof-compile.c b/gcc/testsuite/gcc.dg/lengthof-compile.c new file mode 100644 index 00000000000..b5ca8978a99 --- /dev/null +++ b/gcc/testsuite/gcc.dg/lengthof-compile.c @@ -0,0 +1,48 @@ +/* { dg-do compile } */ + +extern int x[]; + +void +incomplete(int p[]) +{ + unsigned n; + + n = __lengthof__(x); /* { dg-error "incomplete" } */ + + /* We want to support the following one in the future, + but for now it should fail. */ + n = __lengthof__(p); /* { dg-error "invalid" } */ +} + +void +fam(void) +{ + struct { + int x; + int fam[]; + } s; + unsigned n; + + n = __lengthof__(s.fam); /* { dg-error "incomplete" } */ +} + +void fix_fix(int i, char (*a)[3][5], int (*x)[__lengthof__(*a)]); +void fix_var(int i, char (*a)[3][i], int (*x)[__lengthof__(*a)]); +void fix_uns(int i, char (*a)[3][*], int (*x)[__lengthof__(*a)]); + +void +func(void) +{ + int i3[3]; + int i5[5]; + char c35[3][5]; + + fix_fix(5, &c35, &i3); + fix_fix(5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */ + + fix_var(5, &c35, &i3); + fix_var(5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */ + + fix_uns(5, &c35, &i3); + fix_uns(5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */ +} diff --git a/gcc/testsuite/gcc.dg/lengthof.c b/gcc/testsuite/gcc.dg/lengthof.c new file mode 100644 index 00000000000..6aec558749c --- /dev/null +++ b/gcc/testsuite/gcc.dg/lengthof.c @@ -0,0 +1,126 @@ +/* { dg-do run } */ + +#undef NDEBUG +#include + +void +array(void) +{ + short a[7]; + + assert(__lengthof__(a) == 7); + assert(__lengthof__(long [0]) == 0); + assert(__lengthof__(unsigned [99]) == 99); +} + +void +vla(void) +{ + unsigned n; + + n = 99; + assert(__lengthof__(short [n - 10]) == 99 - 10); + + int v[n / 2]; + assert(__lengthof__(v) == 99 / 2); + + n = 0; + int z[n]; + assert(__lengthof__(z) == 0); +} + +void +member(void) +{ + struct { + int a[8]; + } s; + + assert(__lengthof__(s.a) == 8); +} + +void +vla_eval(void) +{ + int i; + + i = 7; + assert(__lengthof__(struct {int x;}[i++]) == 7); + assert(i == 7 + 1); + + int v[i]; + int (*p)[i]; + p = &v; + assert(__lengthof__(*p++) == i); + assert(p - 1 == &v); +} + +void +inner_vla_noeval(void) +{ + int i; + + i = 3; + assert(__lengthof__(struct {int x[i++];}[3]) == 3); + assert(i == 3); +} + +void +array_noeval(void) +{ + long a[5]; + long (*p)[__lengthof__(a)]; + + p = &a; + assert(__lengthof__(*p++) == 5); + assert(p == &a); +} + +void +matrix_zero(void) +{ + int i; + + assert(__lengthof__(int [0][4]) == 0); + i = 3; + assert(__lengthof__(int [0][i]) == 0); +} + +void +matrix_fixed(void) +{ + int i; + + assert(__lengthof__(int [7][4]) == 7); + i = 3; + assert(__lengthof__(int [7][i]) == 7); +} + +void +matrix_vla(void) +{ + int i, j; + + i = 7; + assert(__lengthof__(int [i++][4]) == 7); + assert(i == 7 + 1); + + i = 9; + j = 3; + assert(__lengthof__(int [i++][j]) == 9); + assert(i == 9 + 1); +} + +int +main(void) +{ + array(); + vla(); + member(); + vla_eval(); + inner_vla_noeval(); + array_noeval(); + matrix_zero(); + matrix_fixed(); + matrix_vla(); +}