2015-08-23 03:40:49 +08:00
|
|
|
============
|
|
|
|
Using libc++
|
|
|
|
============
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:local:
|
|
|
|
|
|
|
|
Getting Started
|
|
|
|
===============
|
|
|
|
|
|
|
|
If you already have libc++ installed you can use it with clang.
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ clang++ -stdlib=libc++ test.cpp
|
|
|
|
$ clang++ -std=c++11 -stdlib=libc++ test.cpp
|
|
|
|
|
[Docs] Modernize references to macOS
Summary:
This updates all places in documentation that refer to "Mac OS X", "OS X", etc.
to instead use the modern name "macOS" when no specific version number is
mentioned.
If a specific version is mentioned, this attempts to use the OS name at the time
of that version:
* Mac OS X for 10.0 - 10.7
* OS X for 10.8 - 10.11
* macOS for 10.12 - present
Reviewers: JDevlieghere
Subscribers: mgorny, christof, arphaman, cfe-commits, lldb-commits, libcxx-commits, llvm-commits
Tags: #clang, #lldb, #libc, #llvm
Differential Revision: https://reviews.llvm.org/D62654
llvm-svn: 362113
2019-05-31 00:46:22 +08:00
|
|
|
On macOS and FreeBSD libc++ is the default standard library
|
2015-08-23 03:40:49 +08:00
|
|
|
and the ``-stdlib=libc++`` is not required.
|
|
|
|
|
|
|
|
.. _alternate libcxx:
|
|
|
|
|
|
|
|
If you want to select an alternate installation of libc++ you
|
|
|
|
can use the following options.
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ clang++ -std=c++11 -stdlib=libc++ -nostdinc++ \
|
|
|
|
-I<libcxx-install-prefix>/include/c++/v1 \
|
|
|
|
-L<libcxx-install-prefix>/lib \
|
|
|
|
-Wl,-rpath,<libcxx-install-prefix>/lib \
|
|
|
|
test.cpp
|
|
|
|
|
|
|
|
The option ``-Wl,-rpath,<libcxx-install-prefix>/lib`` adds a runtime library
|
|
|
|
search path. Meaning that the systems dynamic linker will look for libc++ in
|
|
|
|
``<libcxx-install-prefix>/lib`` whenever the program is run. Alternatively the
|
[Docs] Modernize references to macOS
Summary:
This updates all places in documentation that refer to "Mac OS X", "OS X", etc.
to instead use the modern name "macOS" when no specific version number is
mentioned.
If a specific version is mentioned, this attempts to use the OS name at the time
of that version:
* Mac OS X for 10.0 - 10.7
* OS X for 10.8 - 10.11
* macOS for 10.12 - present
Reviewers: JDevlieghere
Subscribers: mgorny, christof, arphaman, cfe-commits, lldb-commits, libcxx-commits, llvm-commits
Tags: #clang, #lldb, #libc, #llvm
Differential Revision: https://reviews.llvm.org/D62654
llvm-svn: 362113
2019-05-31 00:46:22 +08:00
|
|
|
environment variable ``LD_LIBRARY_PATH`` (``DYLD_LIBRARY_PATH`` on macOS) can
|
2015-08-23 03:40:49 +08:00
|
|
|
be used to change the dynamic linkers search paths after a program is compiled.
|
|
|
|
|
|
|
|
An example of using ``LD_LIBRARY_PATH``:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ clang++ -stdlib=libc++ -nostdinc++ \
|
|
|
|
-I<libcxx-install-prefix>/include/c++/v1
|
|
|
|
-L<libcxx-install-prefix>/lib \
|
|
|
|
test.cpp -o
|
|
|
|
$ ./a.out # Searches for libc++ in the systems library paths.
|
|
|
|
$ export LD_LIBRARY_PATH=<libcxx-install-prefix>/lib
|
|
|
|
$ ./a.out # Searches for libc++ along LD_LIBRARY_PATH
|
|
|
|
|
2019-03-22 00:21:09 +08:00
|
|
|
Using ``<filesystem>``
|
|
|
|
======================
|
|
|
|
|
|
|
|
Prior to LLVM 9.0, libc++ provides the implementation of the filesystem library
|
|
|
|
in a separate static library. Users of ``<filesystem>`` and ``<experimental/filesystem>``
|
|
|
|
are required to link ``-lc++fs``. Prior to libc++ 7.0, users of
|
|
|
|
``<experimental/filesystem>`` were required to link libc++experimental.
|
|
|
|
|
|
|
|
Starting with LLVM 9.0, support for ``<filesystem>`` is provided in the main
|
|
|
|
library and nothing special is required to use ``<filesystem>``.
|
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 11:07:09 +08:00
|
|
|
|
2016-05-04 06:32:08 +08:00
|
|
|
Using libc++experimental and ``<experimental/...>``
|
|
|
|
=====================================================
|
2015-08-23 03:40:49 +08:00
|
|
|
|
2016-05-04 06:32:08 +08:00
|
|
|
Libc++ provides implementations of experimental technical specifications
|
|
|
|
in a separate library, ``libc++experimental.a``. Users of ``<experimental/...>``
|
2016-05-06 12:49:30 +08:00
|
|
|
headers may be required to link ``-lc++experimental``.
|
2016-05-04 06:32:08 +08:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ clang++ -std=c++14 -stdlib=libc++ test.cpp -lc++experimental
|
|
|
|
|
|
|
|
Libc++experimental.a may not always be available, even when libc++ is already
|
|
|
|
installed. For information on building libc++experimental from source see
|
|
|
|
:ref:`Building Libc++ <build instructions>` and
|
|
|
|
:ref:`libc++experimental CMake Options <libc++experimental options>`.
|
|
|
|
|
|
|
|
Also see the `Experimental Library Implementation Status <http://libcxx.llvm.org/ts1z_status.html>`__
|
|
|
|
page.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
Experimental libraries are Experimental.
|
|
|
|
* The contents of the ``<experimental/...>`` headers and ``libc++experimental.a``
|
|
|
|
library will not remain compatible between versions.
|
|
|
|
* No guarantees of API or ABI stability are provided.
|
2019-06-11 22:48:40 +08:00
|
|
|
* When we implement the standardized version of an experimental feature,
|
|
|
|
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-23 03:40:49 +08:00
|
|
|
|
|
|
|
Using libc++ on Linux
|
|
|
|
=====================
|
|
|
|
|
2015-10-16 06:41:51 +08:00
|
|
|
On Linux libc++ can typically be used with only '-stdlib=libc++'. However
|
|
|
|
some libc++ installations require the user manually link libc++abi themselves.
|
|
|
|
If you are running into linker errors when using libc++ try adding '-lc++abi'
|
|
|
|
to the link line. For example:
|
2015-08-23 03:40:49 +08:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
|
|
|
|
|
|
|
|
Alternately, you could just add libc++abi to your libraries list, which in
|
|
|
|
most situations will give the same result:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ clang++ -stdlib=libc++ test.cpp -lc++abi
|
|
|
|
|
|
|
|
|
|
|
|
Using libc++ with GCC
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
GCC does not provide a way to switch from libstdc++ to libc++. You must manually
|
|
|
|
configure the compile and link commands.
|
|
|
|
|
|
|
|
In particular you must tell GCC to remove the libstdc++ include directories
|
|
|
|
using ``-nostdinc++`` and to not link libstdc++.so using ``-nodefaultlibs``.
|
|
|
|
|
|
|
|
Note that ``-nodefaultlibs`` removes all of the standard system libraries and
|
|
|
|
not just libstdc++ so they must be manually linked. For example:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ g++ -nostdinc++ -I<libcxx-install-prefix>/include/c++/v1 \
|
|
|
|
test.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
|
2016-01-20 09:26:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
GDB Pretty printers for libc++
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
GDB does not support pretty-printing of libc++ symbols by default. Unfortunately
|
|
|
|
libc++ does not provide pretty-printers itself. However there are 3rd
|
|
|
|
party implementations available and although they are not officially
|
|
|
|
supported by libc++ they may be useful to users.
|
|
|
|
|
|
|
|
Known 3rd Party Implementations Include:
|
|
|
|
|
|
|
|
* `Koutheir's libc++ pretty-printers <https://github.com/koutheir/libcxx-pretty-printers>`_.
|
2016-11-14 07:00:30 +08: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_DEBUG**:
|
2016-12-28 12:58:52 +08:00
|
|
|
See :ref:`using-debug-mode` for more information.
|
2016-11-14 07:00:30 +08:00
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
|
|
|
|
This macro is used to enable -Wthread-safety annotations on libc++'s
|
|
|
|
``std::mutex`` and ``std::lock_guard``. By default these annotations are
|
|
|
|
disabled and must be manually enabled by the user.
|
2016-12-06 03:40:12 +08: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-09 07:57:08 +08:00
|
|
|
|
2017-04-14 04:13:32 +08:00
|
|
|
**_LIBCPP_DISABLE_EXTERN_TEMPLATE**:
|
|
|
|
This macro is used to disable extern template declarations in the libc++
|
|
|
|
headers. The intended use case is for clients who wish to use the libc++
|
|
|
|
headers without taking a dependency on the libc++ library itself.
|
|
|
|
|
2016-12-09 07:57:08 +08:00
|
|
|
**_LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION**:
|
|
|
|
This macro is used to re-enable an extension in `std::tuple` which allowed
|
|
|
|
it to be implicitly constructed from fewer initializers than contained
|
|
|
|
elements. Elements without an initializer are default constructed. For example:
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
std::tuple<std::string, int, std::error_code> foo() {
|
|
|
|
return {"hello world", 42}; // default constructs error_code
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Since libc++ 4.0 this extension has been disabled by default. This macro
|
|
|
|
may be defined to re-enable it in order to support existing code that depends
|
|
|
|
on the extension. New use of this extension should be discouraged.
|
|
|
|
See `PR 27374 <http://llvm.org/PR27374>`_ for more information.
|
|
|
|
|
|
|
|
Note: The "reduced-arity-initialization" extension is still offered but only
|
|
|
|
for explicit conversions. Example:
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
auto foo() {
|
|
|
|
using Tup = std::tuple<std::string, int, std::error_code>;
|
|
|
|
return Tup{"hello world", 42}; // explicit constructor called. OK.
|
|
|
|
}
|
2016-12-09 20:32:02 +08:00
|
|
|
|
2017-01-14 06:02:08 +08: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-07 05:46:17 +08: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-14 06:02:08 +08:00
|
|
|
|
2017-10-10 03:25:17 +08: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`.
|
|
|
|
|
[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-23 01:54:48 +08:00
|
|
|
**_LIBCPP_ENABLE_NODISCARD**:
|
|
|
|
Allow the library to add ``[[nodiscard]]`` attributes to entities not specified
|
|
|
|
as ``[[nodiscard]]`` by the current language dialect. This includes
|
|
|
|
backporting applications of ``[[nodiscard]]`` from newer dialects and
|
|
|
|
additional extended applications at the discretion of the library. All
|
|
|
|
additional applications of ``[[nodiscard]]`` are disabled by default.
|
|
|
|
See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for
|
|
|
|
more information.
|
|
|
|
|
|
|
|
**_LIBCPP_DISABLE_NODISCARD_EXT**:
|
|
|
|
This macro prevents the library from applying ``[[nodiscard]]`` to entities
|
|
|
|
purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
|
|
|
|
for more information.
|
|
|
|
|
2019-03-13 04:10:06 +08: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-23 01:54:48 +08:00
|
|
|
|
2017-02-17 11:25:08 +08: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.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
|
|
|
|
This macro is used to re-enable the `set_unexpected`, `get_unexpected`, and
|
2017-02-17 11:30:25 +08:00
|
|
|
`unexpected` functions, which were removed in C++17.
|
|
|
|
|
|
|
|
**_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
|
|
|
|
This macro is used to re-enable `std::auto_ptr` in C++17.
|
[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-23 01:54:48 +08:00
|
|
|
|
|
|
|
C++2a Specific Configuration Macros:
|
|
|
|
------------------------------------
|
|
|
|
**_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.
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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``.
|
|
|
|
|
|
|
|
|
|
|
|
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-04 02:13:08 +08: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-23 01:54:48 +08: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-04 02:13:08 +08: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 19:12:28 +08:00
|
|
|
* ``lock_guard``'s constructors
|