[libcxx-commits] [PATCH] D65900: [libc++] Use [[nodiscard]] for lock_guard, as an extension
Eric Fiselier via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Aug 7 23:17:39 PDT 2019
I think we try to keep a list under doc/ about the places we apply this
extension.
On Wed, Aug 7, 2019 at 4:03 PM Louis Dionne via Phabricator <
reviews at reviews.llvm.org> wrote:
> ldionne created this revision.
> ldionne added reviewers: mclow.lists, EricWF.
> Herald added subscribers: libcxx-commits, dexonsmith, jkorous, christof.
> Herald added a project: libc++.
>
> D64914 <https://reviews.llvm.org/D64914> added support for applying
> [[nodiscard]] to constructors. This
> commit uses that capability to flag incorrect uses of std::lock_guard
> where one forgets to actually create a variable for the lock_guard.
>
> rdar://45790820
>
>
> Repository:
> rG LLVM Github Monorepo
>
> https://reviews.llvm.org/D65900
>
> Files:
> libcxx/include/__config
> libcxx/include/__mutex_base
> libcxx/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
>
> libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
>
>
> Index:
> libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
> ===================================================================
> --- /dev/null
> +++
> libcxx/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
> @@ -0,0 +1,32 @@
>
> +//===----------------------------------------------------------------------===//
> +//
> +// Part of the LLVM Project, under the Apache License v2.0 with LLVM
> Exceptions.
> +// See https://llvm.org/LICENSE.txt for license information.
> +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
> +//
>
> +//===----------------------------------------------------------------------===//
> +
> +// UNSUPPORTED: libcpp-has-no-threads
> +// UNSUPPORTED: clang-6, clang-6, clang-8, clang-9
> +// UNSUPPORTED: apple-clang-9, apple-clang-10, apple-clang-11
> +
> +// <mutex>
> +
> +// template <class Mutex> class lock_guard;
> +
> +// [[nodiscard]] explicit lock_guard(mutex_type& m);
> +// [[nodiscard]] lock_guard(mutex_type& m, adopt_lock_t);
> +
> +// Test that we properly apply [[nodiscard]] to lock_guard's constructors,
> +// which is a libc++ extension.
> +
> +// MODULES_DEFINES: _LIBCPP_ENABLE_NODISCARD
> +#define _LIBCPP_ENABLE_NODISCARD
> +#include <mutex>
> +
> +int main(int, char**) {
> + std::mutex m;
> + std::lock_guard<std::mutex>{m}; // expected-error{{ignoring temporary
> created by a constructor declared with 'nodiscard' attribute}}
> + std::lock_guard<std::mutex>{m, std::adopt_lock}; //
> expected-error{{ignoring temporary created by a constructor declared with
> 'nodiscard' attribute}}
> + return 0;
> +}
> Index: libcxx/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
> ===================================================================
> --- libcxx/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
> +++ libcxx/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
> @@ -16,7 +16,7 @@
> // guides from existing ctors, needed by default_searcher() below.
> // UNSUPPORTED: apple-clang-9
>
> -// Test that entities declared [[nodiscard]] as at extension by libc++,
> are
> +// Test that entities declared [[nodiscard]] as an extension by libc++,
> are
> // only actually declared such when _LIBCPP_ENABLE_NODISCARD is specified.
>
> // All entities to which libc++ applies [[nodiscard]] as an extension
> should
> Index: libcxx/include/__mutex_base
> ===================================================================
> --- libcxx/include/__mutex_base
> +++ libcxx/include/__mutex_base
> @@ -94,10 +94,11 @@
> mutex_type& __m_;
> public:
>
> - _LIBCPP_INLINE_VISIBILITY
> + _LIBCPP_NODISCARD_CTOR_EXT _LIBCPP_INLINE_VISIBILITY
> explicit lock_guard(mutex_type& __m)
> _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
> : __m_(__m) {__m_.lock();}
> - _LIBCPP_INLINE_VISIBILITY
> +
> + _LIBCPP_NODISCARD_CTOR_EXT _LIBCPP_INLINE_VISIBILITY
> lock_guard(mutex_type& __m, adopt_lock_t)
> _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
> : __m_(__m) {}
> _LIBCPP_INLINE_VISIBILITY
> Index: libcxx/include/__config
> ===================================================================
> --- libcxx/include/__config
> +++ libcxx/include/__config
> @@ -1010,6 +1010,19 @@
> # define _LIBCPP_NODISCARD_AFTER_CXX17
> #endif
>
> +#if __has_cpp_attribute(nodiscard) >= 201907L
> + // Attribute to use to mark a constructor as [[nodiscard]] when the
> + // Standard specifies the constructor as [[nodiscard]]
> +# define _LIBCPP_NODISCARD_CTOR _LIBCPP_NODISCARD
> +
> + // Attribute to use to mark a constructor as [[nodiscard]] as a
> + // libc++ extension.
> +# define _LIBCPP_NODISCARD_CTOR_EXT _LIBCPP_NODISCARD_EXT
> +#else
> +# define _LIBCPP_NODISCARD_CTOR // compiler doesn't support P1771
> +# define _LIBCPP_NODISCARD_CTOR_EXT // compiler doesn't support P1771
> +#endif
> +
> #if _LIBCPP_STD_VER > 14 && defined(__cpp_inline_variables) &&
> (__cpp_inline_variables >= 201606L)
> # define _LIBCPP_INLINE_VAR inline
> #else
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190808/3991dfbe/attachment.html>
More information about the libcxx-commits
mailing list