From patchwork Wed Oct 2 10:58:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1991970 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=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=M8pCfUvH; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4XJWzk28g6z1xtY for ; Wed, 2 Oct 2024 21:00:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 265DD385841C for ; Wed, 2 Oct 2024 11:00:00 +0000 (GMT) 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 ESMTP id 4C5983858402 for ; Wed, 2 Oct 2024 10:59:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C5983858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4C5983858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727866779; cv=none; b=hpdg20mt5nEKD9IZHaYjDd2az68NsW1fevzIbQkq42HrPEONY3m3upvK4baUpC9PgvlG/U1ayV1W9GnC984msAQZ2U2vhz5FIzST35SBQK8a93qz5Q/+WVHXdVJUexwzxAJQpBL82hwpkDiKKI13AsGRuldjS0BXC8ywXnsIAfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727866779; c=relaxed/simple; bh=q14EAbjU838IvWytSkmrm/1uws33lSrojFoS47Je/2E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XXc5j6dyB+wrcq1+orWWEqM4X2tQiqJ3+aU9VJFVg2dElU8QQ/rLmNsDWNGfhFiUTeE9/ySxVgDzLxS/N+C7h6TfoYRRjDs3usP3C8x+EZM/UTfz4wDVZA0sAmRBVtt8x2mbEA3l+H3rCBpLdNNpFsf1+pnkYyWz0bKPWlB/dS0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727866774; 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=rfcJwNGXnDKLO+rfnggjvCNQLzP3WU5D5WItvnz7iMM=; b=M8pCfUvHpsaC+a6xwexMFsESmJtbwOSbopkMxzB3X2f8RN8Yo9A+kJxggysWaWMCdExvx4 tTGOef1gGdgtGFfOYnJmVqDryiZA0TaDfJYNmiTzemYzSLgp58fj7ZCMuYYoi8K29/UN2i f+ZAxDFS+x1WNyYw3RKEY24E8TLbPRY= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-313-bUGGgg28N4idPvCbj8c-Ow-1; Wed, 02 Oct 2024 06:59:30 -0400 X-MC-Unique: bUGGgg28N4idPvCbj8c-Ow-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AFB541955F65; Wed, 2 Oct 2024 10:59:28 +0000 (UTC) Received: from localhost (unknown [10.42.28.136]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ED5663000197; Wed, 2 Oct 2024 10:59:27 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Enable _GLIBCXX_ASSERTIONS by default for -O0 [PR112808] Date: Wed, 2 Oct 2024 11:58:41 +0100 Message-ID: <20241002105926.3172639-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PP_MIME_FAKE_ASCII_TEXT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 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 think we should do this. Tested x86_64-linux. -- >8 -- Too many users don't know about -D_GLIBCXX_ASSERTIONS and so are missing valuable checks for C++ standard library preconditions. This change enables libstdc++ assertions by default when compiling with -O0 so that we diagnose more bugs by default. When users enable optimization we don't add the assertions by default (because they have non-zero overhead) so they still need to enable them manually. For users who really don't want the assertions even in unoptimized builds, defining _GLIBCXX_NO_ASSERTIONS will prevent them from being enabled automatically. libstdc++-v3/ChangeLog: PR libstdc++/112808 * doc/xml/manual/using.xml (_GLIBCXX_ASSERTIONS): Document implicit definition for -O0 compilation. (_GLIBCXX_NO_ASSERTIONS): Document. * doc/html/manual/using_macros.html: Regenerate. * include/bits/c++config [!__OPTIMIZE__] (_GLIBCXX_ASSERTIONS): Define for unoptimized builds. --- libstdc++-v3/doc/html/manual/using_macros.html | 12 +++++++++--- libstdc++-v3/doc/xml/manual/using.xml | 16 +++++++++++++--- libstdc++-v3/include/bits/c++config | 9 +++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/doc/html/manual/using_macros.html b/libstdc++-v3/doc/html/manual/using_macros.html index 67623b5e2af..c1406ec76f7 100644 --- a/libstdc++-v3/doc/html/manual/using_macros.html +++ b/libstdc++-v3/doc/html/manual/using_macros.html @@ -82,9 +82,15 @@ This is described in more detail in Compile Time Checks.

_GLIBCXX_ASSERTIONS

- Undefined by default. When defined, enables extra error checking in - the form of precondition assertions, such as bounds checking in - strings and null pointer checks when dereferencing smart pointers. + Defined by default when compiling with no optimization, undefined + by default when compiling with optimization. + When defined, enables extra error checking in the form of + precondition assertions, such as bounds checking in strings + and null pointer checks when dereferencing smart pointers. +

_GLIBCXX_NO_ASSERTIONS

+ Undefined by default. When defined, prevents the implicit + definition of _GLIBCXX_ASSERTIONS when compiling + with no optimization.

_GLIBCXX_DEBUG

Undefined by default. When defined, compiles user code using the debug mode. diff --git a/libstdc++-v3/doc/xml/manual/using.xml b/libstdc++-v3/doc/xml/manual/using.xml index 89119f6fb2d..7ca3a3f4b4c 100644 --- a/libstdc++-v3/doc/xml/manual/using.xml +++ b/libstdc++-v3/doc/xml/manual/using.xml @@ -1247,9 +1247,19 @@ g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe _GLIBCXX_ASSERTIONS - Undefined by default. When defined, enables extra error checking in - the form of precondition assertions, such as bounds checking in - strings and null pointer checks when dereferencing smart pointers. + Defined by default when compiling with no optimization, undefined + by default when compiling with optimization. + When defined, enables extra error checking in the form of + precondition assertions, such as bounds checking in strings + and null pointer checks when dereferencing smart pointers. + + + _GLIBCXX_NO_ASSERTIONS + + + Undefined by default. When defined, prevents the implicit + definition of _GLIBCXX_ASSERTIONS when compiling + with no optimization. _GLIBCXX_DEBUG diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 29d795f687c..b87a3527f24 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -586,9 +586,14 @@ namespace std #pragma GCC visibility pop } +#ifndef _GLIBCXX_ASSERTIONS +# if defined(_GLIBCXX_DEBUG) // Debug Mode implies checking assertions. -#if defined(_GLIBCXX_DEBUG) && !defined(_GLIBCXX_ASSERTIONS) -# define _GLIBCXX_ASSERTIONS 1 +# define _GLIBCXX_ASSERTIONS 1 +# elif ! defined(__OPTIMIZE__) && ! defined(_GLIBCXX_NO_ASSERTIONS) +// Enable assertions for unoptimized builds. +# define _GLIBCXX_ASSERTIONS 1 +# endif #endif // Disable std::string explicit instantiation declarations in order to assert.