]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Enable _GLIBCXX_ASSERTIONS by default for -O0 [PR112808]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 26 Sep 2024 15:55:07 +0000 (16:55 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 9 Oct 2024 12:39:16 +0000 (13:39 +0100)
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
libstdc++-v3/doc/xml/manual/using.xml
libstdc++-v3/include/bits/c++config

index 67623b5e2aff4e2b95b6b0884e03f99e67fde884..c1406ec76f777c99cdae7beb63e7095c0788dd0b 100644 (file)
        This is described in more detail in
        <a class="link" href="ext_compile_checks.html" title="Chapter 16. Compile Time Checks">Compile Time Checks</a>.
       </p></dd><dt><span class="term"><code class="code">_GLIBCXX_ASSERTIONS</code></span></dt><dd><p>
-       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.
+      </p></dd><dt><span class="term"><code class="code">_GLIBCXX_NO_ASSERTIONS</code></span></dt><dd><p>
+       Undefined by default.  When defined, prevents the implicit
+       definition of <code class="code">_GLIBCXX_ASSERTIONS</code> when compiling
+       with no optimization.
       </p></dd><dt><span class="term"><code class="code">_GLIBCXX_DEBUG</code></span></dt><dd><p>
        Undefined by default. When defined, compiles user code using
        the <a class="link" href="debug_mode.html" title="Chapter 17. Debug Mode">debug mode</a>.
index 89119f6fb2db10bea29f6616972dd0feedd79fd3..7ca3a3f4b4c09dd14d3c9f7f2594df04b9836769 100644 (file)
@@ -1247,9 +1247,19 @@ g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
     <varlistentry><term><code>_GLIBCXX_ASSERTIONS</code></term>
     <listitem>
       <para>
-       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.
+      </para>
+    </listitem></varlistentry>
+    <varlistentry><term><code>_GLIBCXX_NO_ASSERTIONS</code></term>
+    <listitem>
+      <para>
+       Undefined by default.  When defined, prevents the implicit
+       definition of <code>_GLIBCXX_ASSERTIONS</code> when compiling
+       with no optimization.
       </para>
     </listitem></varlistentry>
     <varlistentry><term><code>_GLIBCXX_DEBUG</code></term>
index 29d795f687c6bda0883b42db5a883ded04e67786..b87a3527f24befc7c9360635fc56e6d929183116 100644 (file)
@@ -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.