2021-07-06 10:39:01 -04:00
|
|
|
.. _using-libcxx:
|
|
|
|
|
2015-08-22 19:40:49 +00:00
|
|
|
============
|
|
|
|
Using libc++
|
|
|
|
============
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:local:
|
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
Usually, libc++ is packaged and shipped by a vendor through some delivery vehicle
|
|
|
|
(operating system distribution, SDK, toolchain, etc) and users don't need to do
|
|
|
|
anything special in order to use the library.
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
This page contains information about configuration knobs that can be used by
|
|
|
|
users when they know libc++ is used by their toolchain, and how to use libc++
|
|
|
|
when it is not the default library used by their toolchain.
|
2015-08-22 19:40:49 +00:00
|
|
|
|
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
Using a different version of the C++ Standard
|
|
|
|
=============================================
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
Libc++ implements the various versions of the C++ Standard. Changing the version of
|
|
|
|
the standard can be done by passing ``-std=c++XY`` to the compiler. Libc++ will
|
|
|
|
automatically detect what Standard is being used and will provide functionality that
|
|
|
|
matches that Standard in the library.
|
2015-08-22 19:40:49 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
$ clang++ -std=c++17 test.cpp
|
2019-03-21 16:21:09 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
.. warning::
|
|
|
|
Using ``-std=c++XY`` with a version of the Standard that has not been ratified yet
|
|
|
|
is considered unstable. Libc++ reserves the right to make breaking changes to the
|
|
|
|
library until the standard has been ratified.
|
2019-03-21 16:21:09 +00:00
|
|
|
|
Implement <filesystem>
This patch implements the <filesystem> header and uses that
to provide <experimental/filesystem>.
Unlike other standard headers, the symbols needed for <filesystem>
have not yet been placed in libc++.so. Instead they live in the
new libc++fs.a library. Users of filesystem are required to link this
library. (Also note that libc++experimental no longer contains the
definition of <experimental/filesystem>, which now requires linking libc++fs).
The reason for keeping <filesystem> out of the dylib for now is that
it's still somewhat experimental, and the possibility of requiring an
ABI breaking change is very real. In the future the symbols will likely
be moved into the dylib, or the dylib will be made to link libc++fs automagically).
Note that moving the symbols out of libc++experimental may break user builds
until they update to -lc++fs. This should be OK, because the experimental
library provides no stability guarantees. However, I plan on looking into
ways we can force libc++experimental to automagically link libc++fs.
In order to use a single implementation and set of tests for <filesystem>, it
has been placed in a special `__fs` namespace. This namespace is inline in
C++17 onward, but not before that. As such implementation is available
in C++11 onward, but no filesystem namespace is present "directly", and
as such name conflicts shouldn't occur in C++11 or C++14.
llvm-svn: 338093
2018-07-27 03:07:09 +00:00
|
|
|
|
2022-07-19 10:44:06 -04:00
|
|
|
Enabling experimental C++ Library features
|
|
|
|
==========================================
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2022-07-19 10:44:06 -04:00
|
|
|
Libc++ provides implementations of some experimental features. Experimental features
|
|
|
|
are either Technical Specifications (TSes) or official features that were voted to
|
|
|
|
the Standard but whose implementation is not complete or stable yet in libc++. Those
|
|
|
|
are disabled by default because they are neither API nor ABI stable. However, the
|
2022-07-20 10:42:04 -04:00
|
|
|
``-fexperimental-library`` compiler flag can be defined to turn those features on.
|
2016-05-03 22:32:08 +00:00
|
|
|
|
|
|
|
.. warning::
|
2022-07-20 10:42:04 -04:00
|
|
|
Experimental libraries are experimental.
|
2022-07-19 10:44:06 -04:00
|
|
|
* The contents of the ``<experimental/...>`` headers and the associated static
|
2016-05-03 22:32:08 +00:00
|
|
|
library will not remain compatible between versions.
|
|
|
|
* No guarantees of API or ABI stability are provided.
|
2021-07-06 10:39:01 -04:00
|
|
|
* When the standardized version of an experimental feature is implemented,
|
2019-06-11 14:48:40 +00:00
|
|
|
the experimental feature is removed two releases after the non-experimental
|
|
|
|
version has shipped. The full policy is explained :ref:`here <experimental features>`.
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2022-07-20 10:42:04 -04:00
|
|
|
.. note::
|
|
|
|
On compilers that do not support the ``-fexperimental-library`` flag, users can
|
|
|
|
define the ``_LIBCPP_ENABLE_EXPERIMENTAL`` macro and manually link against the
|
|
|
|
appropriate static library (usually shipped as ``libc++experimental.a``) to get
|
|
|
|
access to experimental library features.
|
|
|
|
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
Using libc++ when it is not the system default
|
|
|
|
==============================================
|
|
|
|
|
|
|
|
On systems where libc++ is provided but is not the default, Clang provides a flag
|
|
|
|
called ``-stdlib=`` that can be used to decide which standard library is used.
|
|
|
|
Using ``-stdlib=libc++`` will select libc++:
|
2015-08-22 19:40:49 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
$ clang++ -stdlib=libc++ test.cpp
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-09-07 12:55:48 -04:00
|
|
|
On systems where libc++ is the library in use by default such as macOS and FreeBSD,
|
|
|
|
this flag is not required.
|
2015-08-22 19:40:49 +00:00
|
|
|
|
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
.. _alternate libcxx:
|
|
|
|
|
|
|
|
Using a custom built libc++
|
|
|
|
===========================
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
Most compilers provide a way to disable the default behavior for finding the
|
|
|
|
standard library and to override it with custom paths. With Clang, this can
|
|
|
|
be done with:
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
.. code-block:: bash
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
$ clang++ -nostdinc++ -nostdlib++ \
|
|
|
|
-isystem <install>/include/c++/v1 \
|
|
|
|
-L <install>/lib \
|
|
|
|
-Wl,-rpath,<install>/lib \
|
|
|
|
-lc++ \
|
|
|
|
test.cpp
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
The option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path,
|
|
|
|
which causes the system's dynamic linker to look for libc++ in ``<install>/lib``
|
|
|
|
whenever the program is loaded.
|
2015-08-22 19:40:49 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
GCC does not support the ``-nostdlib++`` flag, so one must use ``-nodefaultlibs``
|
|
|
|
instead. Since that removes all the standard system libraries and not just libc++,
|
|
|
|
the system libraries must be re-added manually. For example:
|
2015-08-22 19:40:49 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
$ g++ -nostdinc++ -nodefaultlibs \
|
|
|
|
-isystem <install>/include/c++/v1 \
|
|
|
|
-L <install>/lib \
|
|
|
|
-Wl,-rpath,<install>/lib \
|
|
|
|
-lc++ -lc++abi -lm -lc -lgcc_s -lgcc \
|
|
|
|
test.cpp
|
2016-01-20 01:26:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
GDB Pretty printers for libc++
|
2021-07-06 10:39:01 -04:00
|
|
|
==============================
|
2016-01-20 01:26:30 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does
|
|
|
|
provide pretty-printers itself. Those can be used as:
|
2016-01-20 01:26:30 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
.. code-block:: bash
|
2016-01-20 01:26:30 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
$ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \
|
|
|
|
-ex "python register_libcxx_printer_loader()" \
|
|
|
|
<args>
|
2016-11-13 23:00:30 +00:00
|
|
|
|
|
|
|
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
.. _assertions-mode:
|
|
|
|
|
|
|
|
Enabling the "safe libc++" mode
|
|
|
|
===============================
|
|
|
|
|
|
|
|
Libc++ contains a number of assertions whose goal is to catch undefined behavior in the
|
|
|
|
library, usually caused by precondition violations. Those assertions do not aim to be
|
|
|
|
exhaustive -- instead they aim to provide a good balance between safety and performance.
|
|
|
|
In particular, these assertions do not change the complexity of algorithms. However, they
|
|
|
|
might, in some cases, interfere with compiler optimizations.
|
|
|
|
|
|
|
|
By default, these assertions are turned off. Vendors can decide to turn them on while building
|
|
|
|
the compiled library by defining ``LIBCXX_ENABLE_ASSERTIONS=ON`` at CMake configuration time.
|
|
|
|
When ``LIBCXX_ENABLE_ASSERTIONS`` is used, the compiled library will be built with assertions
|
|
|
|
enabled, **and** user code will be built with assertions enabled by default. If
|
|
|
|
``LIBCXX_ENABLE_ASSERTIONS=OFF`` at CMake configure time, the compiled library will not contain
|
|
|
|
assertions and the default when building user code will be to have assertions disabled.
|
|
|
|
As a user, you can consult your vendor to know whether assertions are enabled by default.
|
|
|
|
|
|
|
|
Furthermore, independently of any vendor-selected default, users can always control whether
|
|
|
|
assertions are enabled in their code by defining ``_LIBCPP_ENABLE_ASSERTIONS=0|1`` before
|
|
|
|
including any libc++ header (we recommend passing ``-D_LIBCPP_ENABLE_ASSERTIONS=X`` to the
|
|
|
|
compiler). Note that if the compiled library was built by the vendor without assertions,
|
|
|
|
functions compiled inside the static or shared library won't have assertions enabled even
|
|
|
|
if the user defines ``_LIBCPP_ENABLE_ASSERTIONS=1`` (the same is true for the inverse case
|
|
|
|
where the static or shared library was compiled **with** assertions but the user tries to
|
|
|
|
disable them). However, most of the code in libc++ is in the headers, so the user-selected
|
|
|
|
value for ``_LIBCPP_ENABLE_ASSERTIONS`` (if any) will usually be respected.
|
|
|
|
|
2022-07-25 13:43:47 -04:00
|
|
|
When an assertion fails, the program is aborted through a special verbose termination function. The
|
|
|
|
library provides a default function that prints an error message and calls ``std::abort()``. Note
|
|
|
|
that this function is provided by the static or shared library, so it is only available when deploying
|
2022-08-04 15:25:48 -04:00
|
|
|
to a platform where the compiled library is sufficiently recent. On older platforms, the program will
|
|
|
|
terminate in an unspecified unsuccessful manner, but the quality of diagnostics won't be great.
|
|
|
|
However, users can also override that function with their own, which can be useful to either provide
|
|
|
|
custom behavior or when deploying to an older platform where the default function isn't available.
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
|
2022-08-04 15:25:48 -04:00
|
|
|
Replacing the default verbose termination function is done by defining the
|
|
|
|
``_LIBCPP_AVAILABILITY_CUSTOM_VERBOSE_ABORT_PROVIDED`` macro in all translation units of your program
|
|
|
|
and defining the following function in exactly one translation unit:
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2022-07-25 13:43:47 -04:00
|
|
|
void __libcpp_verbose_abort(char const* format, ...)
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
|
|
|
|
This mechanism is similar to how one can replace the default definition of ``operator new``
|
|
|
|
and ``operator delete``. For example:
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
// In HelloWorldHandler.cpp
|
2022-07-25 13:43:47 -04:00
|
|
|
#include <version> // must include any libc++ header before defining the function (C compatibility headers excluded)
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
|
2022-07-25 13:43:47 -04:00
|
|
|
void std::__libcpp_verbose_abort(char const* format, ...) {
|
2022-07-25 13:19:51 -04:00
|
|
|
va_list list;
|
|
|
|
va_start(list, format);
|
|
|
|
std::vfprintf(stderr, format, list);
|
|
|
|
va_end(list);
|
|
|
|
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// In HelloWorld.cpp
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::vector<int> v;
|
2022-07-25 13:43:47 -04:00
|
|
|
int& x = v[0]; // Your termination function will be called here if _LIBCPP_ENABLE_ASSERTIONS=1
|
[libc++] Add a lightweight overridable assertion handler
This patch adds a lightweight assertion handler mechanism that can be
overriden at link-time in a fashion similar to `operator new`.
This is a third take on https://llvm.org/D121123 (which allowed customizing
the assertion handler at compile-time), and https://llvm.org/D119969
(which allowed customizing the assertion handler at runtime only).
This approach is, I think, the best of all three explored approaches.
Indeed, replacing the assertion handler in user code is ergonomic,
yet we retain the ability to provide a custom assertion handler when
deploying to older platforms that don't have a default handler in
the dylib.
As-is, this patch provides a pretty good amount of backwards compatibility
with the previous debug mode:
- Code that used to set _LIBCPP_DEBUG=0 in order to get basic assertions
in their code will still get basic assertions out of the box, but
those assertions will be using the new assertion handler support.
- Code that was previously compiled with references to __libcpp_debug_function
and friends will work out-of-the-box, no changes required. This is
because we provide the same symbols in the dylib as we used to.
- Code that used to set a custom __libcpp_debug_function will stop
compiling, because we don't provide that declaration anymore. Users
will have to migrate to the new way of setting a custom assertion
handler, which is extremely easy. I suspect that pool of users is
very limited, so breaking them at compile-time is probably acceptable.
The main downside of this approach is that code being compiled with
assertions enabled but deploying to an older platform where the assertion
handler didn't exist yet will fail to compile. However users can easily
fix the problem by providing a custom assertion handler and defining
the _LIBCPP_AVAILABILITY_CUSTOM_ASSERTION_HANDLER_PROVIDED macro to
let the library know about the custom handler. In a way, this is
actually a feature because it avoids a load-time error that one would
otherwise get when trying to run the code on the older target.
Differential Revision: https://reviews.llvm.org/D121478
2022-03-03 17:37:03 -05:00
|
|
|
}
|
|
|
|
|
2022-07-25 13:43:47 -04:00
|
|
|
Also note that the verbose termination function should never return. Since assertions in libc++
|
|
|
|
catch undefined behavior, your code will proceed with undefined behavior if your function is called
|
|
|
|
and does return.
|
|
|
|
|
|
|
|
Furthermore, exceptions should not be thrown from the function. Indeed, many functions in the
|
|
|
|
library are ``noexcept``, and any exception thrown from the termination function will result
|
|
|
|
in ``std::terminate`` being called.
|
|
|
|
|
2016-11-13 23:00:30 +00:00
|
|
|
Libc++ Configuration Macros
|
|
|
|
===========================
|
|
|
|
|
|
|
|
Libc++ provides a number of configuration macros which can be used to enable
|
|
|
|
or disable extended libc++ behavior, including enabling "debug mode" or
|
|
|
|
thread safety annotations.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
|
|
|
|
This macro is used to enable -Wthread-safety annotations on libc++'s
|
2021-04-17 20:34:06 +05:30
|
|
|
``std::mutex`` and ``std::lock_guard``. By default, these annotations are
|
2016-11-13 23:00:30 +00:00
|
|
|
disabled and must be manually enabled by the user.
|
2016-12-05 19:40:12 +00:00
|
|
|
|
|
|
|
**_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**:
|
|
|
|
This macro is used to disable all visibility annotations inside libc++.
|
|
|
|
Defining this macro and then building libc++ with hidden visibility gives a
|
|
|
|
build of libc++ which does not export any symbols, which can be useful when
|
|
|
|
building statically for inclusion into another library.
|
2016-12-08 23:57:08 +00:00
|
|
|
|
2017-01-13 22:02:08 +00:00
|
|
|
**_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**:
|
|
|
|
This macro disables the additional diagnostics generated by libc++ using the
|
|
|
|
`diagnose_if` attribute. These additional diagnostics include checks for:
|
|
|
|
|
2018-12-06 21:46:17 +00:00
|
|
|
* Giving `set`, `map`, `multiset`, `multimap` and their `unordered_`
|
|
|
|
counterparts a comparator which is not const callable.
|
|
|
|
* Giving an unordered associative container a hasher that is not const
|
|
|
|
callable.
|
2017-01-13 22:02:08 +00:00
|
|
|
|
2017-10-09 19:25:17 +00:00
|
|
|
**_LIBCPP_NO_VCRUNTIME**:
|
|
|
|
Microsoft's C and C++ headers are fairly entangled, and some of their C++
|
|
|
|
headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled
|
|
|
|
in from a lot of other headers and provides definitions which clash with
|
|
|
|
libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so
|
|
|
|
there's no way for libc++ to provide a compatible definition, since you can't
|
|
|
|
have multiple definitions).
|
|
|
|
|
|
|
|
By default, libc++ solves this problem by deferring to Microsoft's vcruntime
|
|
|
|
headers where needed. However, it may be undesirable to depend on vcruntime
|
|
|
|
headers, since they may not always be available in cross-compilation setups,
|
|
|
|
or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro
|
|
|
|
prevents libc++ from depending on vcruntime headers. Consequently, it also
|
|
|
|
prevents libc++ headers from being interoperable with vcruntime headers (from
|
|
|
|
the aforementioned clashes), so users of this macro are promising to not
|
|
|
|
attempt to combine libc++ headers with the problematic vcruntime headers. This
|
|
|
|
macro also currently prevents certain `operator new`/`operator delete`
|
|
|
|
replacement scenarios from working, e.g. replacing `operator new` and
|
|
|
|
expecting a non-replaced `operator new[]` to call the replaced `operator new`.
|
|
|
|
|
2022-09-01 12:02:58 +02:00
|
|
|
**_LIBCPP_DISABLE_NODISCARD_EXT**:
|
|
|
|
This macro disables library-extensions of ``[[nodiscard]]``.
|
2022-08-19 15:41:56 +02:00
|
|
|
See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for more information.
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
|
2019-03-12 20:10:06 +00:00
|
|
|
**_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
|
|
|
|
This macro disables warnings when using deprecated components. For example,
|
|
|
|
using `std::auto_ptr` when compiling in C++11 mode will normally trigger a
|
|
|
|
warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
|
|
|
|
no warning will be emitted. By default, this macro is not defined.
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
|
2017-02-17 03:25:08 +00:00
|
|
|
C++17 Specific Configuration Macros
|
|
|
|
-----------------------------------
|
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:
|
|
|
|
This macro is used to re-enable all the features removed in C++17. The effect
|
|
|
|
is equivalent to manually defining each macro listed below.
|
|
|
|
|
2017-02-17 03:30:25 +00:00
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
|
2021-05-24 18:36:17 -04:00
|
|
|
This macro is used to re-enable `auto_ptr`.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**:
|
|
|
|
This macro is used to re-enable the `binder1st`, `binder2nd`,
|
|
|
|
`pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`,
|
|
|
|
`mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`,
|
|
|
|
`const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t`
|
|
|
|
class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`,
|
|
|
|
and `ptr_fun` functions.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**:
|
|
|
|
This macro is used to re-enable the `random_shuffle` algorithm.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
|
|
|
|
This macro is used to re-enable `set_unexpected`, `get_unexpected`, and
|
|
|
|
`unexpected`.
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
|
2022-07-07 19:07:03 +02:00
|
|
|
C++20 Specific Configuration Macros
|
|
|
|
-----------------------------------
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
**_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**:
|
|
|
|
This macro can be used to disable diagnostics emitted from functions marked
|
|
|
|
``[[nodiscard]]`` in dialects after C++17. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
|
|
|
|
for more information.
|
|
|
|
|
2021-05-24 18:36:17 -04:00
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**:
|
|
|
|
This macro is used to re-enable all the features removed in C++20. The effect
|
|
|
|
is equivalent to manually defining each macro listed below.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**:
|
|
|
|
This macro is used to re-enable redundant members of `allocator<T>`,
|
|
|
|
including `pointer`, `reference`, `rebind`, `address`, `max_size`,
|
|
|
|
`construct`, `destroy`, and the two-argument overload of `allocate`.
|
|
|
|
|
2022-06-15 10:55:55 +02:00
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION**:
|
|
|
|
This macro is used to re-enable the library-provided specializations of
|
|
|
|
`allocator<void>` and `allocator<const void>`.
|
|
|
|
Use it in conjunction with `_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS`
|
|
|
|
to ensure that removed members of `allocator<void>` can be accessed.
|
|
|
|
|
2021-05-25 14:34:18 -04:00
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**:
|
|
|
|
This macro is used to re-enable the `argument_type`, `result_type`,
|
|
|
|
`first_argument_type`, and `second_argument_type` members of class
|
|
|
|
templates such as `plus`, `logical_not`, `hash`, and `owner_less`.
|
|
|
|
|
2021-05-24 18:36:17 -04:00
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**:
|
|
|
|
This macro is used to re-enable `not1`, `not2`, `unary_negate`,
|
|
|
|
and `binary_negate`.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**:
|
|
|
|
This macro is used to re-enable `raw_storage_iterator`.
|
|
|
|
|
2021-07-02 17:08:36 +00:00
|
|
|
**_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS**:
|
2021-07-06 10:39:01 -04:00
|
|
|
This macro is used to re-enable `is_literal_type`, `is_literal_type_v`,
|
2021-07-02 17:08:36 +00:00
|
|
|
`result_of` and `result_of_t`.
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
|
2021-07-06 10:39:01 -04:00
|
|
|
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
Libc++ Extensions
|
|
|
|
=================
|
|
|
|
|
|
|
|
This section documents various extensions provided by libc++, how they're
|
|
|
|
provided, and any information regarding how to use them.
|
|
|
|
|
|
|
|
.. _nodiscard extension:
|
|
|
|
|
|
|
|
Extended applications of ``[[nodiscard]]``
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
The ``[[nodiscard]]`` attribute is intended to help users find bugs where
|
|
|
|
function return values are ignored when they shouldn't be. After C++17 the
|
|
|
|
C++ standard has started to declared such library functions as ``[[nodiscard]]``.
|
|
|
|
However, this application is limited and applies only to dialects after C++17.
|
|
|
|
Users who want help diagnosing misuses of STL functions may desire a more
|
|
|
|
liberal application of ``[[nodiscard]]``.
|
|
|
|
|
|
|
|
For this reason libc++ provides an extension that does just that! The
|
2022-09-01 12:02:58 +02:00
|
|
|
extension is enabled by default and can be disabled by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
|
|
|
|
The extended applications of ``[[nodiscard]]`` takes two forms:
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
|
|
|
|
1. Backporting ``[[nodiscard]]`` to entities declared as such by the
|
|
|
|
standard in newer dialects, but not in the present one.
|
|
|
|
|
2021-04-05 14:56:03 -04:00
|
|
|
2. Extended applications of ``[[nodiscard]]``, at the library's discretion,
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
applied to entities never declared as such by the standard.
|
|
|
|
|
|
|
|
Entities declared with ``_LIBCPP_NODISCARD_EXT``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
This section lists all extended applications of ``[[nodiscard]]`` to entities
|
|
|
|
which no dialect declares as such (See the second form described above).
|
|
|
|
|
libcxx: Add _LIBCPP_NODISCARD_EXT to 38 more functions
This builds on the work done in r342808 and adds _LIBCPP_NODISCARD_EXT
to 37 more functions, namely:
adjacent_find, all_of, any_of, binary_search, clamp, count_if, count,
equal_range, equal, find_end, find_first_not_of, find_first_of, find_if,
find, includes, is_heap_until, is_heap, is_partitioned, is_permutation,
is_sorted_until, is_sorted, lexicographical_compare, lower_bound,
max_element, max, min_element, min, minmax_element, minmax, mismatch,
none_of, remove_if, remove, search_n, search, unique, upper_bound
The motivation here is that we noticed that find_if is nodiscard with
Visual Studio's standard library, and we deemed that useful
(https://crbug.com/948122).
https://devblogs.microsoft.com/cppblog/c17-progress-in-vs-2017-15-5-and-15-6/
says "Our criteria for emitting the warning are: discarding the return
value is a guaranteed leak [...], discarding the return value is
near-guaranteed to be incorrect (e.g. remove()/remove_if()/unique()), or
the function is essentially a pure observer (e.g. vector::empty() and
std::is_sorted())." so I went through algorithm and tried to apply these
criteria.
Some of these, like vector::empty() are already nodiscard per C++
standard and didn't need changing.
I didn't (yet?) go over std::string::find* methods which should probably
have _LIBCPP_NODISCARD_EXT too (but not as part of this change).
Differential Revision: https://reviews.llvm.org/D60145
llvm-svn: 357619
2019-04-03 18:13:08 +00:00
|
|
|
* ``adjacent_find``
|
|
|
|
* ``all_of``
|
|
|
|
* ``any_of``
|
|
|
|
* ``binary_search``
|
|
|
|
* ``clamp``
|
|
|
|
* ``count_if``
|
|
|
|
* ``count``
|
|
|
|
* ``equal_range``
|
|
|
|
* ``equal``
|
|
|
|
* ``find_end``
|
|
|
|
* ``find_first_of``
|
|
|
|
* ``find_if_not``
|
|
|
|
* ``find_if``
|
|
|
|
* ``find``
|
[libc++] Add _LIBCPP_ENABLE_NODISCARD and _LIBCPP_NODISCARD_EXT to allow pre-C++2a [[nodiscard]]
Summary:
The `[[nodiscard]]` attribute is intended to help users find bugs where
function return values are ignored when they shouldn't be. After C++17 the
C++ standard has started to declared such library functions as `[[nodiscard]]`.
However, this application is limited and applies only to dialects after C++17.
Users who want help diagnosing misuses of STL functions may desire a more
liberal application of `[[nodiscard]]`.
For this reason libc++ provides an extension that does just that! The
extension must be enabled by defining `_LIBCPP_ENABLE_NODISCARD`. The extended
applications of `[[nodiscard]]` takes two forms:
1. Backporting `[[nodiscard]]` to entities declared as such by the
standard in newer dialects, but not in the present one.
2. Extended applications of `[[nodiscard]]`, at the libraries discretion,
applied to entities never declared as such by the standard.
Users may also opt-out of additional applications `[[nodiscard]]` using
additional macros.
Applications of the first form, which backport `[[nodiscard]]` from a newer
dialect may be disabled using macros specific to the dialect it was added. For
example `_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17`.
Applications of the second form, which are pure extensions, may be disabled
by defining `_LIBCPP_DISABLE_NODISCARD_EXT`.
This patch was originally written by me (Roman Lebedev),
then but then reworked by Eric Fiselier.
Reviewers: mclow.lists, thakis, EricWF
Reviewed By: thakis, EricWF
Subscribers: llvm-commits, mclow.lists, lebedev.ri, EricWF, rjmccall, Quuxplusone, cfe-commits, christof
Differential Revision: https://reviews.llvm.org/D45179
llvm-svn: 342808
2018-09-22 17:54:48 +00:00
|
|
|
* ``get_temporary_buffer``
|
libcxx: Add _LIBCPP_NODISCARD_EXT to 38 more functions
This builds on the work done in r342808 and adds _LIBCPP_NODISCARD_EXT
to 37 more functions, namely:
adjacent_find, all_of, any_of, binary_search, clamp, count_if, count,
equal_range, equal, find_end, find_first_not_of, find_first_of, find_if,
find, includes, is_heap_until, is_heap, is_partitioned, is_permutation,
is_sorted_until, is_sorted, lexicographical_compare, lower_bound,
max_element, max, min_element, min, minmax_element, minmax, mismatch,
none_of, remove_if, remove, search_n, search, unique, upper_bound
The motivation here is that we noticed that find_if is nodiscard with
Visual Studio's standard library, and we deemed that useful
(https://crbug.com/948122).
https://devblogs.microsoft.com/cppblog/c17-progress-in-vs-2017-15-5-and-15-6/
says "Our criteria for emitting the warning are: discarding the return
value is a guaranteed leak [...], discarding the return value is
near-guaranteed to be incorrect (e.g. remove()/remove_if()/unique()), or
the function is essentially a pure observer (e.g. vector::empty() and
std::is_sorted())." so I went through algorithm and tried to apply these
criteria.
Some of these, like vector::empty() are already nodiscard per C++
standard and didn't need changing.
I didn't (yet?) go over std::string::find* methods which should probably
have _LIBCPP_NODISCARD_EXT too (but not as part of this change).
Differential Revision: https://reviews.llvm.org/D60145
llvm-svn: 357619
2019-04-03 18:13:08 +00:00
|
|
|
* ``includes``
|
|
|
|
* ``is_heap_until``
|
|
|
|
* ``is_heap``
|
|
|
|
* ``is_partitioned``
|
|
|
|
* ``is_permutation``
|
|
|
|
* ``is_sorted_until``
|
|
|
|
* ``is_sorted``
|
|
|
|
* ``lexicographical_compare``
|
|
|
|
* ``lower_bound``
|
|
|
|
* ``max_element``
|
|
|
|
* ``max``
|
|
|
|
* ``min_element``
|
|
|
|
* ``min``
|
|
|
|
* ``minmax_element``
|
|
|
|
* ``minmax``
|
|
|
|
* ``mismatch``
|
|
|
|
* ``none_of``
|
|
|
|
* ``remove_if``
|
|
|
|
* ``remove``
|
|
|
|
* ``search_n``
|
|
|
|
* ``search``
|
|
|
|
* ``unique``
|
|
|
|
* ``upper_bound``
|
2019-08-13 11:12:28 +00:00
|
|
|
* ``lock_guard``'s constructors
|
2021-04-05 14:56:03 -04:00
|
|
|
* ``as_const``
|
2020-05-28 14:28:38 -04:00
|
|
|
* ``bit_cast``
|
2021-04-05 14:56:03 -04:00
|
|
|
* ``forward``
|
|
|
|
* ``move``
|
|
|
|
* ``move_if_noexcept``
|
|
|
|
* ``identity::operator()``
|
|
|
|
* ``to_integer``
|
|
|
|
* ``to_underlying``
|
[libc++] Support int8_t and uint8_t in integer distributions as an extension
In D125283, we ensured that integer distributions would not compile when
used with arbitrary unsupported types. This effectively enforced what
the Standard mentions here: http://eel.is/c++draft/rand#req.genl-1.5.
However, this also had the effect of breaking some users that were
using integer distributions with unsupported types like int8_t. Since we
already support using __int128_t in those distributions, it is reasonable
to also support smaller types like int8_t and its unsigned variant. This
commit implements that, adds tests and documents the extension. Note that
we voluntarily don't add support for instantiating these distributions
with bool and char, since those are not integer types. However, it is
trivial to replace uses of these random distributions on char using int8_t.
It is also interesting to note that in the process of adding tests
for smaller types, I discovered that our distributions sometimes don't
provide as faithful a distribution when instantiated with smaller types,
so I had to relax a couple of tests. In particular, we do a really bad
job at implementing the negative binomial, geometric and poisson distributions
for small types. I think this all boils down to the algorithm we use in
std::poisson_distribution, however I am running out of time to investigate
that and changing the algorithm would be an ABI break (which might be
reasonable).
As part of this patch, I also added a mitigation for a very likely
integer overflow bug we were hitting in our tests in negative_binomial_distribution.
I also filed http://llvm.org/PR56656 to track fixing the problematic
distributions with int8_t and uint8_t.
Supersedes D125283.
Differential Revision: https://reviews.llvm.org/D126823
2022-06-01 15:25:14 -04:00
|
|
|
|
2022-07-07 20:02:07 +02:00
|
|
|
Extended integral type support
|
|
|
|
------------------------------
|
|
|
|
|
2022-08-10 17:34:45 -04:00
|
|
|
Several platforms support types that are not specified in the Standard, such as
|
|
|
|
the 128-bit integral types ``__int128_t`` and ``__uint128_t``. As an extension,
|
|
|
|
libc++ does a best-effort attempt to support these types like other integral
|
|
|
|
types, by supporting them notably in:
|
2022-07-07 20:02:07 +02:00
|
|
|
|
|
|
|
* ``<bits>``
|
|
|
|
* ``<charconv>``
|
|
|
|
* ``<functional>``
|
|
|
|
* ``<type_traits>``
|
|
|
|
* ``<format>``
|
|
|
|
* ``<random>``
|
2022-07-15 07:42:17 +02:00
|
|
|
|
2022-08-10 17:34:45 -04:00
|
|
|
Additional types supported in random distributions
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The `C++ Standard <http://eel.is/c++draft/rand#req.genl-1.5>`_ mentions that instantiating several random number
|
|
|
|
distributions with types other than ``short``, ``int``, ``long``, ``long long``, and their unsigned versions is
|
|
|
|
undefined. As an extension, libc++ supports instantiating ``binomial_distribution``, ``discrete_distribution``,
|
|
|
|
``geometric_distribution``, ``negative_binomial_distribution``, ``poisson_distribution``, and ``uniform_int_distribution``
|
|
|
|
with ``int8_t``, ``__int128_t`` and their unsigned versions.
|
|
|
|
|
2022-07-15 07:42:17 +02:00
|
|
|
Extensions to ``<format>``
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
The exposition only type ``basic-format-string`` and its typedefs
|
|
|
|
``format-string`` and ``wformat-string`` became ``basic_format_string``,
|
|
|
|
``format_string``, and ``wformat_string`` in C++23. Libc++ makes these types
|
|
|
|
available in C++20 as an extension.
|