Skip to content

Commit 361d230

Browse files
committed
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.
1 parent 6ce1df3 commit 361d230

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

libstdc++-v3/doc/html/manual/using_macros.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@
8282
This is described in more detail in
8383
<a class="link" href="ext_compile_checks.html" title="Chapter 16. Compile Time Checks">Compile Time Checks</a>.
8484
</p></dd><dt><span class="term"><code class="code">_GLIBCXX_ASSERTIONS</code></span></dt><dd><p>
85-
Undefined by default. When defined, enables extra error checking in
86-
the form of precondition assertions, such as bounds checking in
87-
strings and null pointer checks when dereferencing smart pointers.
85+
Defined by default when compiling with no optimization, undefined
86+
by default when compiling with optimization.
87+
When defined, enables extra error checking in the form of
88+
precondition assertions, such as bounds checking in strings
89+
and null pointer checks when dereferencing smart pointers.
90+
</p></dd><dt><span class="term"><code class="code">_GLIBCXX_NO_ASSERTIONS</code></span></dt><dd><p>
91+
Undefined by default. When defined, prevents the implicit
92+
definition of <code class="code">_GLIBCXX_ASSERTIONS</code> when compiling
93+
with no optimization.
8894
</p></dd><dt><span class="term"><code class="code">_GLIBCXX_DEBUG</code></span></dt><dd><p>
8995
Undefined by default. When defined, compiles user code using
9096
the <a class="link" href="debug_mode.html" title="Chapter 17. Debug Mode">debug mode</a>.

libstdc++-v3/doc/xml/manual/using.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,19 @@ g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
12471247
<varlistentry><term><code>_GLIBCXX_ASSERTIONS</code></term>
12481248
<listitem>
12491249
<para>
1250-
Undefined by default. When defined, enables extra error checking in
1251-
the form of precondition assertions, such as bounds checking in
1252-
strings and null pointer checks when dereferencing smart pointers.
1250+
Defined by default when compiling with no optimization, undefined
1251+
by default when compiling with optimization.
1252+
When defined, enables extra error checking in the form of
1253+
precondition assertions, such as bounds checking in strings
1254+
and null pointer checks when dereferencing smart pointers.
1255+
</para>
1256+
</listitem></varlistentry>
1257+
<varlistentry><term><code>_GLIBCXX_NO_ASSERTIONS</code></term>
1258+
<listitem>
1259+
<para>
1260+
Undefined by default. When defined, prevents the implicit
1261+
definition of <code>_GLIBCXX_ASSERTIONS</code> when compiling
1262+
with no optimization.
12531263
</para>
12541264
</listitem></varlistentry>
12551265
<varlistentry><term><code>_GLIBCXX_DEBUG</code></term>

libstdc++-v3/include/bits/c++config

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,14 @@ namespace std
586586
#pragma GCC visibility pop
587587
}
588588

589+
#ifndef _GLIBCXX_ASSERTIONS
590+
# if defined(_GLIBCXX_DEBUG)
589591
// Debug Mode implies checking assertions.
590-
#if defined(_GLIBCXX_DEBUG) && !defined(_GLIBCXX_ASSERTIONS)
591-
# define _GLIBCXX_ASSERTIONS 1
592+
# define _GLIBCXX_ASSERTIONS 1
593+
# elif ! defined(__OPTIMIZE__) && ! defined(_GLIBCXX_NO_ASSERTIONS)
594+
// Enable assertions for unoptimized builds.
595+
# define _GLIBCXX_ASSERTIONS 1
596+
# endif
592597
#endif
593598

594599
// Disable std::string explicit instantiation declarations in order to assert.

0 commit comments

Comments
 (0)