2024-08-22 09:16:01 -04:00
.. _user-documentation:
2021-07-06 10:39:01 -04:00
2024-08-22 09:16:01 -04:00
==================
User documentation
==================
2015-08-22 19:40:49 +00:00
.. contents ::
:local:
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++
2024-08-22 09:16:01 -04:00
when it is not the default library used by their toolchain. It is aimed at
users of libc++: a separate page contains documentation aimed at vendors who
build and ship libc++ as part of 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
2024-08-22 09:16:01 -04:00
Note that using `` -std=c++XY `` with a version of the Standard that has not been ratified
yet is considered unstable. While we strive to maintain stability, libc++ may be forced to
make breaking changes to features shipped in a Standard that hasn't been ratified yet. Use
these versions of the Standard at your own risk.
Using libc++ when it is not the system default
==============================================
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.
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++:
.. code-block :: bash
$ clang++ -stdlib=libc++ test.cpp
On systems where libc++ is the library in use by default such as macOS and FreeBSD,
this flag is not required.
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
2024-08-22 09:16:01 -04:00
On compilers that do not support the `` -fexperimental-library `` flag (such as GCC),
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.
2023-09-12 11:02:17 -04:00
The following features are currently considered experimental and are only provided
when `` -fexperimental-library `` is passed:
* The parallel algorithms library (`` <execution> `` and the associated algorithms)
* `` std::chrono::tzdb `` and related time zone functionality
2024-09-12 09:48:59 -04:00
* `` <syncstream> ``
2023-09-12 11:02:17 -04:00
2024-08-22 09:16:01 -04:00
.. note ::
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
2016-11-13 23:00:30 +00:00
Libc++ Configuration Macros
===========================
2024-08-22 09:16:01 -04:00
Libc++ provides a number of configuration macros that can be used by developers to
enable or disable extended libc++ behavior.
.. warning ::
Configuration macros that are not documented here are not intended to be customized
by developers and should not be used. In particular, some configuration macros are
only intended to be used by vendors and changing their value from the one provided
in your toolchain can lead to unexpected behavior.
2016-11-13 23:00:30 +00:00
**_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
2023-11-08 09:10:00 -10:00
**_LIBCPP_HARDENING_MODE** :
This macro is used to choose the :ref: `hardening mode <using-hardening-modes>` .
2023-07-14 16:58:15 -07:00
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-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` .
2025-03-24 18:20:20 +01:00
**_LIBCPP_REMOVE_TRANSITIVE_INCLUDES** :
When this macro is defined, the standard library headers will adhere to a
stricter policy regarding the (transitive) inclusion of other standard library
headers, only guaranteeing to provide those definitions explicitly mandated by
the standard. Please notice that defining this macro might break existing codebases
that implicitly rely on standard headers providing definitions not explicitly
required by the standard.
The primary motivation for this configuration macro is to improve compilation
times. In most standard library implementations, header files include more
definitions than officially required because the implementation details give rise
to internal dependencies. The common practice is to have the standard headers
internally include other standard headers, but this generally results in
increased compilation overhead. This configuration option attempts to mitigate
this problem by avoiding such unnecessary inclusions. Compiling
a codebase with this macro may improve portability by identifying
missing standard header inclusions.
However, be aware that enabling this macro may lead to breakages
when updating to a newer version of the library, since transitive includes
that your code was previously relying on may have been removed.
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
2024-08-22 09:16:01 -04:00
**_LIBCPP_ENABLE_EXPERIMENTAL** :
This macro enables experimental features. This can be used on compilers that do
not support the `` -fexperimental-library `` flag. When used, users also need to
ensure that the appropriate experimental library (usually `` libc++experimental.a `` )
is linked into their program.
2017-02-17 03:25:08 +00:00
C++17 Specific Configuration Macros
-----------------------------------
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.
2024-11-08 11:35:31 +00:00
**_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION** :
This macro is used to re-enable `unary_function` and `binary_function` .
2021-05-24 18:36:17 -04:00
**_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
-----------------------------------
2024-08-28 05:15:03 +08:00
**_LIBCPP_ENABLE_CXX20_REMOVED_UNCAUGHT_EXCEPTION** :
This macro is used to re-enable `uncaught_exception` .
2024-04-05 22:04:07 +03:00
**_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE** :
2023-12-30 14:05:22 +01:00
This macro is used to re-enable the function
`` std::shared_ptr<...>::unique() `` .
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` .
2024-09-16 23:53:05 +08:00
**_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER** :
This macro is used to re-enable `get_temporary_buffer` and `return_temporary_buffer` .
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
2023-11-24 17:34:30 +01:00
C++26 Specific Configuration Macros
-----------------------------------
**_LIBCPP_ENABLE_CXX26_REMOVED_CODECVT** :
This macro is used to re-enable all named declarations in `` <codecvt> `` .
2024-04-05 22:04:07 +03:00
**_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE** :
2023-11-25 13:56:40 +01:00
This macro is used to re-enable the function
`` std::basic_string<...>::reserve() `` .
2023-11-24 17:34:30 +01:00
2024-01-20 06:07:19 +02:00
**_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS** :
2024-04-04 06:03:00 +03:00
This macro is used to re-enable redundant member of `` allocator<T>::is_always_equal `` .
**_LIBCPP_ENABLE_CXX26_REMOVED_STRSTREAM** :
This macro is used to re-enable all named declarations in `` <strstream> `` .
2024-01-20 06:07:19 +02:00
2024-04-05 22:04:07 +03:00
**_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT** :
This macro is used to re-enable the `` wstring_convert `` and `` wbuffer_convert ``
in `` <locale> `` .
[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.
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.
2023-02-21 17:33:56 +01:00
For padding Unicode strings the `` format `` library relies on the Unicode
Standard. Libc++ retroactively updates the Unicode Standard in older C++
versions. This allows the library to have better estimates for newly introduced
Unicode code points, without requiring the user to use the latest C++ version
in their code base.
2023-06-27 18:39:59 +02:00
2022-04-03 17:43:52 +02:00
In C++26 formatting pointers gained a type `` P `` and allows to use
zero-padding. These options have been retroactively applied to C++20.
2023-08-20 12:30:57 +02:00
Extensions to the C++23 modules `` std `` and `` std.compat ``
----------------------------------------------------------
Like other major implementations, libc++ provides C++23 modules `` std `` and
2024-08-22 09:16:01 -04:00
`` std.compat `` in C++20 as an extension.
2023-08-20 12:30:57 +02:00
[libcxx] Allow string to use SSO in constant evaluation. (#66576)
Previously, libcxx forced all strings created during constant evaluation
to point to allocated memory. That was done due to implementation
difficultites, but it turns out not to be necessary. This patch permits
the use of SSO strings during constant evaluation, and also simplifies
the implementation.
This does have a downside in terms of enabling users to accidentally
write non-portable code, however, which I've documented in
UsingLibcxx.rst.
In particular, whether `constinit std::string x = "...";` will
successfully compile now depends on whether the string is smaller than
the SSO capacity -- in libc++, up to 22 bytes on 64-bit platforms, and
up to 10 bytes on 32-bit platforms. By comparison, libstdc++ and MSVC
have an SSO capacity of 15 bytes, except that in libstdc++,
constant-initialized strings cannot be used as function-locals because
the object contains a pointer to itself.
Closes #68434
2023-10-10 14:31:47 -04:00
Constant-initialized std::string
--------------------------------
As an implementation-specific optimization, `` std::basic_string `` (`` std::string `` ,
`` std::wstring `` , etc.) may either store the string data directly in the object, or else store a
pointer to heap-allocated memory, depending on the length of the string.
As of C++20, the constructors are now declared `` constexpr `` , which permits strings to be used
during constant-evaluation time. In libc++, as in other common implementations, it is also possible
to constant-initialize a string object (e.g. via declaring a variable with `` constinit `` or
`` constexpr `` ), but, only if the string is short enough to not require a heap allocation. Reliance
upon this should be discouraged in portable code, as the allowed length differs based on the
standard-library implementation and also based on whether the platform uses 32-bit or 64-bit
pointers.
.. code-block :: cpp
// Non-portable: 11-char string works on 64-bit libc++, but not on 32-bit.
constinit std::string x = "hello world";
// Prefer to use string_view, or remove constinit/constexpr from the variable definition:
constinit std::string_view x = "hello world";
std::string_view y = "hello world";
2023-07-09 17:54:11 +02:00
.. _turning-off-asan:
[ASan][libcxx] A way to turn off annotations for containers with a specific allocator
This revision is part of our efforts to support container annotations with (almost) every allocator.
That patch is necessary to enable support for most annotations (D136765). Without a way to turn off annotations, it's hard to use ASan with area allocators (no calls to destructors).
This is an answer to a request about it. This patch provides a solution to the aforementioned issue by introducing a new template structure `__asan_annotate_container_with_allocator`, which allows the disabling of container annotations for a specific allocator.
This patch also introduces `_LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS` FTM.
To turn off annotations, it is sufficient to create a template specialization with a false value using a [Unary Type Trait](https://en.cppreference.com/w/cpp/types/integral_constant).
The proposed structure is being used in the code enabling annotations for all allocators in `std::vector`, `std::basic_string`, and `std::deque`. (D136765 D146214 D146815)
Possibility to do it was added to ASan API in rGdd1b7b797a116eed588fd752fbe61d34deeb24e4 commit.
For context on not calling a destructor, look at https://eel.is/c++draft/basic.life#5 and notes there, you may also read a discussion in D136765.
Reviewed By: ldionne, philnik, #libc, hans
Spies: EricWF, mikhail.ramalho, #sanitizers, libcxx-commits, hans, vitalybuka
Differential Revision: https://reviews.llvm.org/D145628
2023-05-04 14:16:06 -07:00
Turning off ASan annotation in containers
2023-06-27 18:39:59 +02:00
-----------------------------------------
[ASan][libcxx] A way to turn off annotations for containers with a specific allocator
This revision is part of our efforts to support container annotations with (almost) every allocator.
That patch is necessary to enable support for most annotations (D136765). Without a way to turn off annotations, it's hard to use ASan with area allocators (no calls to destructors).
This is an answer to a request about it. This patch provides a solution to the aforementioned issue by introducing a new template structure `__asan_annotate_container_with_allocator`, which allows the disabling of container annotations for a specific allocator.
This patch also introduces `_LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS` FTM.
To turn off annotations, it is sufficient to create a template specialization with a false value using a [Unary Type Trait](https://en.cppreference.com/w/cpp/types/integral_constant).
The proposed structure is being used in the code enabling annotations for all allocators in `std::vector`, `std::basic_string`, and `std::deque`. (D136765 D146214 D146815)
Possibility to do it was added to ASan API in rGdd1b7b797a116eed588fd752fbe61d34deeb24e4 commit.
For context on not calling a destructor, look at https://eel.is/c++draft/basic.life#5 and notes there, you may also read a discussion in D136765.
Reviewed By: ldionne, philnik, #libc, hans
Spies: EricWF, mikhail.ramalho, #sanitizers, libcxx-commits, hans, vitalybuka
Differential Revision: https://reviews.llvm.org/D145628
2023-05-04 14:16:06 -07:00
`` __asan_annotate_container_with_allocator `` is a customization point to allow users to disable
`Address Sanitizer annotations for containers <https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow> `_ for specific allocators. This may be necessary for allocators that access allocated memory.
This customization point exists only when `` _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS `` Feature Test Macro is defined.
For allocators not running destructors, it is also possible to `bulk-unpoison memory <https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning> `_ instead of disabling annotations altogether.
The struct may be specialized for user-defined allocators. It is a `Cpp17UnaryTypeTrait <http://eel.is/c++draft/type.traits#meta.rqmts> `_ with a base characteristic of `` true_type `` if the container is allowed to use annotations and `` false_type `` otherwise.
The annotations for a `` user_allocator `` can be disabled like this:
.. code-block :: cpp
#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS
template <class T>
struct std::__asan_annotate_container_with_allocator<user_allocator<T>> : std::false_type {};
#endif
Why may I want to turn it off?
2023-06-27 18:39:59 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ASan][libcxx] A way to turn off annotations for containers with a specific allocator
This revision is part of our efforts to support container annotations with (almost) every allocator.
That patch is necessary to enable support for most annotations (D136765). Without a way to turn off annotations, it's hard to use ASan with area allocators (no calls to destructors).
This is an answer to a request about it. This patch provides a solution to the aforementioned issue by introducing a new template structure `__asan_annotate_container_with_allocator`, which allows the disabling of container annotations for a specific allocator.
This patch also introduces `_LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS` FTM.
To turn off annotations, it is sufficient to create a template specialization with a false value using a [Unary Type Trait](https://en.cppreference.com/w/cpp/types/integral_constant).
The proposed structure is being used in the code enabling annotations for all allocators in `std::vector`, `std::basic_string`, and `std::deque`. (D136765 D146214 D146815)
Possibility to do it was added to ASan API in rGdd1b7b797a116eed588fd752fbe61d34deeb24e4 commit.
For context on not calling a destructor, look at https://eel.is/c++draft/basic.life#5 and notes there, you may also read a discussion in D136765.
Reviewed By: ldionne, philnik, #libc, hans
Spies: EricWF, mikhail.ramalho, #sanitizers, libcxx-commits, hans, vitalybuka
Differential Revision: https://reviews.llvm.org/D145628
2023-05-04 14:16:06 -07:00
There are a few reasons why you may want to turn off annotations for an allocator.
Unpoisoning may not be an option, if (for example) you are not maintaining the allocator.
* You are using allocator, which does not call destructor during deallocation.
* You are aware that memory allocated with an allocator may be accessed, even when unused by container.
[libcxx] Fix using std::wcout/wcin on Windows with streams configured in wide mode
On Windows, the underlying file descriptors for stdout/stdin/stderr
can be reconfigured to wide mode. In the default (narrow) mode, the
charset usually isn't utf8 (as libcxx assumes), but normally a locale
specific codepage (where each codepage only can represent a small
subset of unicode characters).
By configuring the stdout file descriptor to wide mode, the user can
output wchar_t based strings without convesion to the narrow charset.
Within libcxx, don't try to use codecvt to convert this to a narrow
character encoding, but output these strings as such with fputwc.
In wide mode, such strings could be output directly with fwrite too,
but if the file descriptor hasn't been configured in wide mode, that
breaks the output (which currently works reasonably). By always
outputting one character at a time with fputwc, it works regardless
of mode of the stdout file descriptor.
For the narrow output stream, std::cout, outputting (via fwrite)
does fail when the file descriptor is set to wide mode. This matches
how it behaves with both MS STL and GNU libstdc++ too, so this is
probably acceptable.
This fixes https://github.com/llvm/llvm-project/issues/46646, and
the downstream bugs https://github.com/mstorsjo/llvm-mingw/issues/145
and https://github.com/mstorsjo/llvm-mingw/issues/222.
Differential Revision: https://reviews.llvm.org/D146398
2023-03-15 12:11:28 +02:00
2024-10-07 17:27:29 -04:00
Support for compiler extensions
-------------------------------
Clang, GCC and other compilers all provide their own set of language extensions. These extensions
have often been developed without particular consideration for their interaction with the library,
and as such, libc++ does not go out of its way to support them. The library may support specific
compiler extensions which would then be documented explicitly, but the basic expectation should be
that no special support is provided for arbitrary compiler extensions.
[libcxx] Fix using std::wcout/wcin on Windows with streams configured in wide mode
On Windows, the underlying file descriptors for stdout/stdin/stderr
can be reconfigured to wide mode. In the default (narrow) mode, the
charset usually isn't utf8 (as libcxx assumes), but normally a locale
specific codepage (where each codepage only can represent a small
subset of unicode characters).
By configuring the stdout file descriptor to wide mode, the user can
output wchar_t based strings without convesion to the narrow charset.
Within libcxx, don't try to use codecvt to convert this to a narrow
character encoding, but output these strings as such with fputwc.
In wide mode, such strings could be output directly with fwrite too,
but if the file descriptor hasn't been configured in wide mode, that
breaks the output (which currently works reasonably). By always
outputting one character at a time with fputwc, it works regardless
of mode of the stdout file descriptor.
For the narrow output stream, std::cout, outputting (via fwrite)
does fail when the file descriptor is set to wide mode. This matches
how it behaves with both MS STL and GNU libstdc++ too, so this is
probably acceptable.
This fixes https://github.com/llvm/llvm-project/issues/46646, and
the downstream bugs https://github.com/mstorsjo/llvm-mingw/issues/145
and https://github.com/mstorsjo/llvm-mingw/issues/222.
Differential Revision: https://reviews.llvm.org/D146398
2023-03-15 12:11:28 +02:00
Platform specific behavior
==========================
Windows
-------
The `` stdout `` , `` stderr `` , and `` stdin `` file streams can be placed in
Unicode mode by a suitable call to `` _setmode() `` . When in this mode,
the sequence of bytes read from, or written to, these streams is interpreted
as a sequence of little-endian `` wchar_t `` elements. Thus, use of
`` std::cout `` , `` std::cerr `` , or `` std::cin `` with streams in Unicode mode
will not behave as they usually do since bytes read or written won't be
interpreted as individual `` char `` elements. However, `` std::wcout `` ,
`` std::wcerr `` , and `` std::wcin `` will behave as expected.
Wide character stream such as `` std::wcin `` or `` std::wcout `` imbued with a
locale behave differently than they otherwise do. By default, wide character
streams don't convert wide characters but input/output them as is. If a
specific locale is imbued, the IO with the underlying stream happens with
regular `` char `` elements, which are converted to/from wide characters
according to the locale. Note that this doesn't behave as expected if the
stream has been set in Unicode mode.
2024-08-22 09:16:01 -04:00
Third-party Integrations
========================
Libc++ provides integration with a few third-party tools.
[lldb][libc++] Hide all libc++ implementation details from stacktraces (#108870)
This commit changes the libc++ frame recognizer to hide implementation
details of libc++ more aggressively. The applied heuristic is rather
straightforward: We consider every function name starting with `__` as
an implementation detail.
This works pretty neatly for `std::invoke`, `std::function`,
`std::sort`, `std::map::emplace` and many others. Also, this should
align quite nicely with libc++'s general coding convention of using the
`__` for their implementation details, thereby keeping the future
maintenance effort low.
However, this heuristic by itself does not work in 100% of the cases:
E.g., `std::ranges::sort` is not a function, but an object with an
overloaded `operator()`, which means that there is no actual call
`std::ranges::sort` in the call stack. Instead, there is a
`std::ranges::__sort::operator()` call. To make sure that we don't hide
this stack frame, we never hide the frame which represents the entry
point from user code into libc++ code
2024-10-10 19:27:27 +02:00
Debugging libc++ internals in LLDB
----------------------------------
LLDB hides the implementation details of libc++ by default.
E.g., when setting a breakpoint in a comparator passed to `` std::sort `` , the
backtrace will read as
.. code-block ::
(lldb) thread backtrace
* thread #1, name = 'a.out', stop reason = breakpoint 3.1
* frame #0: 0x000055555555520e a.out`my_comparator(a=1, b=8) at test-std-sort.cpp:6:3
frame #7: 0x0000555555555615 a.out`void std::__1::sort[abi:ne200000]<std::__1::__wrap_iter<int*>, bool (*)(int, int)>(__first=(item = 8), __last=(item = 0), __comp=(a.out` my_less(int, int) at test-std-sort.cpp:5)) at sort.h:1003:3
frame #8: 0x000055555555531a a.out`main at test-std-sort.cpp:24:3
Note how the caller of `` my_comparator `` is shown as `` std::sort `` . Looking at
the frame numbers, we can see that frames #1 until #6 were hidden. Those frames
represent internal implementation details such as `` __sort4 `` and similar
utility functions.
To also show those implementation details, use `` thread backtrace -u `` .
Alternatively, to disable those compact backtraces, use `` frame recognizer list ``
and `` frame recognizer disable `` on the "libc++ frame recognizer".
Futhermore, stepping into libc++ functions is disabled by default. This is controlled via the
setting `` target.process.thread.step-avoid-regexp `` which defaults to `` ^std:: `` and can be
disabled using `` settings set target.process.thread.step-avoid-regexp "" `` .
2024-08-22 09:16:01 -04:00
GDB Pretty printers for libc++
------------------------------
GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does
provide pretty-printers itself. Those can be used as:
.. code-block :: bash
$ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \
-ex "python register_libcxx_printer_loader()" \
<args>
.. _include-what-you-use:
include-what-you-use (IWYU)
---------------------------
libc++ provides an IWYU `mapping file <https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUMappings.md> `_ ,
which drastically improves the accuracy of the tool when using libc++. To use the mapping file with
IWYU, you should run the tool like so:
.. code-block :: bash
$ include-what-you-use -Xiwyu --mapping_file=/path/to/libcxx/include/libcxx.imp file.cpp
If you would prefer to not use that flag, then you can replace `` /path/to/include-what-you-use/share/libcxx.imp ``
file with the libc++-provided `` libcxx.imp `` file.