From patchwork Fri Aug 11 08:09:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 134355 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp930329vqi; Fri, 11 Aug 2023 01:10:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZe4NxuvAd43Eu9dPCTU2sryT8e0oWkYA/gAAxfwROE2byPrcmSelu2mJRKrCbpOBAmXQe X-Received: by 2002:aa7:d98d:0:b0:523:6bce:6a83 with SMTP id u13-20020aa7d98d000000b005236bce6a83mr1034593eds.37.1691741419598; Fri, 11 Aug 2023 01:10:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691741419; cv=none; d=google.com; s=arc-20160816; b=uum6xPHzCrAyzwc3IjztkFENZT6qOm91XaPQCRXVrE9dZA7GXezJPXfW49n/bMyLvl /Ybet7hjBDVh9IRSXmbJzgUj4GXaSmRbFvYcgzcHTCx5Jsum9WudzWopOSyyMv56hli8 8QPCn9E8karIuyHLp8rYdGDTTIYMsxO02CBscmqsD/5aP2ob2kv6DJ7Fks07xyUH+D/J tqSP0OZnFslmDPuLq29n9ArEvknbr9QwcXQRyJhSXJRZ4XD2KTx3nCxFx3po04/P7WeN wLswlfZeRR0Q8HbdASax4OXJEFGBmfuhW/rlB7n/DGZDqLO2p/od6okHuz8V9cLyaw5P YC0Q== 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=TPe+3qXbY++RZ2CQwZ6hBJSFAF1zXpasZViuHhL+xNA=; fh=93B2EkbdSZtlf0r5fbRgrRBI22A7uhJ3jVicEXL/JQs=; b=mrGo3m9zlYODHgYxYQY01OrSCKFeypDOeyjGw0uajia94XBvKowUCWvBOynyIaN25O NyiRr0w+fGNGreF2lEWCSqyPiUYIewIwYLfCKabPlERnTDAGmF5+3hdd9Pkrq9As5xjY /oUiM1X86yZCFiILW1b4O4PAGwxLXFfN7WNKeiYkgN9G8KXaE0P69jtoppisyfd24OzX 5n8eG/pfMRLZxVqOCLndmgavLPGd4KaYnz35iZIhOUjUIUZJaY0LUHzMGQk+aWmpTHCO 0da9OsoB8T3jILDCYHtzu15nw7WnUdELcXWmtokroo5lyR+4OaeqNQ+IC0oyIdxHxyvA 4CZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rfXczdBJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j22-20020aa7c0d6000000b00523b4d7af90si2070827edp.565.2023.08.11.01.10.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 01:10:19 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rfXczdBJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 792BC3858D3C for ; Fri, 11 Aug 2023 08:10:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 792BC3858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691741418; bh=TPe+3qXbY++RZ2CQwZ6hBJSFAF1zXpasZViuHhL+xNA=; 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=rfXczdBJJmH6B4bBuvS96GX+fq4MZFlKB6BKlAXQgcfngXOb59eRZrXGrJOslI9g4 CG9GgH6DVUnuvIfZNeiJ3/DqtwnWZKgMW18G5f93MkY0QNg/VeYr5GPFFG6G3wM+ee q1swAN5HtPw4ZmaakJPGRWEPlyPx+WIKPYLblhF4= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 7D8613858D20 for ; Fri, 11 Aug 2023 08:09:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D8613858D20 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-615-n0eWjaa3Miiu-g0CyiISbA-1; Fri, 11 Aug 2023 04:09:30 -0400 X-MC-Unique: n0eWjaa3Miiu-g0CyiISbA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 86EDD185A792; Fri, 11 Aug 2023 08:09:30 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.42]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4BAD6C15BAE; Fri, 11 Aug 2023 08:09:30 +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 37B89RGg009126 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 11 Aug 2023 10:09:27 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37B89QqN009125; Fri, 11 Aug 2023 10:09:26 +0200 Date: Fri, 11 Aug 2023 10:09:26 +0200 To: Joseph Myers Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c, v3: Add stdckdint.h header for C23 Message-ID: References: <94c16b89-141e-35a0-a7e7-ff5e23c711a9@codesourcery.com> MIME-Version: 1.0 In-Reply-To: <94c16b89-141e-35a0-a7e7-ff5e23c711a9@codesourcery.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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: 1773919450812055150 X-GMAIL-MSGID: 1773919450812055150 On Thu, Aug 10, 2023 at 10:31:12PM +0000, Joseph Myers wrote: > > The following patch (on top of the stdckdint.h patch and _BitInt patch > > series) adds a test for _BitInt diagnostics of ckd_{add,sub,mul} macros. > > I remain unconvinced that diagnosing use with types where it's clear what > the right semantics for the operation are is a good idea (given, in the > _BitInt case, that you've already implemented the built-in functions for > _BitInt types). (Diagnosing for bool results *is* a good idea, since it's > not clear what the semantics should be. Likewise for enums with fixed > underlying type bool, whether or not it's diagnosed for other enums.) Ok, here is an updated patch without that diagnostics. All that is diagnosed is when result is bool or enum (any kind). Even for enums without bool underlying type, a question is what exactly it would mean, whether checking result fits into the range of the underlying type, or range between smallest and largest enumerator, or signed/unsigned range with minimum precision to represent smallest/largest enumerator, or only where the result would fall into some enumerator, so I think punting on those as we do for years for __builtin_*_overflow is ok. 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-10 17:23:55.502325592 +0200 +++ gcc/Makefile.in 2023-08-11 09:37:35.968944530 +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 09:37:35.968944530 +0200 +++ gcc/ginclude/stdckdint.h 2023-08-11 09:39:50.383054196 +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 09:37:35.968944530 +0200 +++ gcc/testsuite/gcc.dg/stdckdint-1.c 2023-08-11 09:37:35.968944530 +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 09:37:35.968944530 +0200 +++ gcc/testsuite/gcc.dg/stdckdint-2.c 2023-08-11 09:56:58.536595283 +0200 @@ -0,0 +1,47 @@ +/* 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; + 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); +}