[libcxx-commits] [PATCH] D65900: [libc++] Use [[nodiscard]] for lock_guard, as an extension

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 13 04:12:38 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL368664: [libc++] Use [[nodiscard]] for lock_guard, as an extension (authored by ldionne, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D65900?vs=214649&id=214801#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65900/new/

https://reviews.llvm.org/D65900

Files:
  libcxx/trunk/docs/UsingLibcxx.rst
  libcxx/trunk/include/__mutex_base
  libcxx/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
  libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp


Index: libcxx/trunk/docs/UsingLibcxx.rst
===================================================================
--- libcxx/trunk/docs/UsingLibcxx.rst
+++ libcxx/trunk/docs/UsingLibcxx.rst
@@ -345,3 +345,4 @@
 * ``search``
 * ``unique``
 * ``upper_bound``
+* ``lock_guard``'s constructors
Index: libcxx/trunk/include/__mutex_base
===================================================================
--- libcxx/trunk/include/__mutex_base
+++ libcxx/trunk/include/__mutex_base
@@ -94,10 +94,11 @@
     mutex_type& __m_;
 public:
 
-    _LIBCPP_INLINE_VISIBILITY
+    _LIBCPP_NODISCARD_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_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/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
===================================================================
--- libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
+++ libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// [[nodiscard]] on constructors isn't supported by all compilers
+// 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/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
===================================================================
--- libcxx/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
+++ libcxx/trunk/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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65900.214801.patch
Type: text/x-patch
Size: 3392 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190813/f125d065/attachment.bin>


More information about the libcxx-commits mailing list