From patchwork Fri Aug 11 17:31:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 134606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1250088vqi; Fri, 11 Aug 2023 10:32:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFwxCYsZ8AVkmYnNV9r24c5jmIpqdjLPO9tng3xLDzDHNX9d0JT+bdcRk+5xUnalWYIQfbX X-Received: by 2002:a17:906:18:b0:99b:48b9:8e45 with SMTP id 24-20020a170906001800b0099b48b98e45mr2391596eja.76.1691775135058; Fri, 11 Aug 2023 10:32:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691775135; cv=none; d=google.com; s=arc-20160816; b=0YutegvOFcY+P8+Fq31Yp+xSrU+4aDFTZNL/kphDwiPjBp0Sn1CTfALT+tdLLv20tp o7hhV/rSXINy6E1E7yJkF7IZp+rmkAkQ768F59NH1Sp5ANeKr0+xbi/N57vxEVAmMKZ0 R/1qwexAVIUjR+bXHesnL8jU7HWcE+QsXQ9Bg1/tqCmGlC+S70WJF27sl1+7BRU7irPf QfW6/ObcLoo3ncpqLe+5vx6SGdKchvuhph3SP668NC7mDmT6pVm8aQyXKZYn0jqRXMdR cynjKKtTP0nM9tIsyjUooJ+2cNUk+AJyS+1QZEI7gnm+mcDxkzCOa4OBa++HkbCV6yoD g1zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=TUYlTP8mCpVAZ2+VShDQRmmvV1XKKvbLlbfoQpFmEV0=; fh=93B2EkbdSZtlf0r5fbRgrRBI22A7uhJ3jVicEXL/JQs=; b=Nr4w+OLprglkV+ghCRVnvjWOtsmYGJXfEQ+JclzbzqMpME/bPP183b+9A+WSXxFRhA I5TatiMKt096VMk5Q8WPZ4tnczQI9AEZzw7LWE60UYXIXCGjLOGNj7GvF6Jfe6iGxOwN iPQ1QXLCQV8eFzsiN8/3luotfGZDBa9VLyJJzFmVryVdCjdVFS0nvR1qu3I44iBA3UFi eusOg1fpqsiUOanxDjkxizYhwwSi7TsA6wU9hspbQDGzVMtuwArabGiRwgkYoK3xmAIg BptGKr0//p4/x3Pa/Qputkwp5+0dL/kpKOmrxxjaLp3AG+M3EmE/fiaa0P1O2F/X9HIu HTFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="e81gfJh/"; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id uz11-20020a170907118b00b0099b4985e4d5si4026110ejb.451.2023.08.11.10.32.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 10:32:15 -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; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="e81gfJh/"; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBF6C3858CD1 for ; Fri, 11 Aug 2023 17:31:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBF6C3858CD1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691775118; bh=TUYlTP8mCpVAZ2+VShDQRmmvV1XKKvbLlbfoQpFmEV0=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=e81gfJh/duBTKT+YZvopwe31a3EWjZKmhfbcn6hM9gtCF4ZM7WtPF+76PySIlPQYo nifq5SPN/4bbuZsust14z7GuWFjvDngXeBuJk+kAHumEIWGbCbmb1jwEXc7lHTItA+ mnwTUOdGeZKtuLL1P55wvcmwKOV1PHSdZI4Ls6C8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 04D373858436 for ; Fri, 11 Aug 2023 17:31:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04D373858436 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-380-8_kwNLozNSGluXsiwiyw7Q-1; Fri, 11 Aug 2023 13:31:12 -0400 X-MC-Unique: 8_kwNLozNSGluXsiwiyw7Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5D2638011AD; Fri, 11 Aug 2023 17:31:12 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.42]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 21F5E5CC9A; Fri, 11 Aug 2023 17:31:11 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 37BHV97b036517 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 11 Aug 2023 19:31:09 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37BHV8eu036516; Fri, 11 Aug 2023 19:31:08 +0200 Date: Fri, 11 Aug 2023 19:31:08 +0200 To: Joseph Myers Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c, v4: Add stdckdint.h header for C23 Message-ID: References: <94c16b89-141e-35a0-a7e7-ff5e23c711a9@codesourcery.com> <25aa318c-5566-af62-6acb-6557e20bc8@codesourcery.com> MIME-Version: 1.0 In-Reply-To: <25aa318c-5566-af62-6acb-6557e20bc8@codesourcery.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773954803838736351 X-GMAIL-MSGID: 1773954803838736351 On Fri, Aug 11, 2023 at 01:25:38PM +0000, Joseph Myers wrote: > On Fri, 11 Aug 2023, Jakub Jelinek wrote: > > > All that is diagnosed is when result is bool or enum (any kind). Even for > > I'd suggest tests that other nonsense cases are diagnosed, such as > floating-point or pointer arguments or results (hopefully such cases are > already diagnosed and just need tests). So like this then? 2023-08-11 Jakub Jelinek * Makefile.in (USER_H): Add stdckdint.h. * ginclude/stdckdint.h: New file. * gcc.dg/stdckdint-1.c: New test. * gcc.dg/stdckdint-2.c: New test. Jakub --- gcc/Makefile.in.jj 2023-08-11 10:15:49.669691051 +0200 +++ gcc/Makefile.in 2023-08-11 18:48:52.829964582 +0200 @@ -469,6 +469,7 @@ USER_H = $(srcdir)/ginclude/float.h \ $(srcdir)/ginclude/stdnoreturn.h \ $(srcdir)/ginclude/stdalign.h \ $(srcdir)/ginclude/stdatomic.h \ + $(srcdir)/ginclude/stdckdint.h \ $(EXTRA_HEADERS) USER_H_INC_NEXT_PRE = @user_headers_inc_next_pre@ --- gcc/ginclude/stdckdint.h.jj 2023-08-11 18:48:52.829964582 +0200 +++ gcc/ginclude/stdckdint.h 2023-08-11 18:48:52.829964582 +0200 @@ -0,0 +1,40 @@ +/* Copyright (C) 2023 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +Under Section 7 of GPL version 3, you are granted additional +permissions described in the GCC Runtime Library Exception, version +3.1, as published by the Free Software Foundation. + +You should have received a copy of the GNU General Public License and +a copy of the GCC Runtime Library Exception along with this program; +see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +. */ + +/* ISO C23: 7.20 Checked Integer Arithmetic . */ + +#ifndef _STDCKDINT_H +#define _STDCKDINT_H + +#define __STDC_VERSION_STDCKDINT_H__ 202311L + +#define ckd_add(r, a, b) ((_Bool) __builtin_add_overflow (a, b, r)) +#define ckd_sub(r, a, b) ((_Bool) __builtin_sub_overflow (a, b, r)) +#define ckd_mul(r, a, b) ((_Bool) __builtin_mul_overflow (a, b, r)) + +/* Allow for the C library to add its part to the header. */ +#if !defined (_LIBC_STDCKDINT_H) && __has_include_next () +# include_next +#endif + +#endif /* stdckdint.h */ --- gcc/testsuite/gcc.dg/stdckdint-1.c.jj 2023-08-11 18:48:52.829964582 +0200 +++ gcc/testsuite/gcc.dg/stdckdint-1.c 2023-08-11 18:48:52.829964582 +0200 @@ -0,0 +1,61 @@ +/* Test C23 Checked Integer Arithmetic macros in . */ +/* { dg-do run } */ +/* { dg-options "-std=c2x" } */ + +#include + +#if __STDC_VERSION_STDCKDINT_H__ != 202311L +# error __STDC_VERSION_STDCKDINT_H__ not defined to 202311L +#endif + +extern void abort (void); + +int +main () +{ + unsigned int a; + if (ckd_add (&a, 1, 2) || a != 3) + abort (); + if (ckd_add (&a, ~2U, 2) || a != ~0U) + abort (); + if (!ckd_add (&a, ~2U, 4) || a != 1) + abort (); + if (ckd_sub (&a, 42, 2) || a != 40) + abort (); + if (!ckd_sub (&a, 11, ~0ULL) || a != 12) + abort (); + if (ckd_mul (&a, 42, 16U) || a != 672) + abort (); + if (ckd_mul (&a, ~0UL, 0) || a != 0) + abort (); + if (ckd_mul (&a, 1, ~0U) || a != ~0U) + abort (); + if (ckd_mul (&a, ~0UL, 1) != (~0UL > ~0U) || a != ~0U) + abort (); + static_assert (_Generic (ckd_add (&a, 1, 1), bool: 1, default: 0)); + static_assert (_Generic (ckd_sub (&a, 1, 1), bool: 1, default: 0)); + static_assert (_Generic (ckd_mul (&a, 1, 1), bool: 1, default: 0)); + signed char b; + if (ckd_add (&b, 8, 12) || b != 20) + abort (); + if (ckd_sub (&b, 8UL, 12ULL) || b != -4) + abort (); + if (ckd_mul (&b, 2, 3) || b != 6) + abort (); + unsigned char c; + if (ckd_add (&c, 8, 12) || c != 20) + abort (); + if (ckd_sub (&c, 8UL, 12ULL) != (-4ULL > (unsigned char) -4U) + || c != (unsigned char) -4U) + abort (); + if (ckd_mul (&c, 2, 3) || c != 6) + abort (); + long long d; + if (ckd_add (&d, ~0U, ~0U) != (~0U + 1ULL < ~0U) + || d != (long long) (2 * (unsigned long long) ~0U)) + abort (); + if (ckd_sub (&d, 0, 0) || d != 0) + abort (); + if (ckd_mul (&d, 16, 1) || d != 16) + abort (); +} --- gcc/testsuite/gcc.dg/stdckdint-2.c.jj 2023-08-11 18:48:52.829964582 +0200 +++ gcc/testsuite/gcc.dg/stdckdint-2.c 2023-08-11 19:28:50.081643961 +0200 @@ -0,0 +1,87 @@ +/* Test C23 Checked Integer Arithmetic macros in . */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x" } */ + +#include + +int +main () +{ + char a; + bool b; + enum E { E1, E2 } c = E1; + int d; + int *e; + float f; + double g; + long double h; + ckd_add (&a, 1, 1); + ckd_sub (&a, 1, 1); + ckd_mul (&a, 1, 1); + ckd_add (&b, 1, 1); /* { dg-error "has pointer to boolean type" } */ + ckd_sub (&b, 1, 1); /* { dg-error "has pointer to boolean type" } */ + ckd_mul (&b, 1, 1); /* { dg-error "has pointer to boolean type" } */ + ckd_add (&c, 1, 1); /* { dg-error "has pointer to enumerated type" } */ + ckd_sub (&c, 1, 1); /* { dg-error "has pointer to enumerated type" } */ + ckd_mul (&c, 1, 1); /* { dg-error "has pointer to enumerated type" } */ + ckd_add (&d, (char) 1, 1); + ckd_sub (&d, (char) 1, 1); + ckd_mul (&d, (char) 1, 1); + ckd_add (&d, false, 1); + ckd_sub (&d, false, 1); + ckd_mul (&d, false, 1); + ckd_add (&d, true, 1); + ckd_sub (&d, true, 1); + ckd_mul (&d, true, 1); + ckd_add (&d, c, 1); + ckd_sub (&d, c, 1); + ckd_mul (&d, c, 1); + ckd_add (&d, 1, (char) 1); + ckd_sub (&d, 1, (char) 1); + ckd_mul (&d, 1, (char) 1); + ckd_add (&d, 1, false); + ckd_sub (&d, 1, false); + ckd_mul (&d, 1, false); + ckd_add (&d, 1, true); + ckd_sub (&d, 1, true); + ckd_mul (&d, 1, true); + ckd_add (&d, 1, c); + ckd_sub (&d, 1, c); + ckd_mul (&d, 1, c); + ckd_add (&e, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_sub (&e, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_mul (&e, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_add (&f, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_sub (&f, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_mul (&f, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_add (&g, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_sub (&g, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_mul (&g, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_add (&h, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_sub (&h, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_mul (&h, 1, 1); /* { dg-error "does not have pointer to integral type" } */ + ckd_add (&d, 1.0f, 1); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1.0f, 1); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1.0f, 1); /* { dg-error "does not have integral type" } */ + ckd_add (&d, 1.0, 1); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1.0, 1); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1.0, 1); /* { dg-error "does not have integral type" } */ + ckd_add (&d, 1.0L, 1); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1.0L, 1); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1.0L, 1); /* { dg-error "does not have integral type" } */ + ckd_add (&d, 1, 1.0f); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1, 1.0f); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1, 1.0f); /* { dg-error "does not have integral type" } */ + ckd_add (&d, 1, 1.0); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1, 1.0); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1, 1.0); /* { dg-error "does not have integral type" } */ + ckd_add (&d, 1, 1.0L); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1, 1.0L); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1, 1.0L); /* { dg-error "does not have integral type" } */ + ckd_add (&d, (int *) 0, 1); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, (int *) 0, 1); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, (int *) 0, 1); /* { dg-error "does not have integral type" } */ + ckd_add (&d, 1, (int *) 0); /* { dg-error "does not have integral type" } */ + ckd_sub (&d, 1, (int *) 0); /* { dg-error "does not have integral type" } */ + ckd_mul (&d, 1, (int *) 0); /* { dg-error "does not have integral type" } */ +}