From: Jonathan Wakely Date: Thu, 26 Sep 2024 15:55:07 +0000 (+0100) Subject: libstdc++: Enable _GLIBCXX_ASSERTIONS by default for -O0 [PR112808] X-Git-Tag: basepoints/gcc-16~5345 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=361d230fd7800a7e749aba8ed020f54f5c26d504;p=thirdparty%2Fgcc.git libstdc++: Enable _GLIBCXX_ASSERTIONS by default for -O0 [PR112808] 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. --- 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.