From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 F05113858C2D for ; Thu, 3 Nov 2022 19:57:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F05113858C2D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667505476; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tRFyOHubI9aXSlUC7SGe0GkQfIDnuI0hQ86wbGzIbXc=; b=XRngs5HYWKXtblzoh6XcX9ItmglKQUFRwLw4p6PClRixKYfA2TEzFFvtycqyroqX3jnY0F bhOe9l1T/Wrk1LoAbK9p5Iq65AoxFPIskdPeMMeBlJy3n2bOQwO6zPDDrhOzGKX28VuFNo lmd0PlS57fvfyxoeBvOkLi8l1r4oUJU= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-114-NVqA8qwcMW6xrvL8iyijJA-1; Thu, 03 Nov 2022 15:57:54 -0400 X-MC-Unique: NVqA8qwcMW6xrvL8iyijJA-1 Received: by mail-qv1-f71.google.com with SMTP id q17-20020a056214019100b004b1d3c9f3acso2046632qvr.0 for ; Thu, 03 Nov 2022 12:57:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tRFyOHubI9aXSlUC7SGe0GkQfIDnuI0hQ86wbGzIbXc=; b=XdeZjhG8t/GVmx3lIZIDsFCk9PtNbEdkMBzyCdYUwv0q2fyo/+o8gROb9jJGudlta5 ghTHDYcfue4Xdi+RYqMgmynSuP71I7Fr7/XNOmeV1DlHg0901+9m+lnuQfSKVctaZWII 5XPQMEkRQxQJSt0VN6igW3K8DlV8Q0JQvzIMnnSV//91W3lKe+1ItVUp0XWMLEDbISAm a4qsqqYEvBUGuGWuMcda7eyqDczXw4xRZTxgqmH2MZnBJbR71diV13Njs0F+c3M6It2t Q4tOWW8RHXaoBd9g2UJ40k9e/1qOtKC1jzplh2pa6RO5bqtCc2HudZj+Xn/9V3BDxjCl 1cfQ== X-Gm-Message-State: ACrzQf10MsDHeeuT2gwgXQ/NQxVyCCVHpl3Fort7dC72aYueJJEigS8s hxg26Co4TQit4SRMJuoQdtYYqyEAvCWZvljAJ4i2AVc6hWlS79nwQwy90FfmX6bXKb5iiKRq1Cw f+/XrMFGbCGaReNFFHJq2pYRjQPWWwFXA/PmR6kwZ5uqaQb7UQJQoYM+21KuHlW8y/g== X-Received: by 2002:a05:6214:d87:b0:4bb:942b:f5f9 with SMTP id e7-20020a0562140d8700b004bb942bf5f9mr28238200qve.126.1667505473444; Thu, 03 Nov 2022 12:57:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7ILVLA5y2uj53mgxk71QerrTNrVmlCrWcSViBSR2hRj4JcKt4mZzFOimz/4nlM2QdI+dN9Wg== X-Received: by 2002:a05:6214:d87:b0:4bb:942b:f5f9 with SMTP id e7-20020a0562140d8700b004bb942bf5f9mr28238164qve.126.1667505472856; Thu, 03 Nov 2022 12:57:52 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id bi11-20020a05620a318b00b006eeca296c00sm1289009qkb.104.2022.11.03.12.57.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 12:57:52 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] libstdc++: add experimental Contracts support Date: Thu, 3 Nov 2022 15:57:50 -0400 Message-Id: <20221103195750.2113734-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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 List-Id: Tested x86_64-pc-linux-gnu. OK for trunk? -- >8 -- This patch adds the library support for the experimental C++ Contracts implementation. This now consists only of a default definition of the violation handler, which users can override through defining their own version. To avoid ABI stability problems with libstdc++.so this is added to a separate -lstdc++exp static library, which the driver knows to add when it sees -fcontracts. libstdc++-v3/ChangeLog: * acinclude.m4 (glibcxx_SUBDIRS): Add src/experimental. * include/Makefile.am (experimental_headers): Add contract. * include/Makefile.in: Regenerate. * src/Makefile.am (SUBDIRS): Add experimental. * src/Makefile.in: Regenerate. * configure: Regenerate. * src/experimental/contract.cc: New file. * src/experimental/Makefile.am: New file. * src/experimental/Makefile.in: New file. * include/experimental/contract: New file. --- libstdc++-v3/src/experimental/contract.cc | 41 ++ libstdc++-v3/acinclude.m4 | 2 +- libstdc++-v3/include/Makefile.am | 1 + libstdc++-v3/include/Makefile.in | 1 + libstdc++-v3/src/Makefile.am | 3 +- libstdc++-v3/src/Makefile.in | 6 +- libstdc++-v3/src/experimental/Makefile.am | 96 +++ libstdc++-v3/src/experimental/Makefile.in | 796 +++++++++++++++++++++ libstdc++-v3/include/experimental/contract | 84 +++ 9 files changed, 1026 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/src/experimental/contract.cc create mode 100644 libstdc++-v3/src/experimental/Makefile.am create mode 100644 libstdc++-v3/src/experimental/Makefile.in create mode 100644 libstdc++-v3/include/experimental/contract diff --git a/libstdc++-v3/src/experimental/contract.cc b/libstdc++-v3/src/experimental/contract.cc new file mode 100644 index 00000000000..b9b72cd7df0 --- /dev/null +++ b/libstdc++-v3/src/experimental/contract.cc @@ -0,0 +1,41 @@ +// -*- C++ -*- std::experimental::contract_violation and friends +// Copyright (C) 1994-2022 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 +// . + +#include +#include + +__attribute__ ((weak)) void +handle_contract_violation (const std::experimental::contract_violation &violation) +{ + std::cerr << "default std::handle_contract_violation called: " << std::endl + << " " << violation.file_name() + << " " << violation.line_number() + << " " << violation.function_name() + << " " << violation.comment() + << " " << violation.assertion_level() + << " " << violation.assertion_role() + << " " << (int)violation.continuation_mode() + << std::endl; +} + diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 6f672924a73..baf01913a90 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -49,7 +49,7 @@ AC_DEFUN([GLIBCXX_CONFIGURE], [ # Keep these sync'd with the list in Makefile.am. The first provides an # expandable list at autoconf time; the second provides an expandable list # (i.e., shell variable) at configure time. - m4_define([glibcxx_SUBDIRS],[include libsupc++ src src/c++98 src/c++11 src/c++17 src/c++20 src/filesystem src/libbacktrace doc po testsuite python]) + m4_define([glibcxx_SUBDIRS],[include libsupc++ src src/c++98 src/c++11 src/c++17 src/c++20 src/filesystem src/libbacktrace src/experimental doc po testsuite python]) SUBDIRS='glibcxx_SUBDIRS' # These need to be absolute paths, yet at the same time need to diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index 96137a6621a..a6f9912cb9b 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -769,6 +769,7 @@ experimental_headers = \ ${experimental_srcdir}/array \ ${experimental_srcdir}/buffer \ ${experimental_srcdir}/chrono \ + ${experimental_srcdir}/contract \ ${experimental_srcdir}/deque \ ${experimental_srcdir}/executor \ ${experimental_srcdir}/forward_list \ diff --git a/libstdc++-v3/src/Makefile.am b/libstdc++-v3/src/Makefile.am index b83c222d51d..6f08068326d 100644 --- a/libstdc++-v3/src/Makefile.am +++ b/libstdc++-v3/src/Makefile.am @@ -37,7 +37,7 @@ backtrace_supported_h = endif ## Keep this list sync'd with acinclude.m4:GLIBCXX_CONFIGURE. -SUBDIRS = c++98 c++11 c++17 c++20 $(filesystem_dir) $(backtrace_dir) +SUBDIRS = c++98 c++11 c++17 c++20 $(filesystem_dir) $(backtrace_dir) experimental # Cross compiler support. if VTV_CYGMIN @@ -75,6 +75,7 @@ endif if ENABLE_BACKTRACE vpath % $(top_srcdir)/src/libbacktrace endif +vpath % $(top_srcdir)/src/experimental if GLIBCXX_LDBL_COMPAT ldbl_compat_sources = compatibility-ldbl.cc diff --git a/libstdc++-v3/src/experimental/Makefile.am b/libstdc++-v3/src/experimental/Makefile.am new file mode 100644 index 00000000000..a1c2f62a518 --- /dev/null +++ b/libstdc++-v3/src/experimental/Makefile.am @@ -0,0 +1,96 @@ +## Makefile for experimental additions to the GNU C++ Standard library. +## +## Copyright (C) 2014-2022 Free Software Foundation, Inc. +## +## Process this file with automake to produce Makefile.in. +## +## 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. +## +## You should have received a copy of the GNU General Public License +## along with GCC; see the file COPYING3. If not see +## . + +include $(top_srcdir)/fragment.am + +toolexeclib_LTLIBRARIES = libstdc++exp.la + +headers = + +sources = \ + contract.cc + +# vpath % $(top_srcdir)/src/experimental + +libstdc__exp_la_SOURCES = $(sources) + +# AM_CXXFLAGS needs to be in each subdirectory so that it can be +# modified in a per-library or per-sub-library way. Need to manually +# set this option because CONFIG_CXXFLAGS has to be after +# OPTIMIZE_CXXFLAGS on the compile line so that -O2 can be overridden +# as the occasion calls for it. +AM_CXXFLAGS = \ + -std=gnu++17 -nostdinc++ \ + $(glibcxx_lt_pic_flag) $(glibcxx_compiler_shared_flag) \ + $(XTEMPLATE_FLAGS) $(VTV_CXXFLAGS) \ + $(WARN_CXXFLAGS) $(OPTIMIZE_CXXFLAGS) $(CONFIG_CXXFLAGS) \ + -fimplicit-templates + +AM_MAKEFLAGS = \ + "gxx_include_dir=$(gxx_include_dir)" + +# Libtool notes + +# 1) In general, libtool expects an argument such as `--tag=CXX' when +# using the C++ compiler, because that will enable the settings +# detected when C++ support was being configured. However, when no +# such flag is given in the command line, libtool attempts to figure +# it out by matching the compiler name in each configuration section +# against a prefix of the command line. The problem is that, if the +# compiler name and its initial flags stored in the libtool +# configuration file don't match those in the command line, libtool +# can't decide which configuration to use, and it gives up. The +# correct solution is to add `--tag CXX' to LTCXXCOMPILE and maybe +# CXXLINK, just after $(LIBTOOL), so that libtool doesn't have to +# attempt to infer which configuration to use. +# +# The second tag argument, `--tag disable-shared` means that libtool +# only compiles each source once, for static objects. In actuality, +# glibcxx_lt_pic_flag and glibcxx_compiler_shared_flag are added to +# the libtool command that is used create the object, which is +# suitable for shared libraries. The `--tag disable-shared` must be +# placed after --tag CXX lest things CXX undo the affect of +# disable-shared. + +# 2) Need to explicitly set LTCXXCOMPILE so that EXTRA_CXX_FLAGS is +# last. (That way, things like -O2 passed down from the toplevel can +# be overridden by --enable-debug.) +LTCXXCOMPILE = \ + $(LIBTOOL) --tag CXX --tag disable-shared \ + $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=compile $(CXX) $(TOPLEVEL_INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) $(EXTRA_CXX_FLAGS) + +LTLDFLAGS = $(shell $(SHELL) $(top_srcdir)/../libtool-ldflags $(LDFLAGS)) + +# 3) We'd have a problem when building the shared libstdc++ object if +# the rules automake generates would be used. We cannot allow g++ to +# be used since this would add -lstdc++ to the link line which of +# course is problematic at this point. So, we get the top-level +# directory to configure libstdc++-v3 to use gcc as the C++ +# compilation driver. +CXXLINK = \ + $(LIBTOOL) --tag CXX --tag disable-shared \ + $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=link $(CXX) \ + $(VTV_CXXLINKFLAGS) \ + $(OPT_LDFLAGS) $(SECTION_LDFLAGS) $(AM_CXXFLAGS) $(LTLDFLAGS) -o $@ diff --git a/libstdc++-v3/include/experimental/contract b/libstdc++-v3/include/experimental/contract new file mode 100644 index 00000000000..16f3fd5fd5a --- /dev/null +++ b/libstdc++-v3/include/experimental/contract @@ -0,0 +1,82 @@ +// Contracts support header for -*- C++ -*- + +// Copyright (C) 1995-2022 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 +// . + +/** @file contract + * This is a Standard C++ Library header. + */ + +#ifndef _GLIBCXX_CONTRACT +#define _GLIBCXX_CONTRACT 1 + +#pragma GCC system_header + +#if __cplusplus >= 201703L + +#include +#include +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + +namespace experimental +{ + // From P1332 + enum class contract_violation_continuation_mode { + NEVER_CONTINUE, MAYBE_CONTINUE + }; + + class contract_violation { + const char* _M_file; + const char* _M_function; + const char* _M_comment; + const char* _M_level; + const char* _M_role; + uint_least32_t _M_line; + signed char _M_continue; + public: + // From N4820 + uint_least32_t line_number() const noexcept { return _M_line; } + string_view file_name() const noexcept { return _M_file; } + string_view function_name() const noexcept { return _M_function; } + string_view comment() const noexcept { return _M_comment; } + string_view assertion_level() const noexcept { return _M_level; } + // From P1332 + string_view assertion_role() const noexcept { return _M_role; } + contract_violation_continuation_mode continuation_mode() const noexcept + { return static_cast(_M_continue); } + }; + +} // namespace experimental + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std + +// To override the contract violation handler, define +//void ::handle_contract_violation (const std::experimental::contract_violation &); + +#endif // C++17 +#endif // _GLIBCXX_CONTRACT base-commit: a4cd2389276a30c39034a83d640ce68fa407bac1 prerequisite-patch-id: 329bc16a88dc9a3b13cd3fcecb3678826cc592dc -- 2.31.1