[libcxx-commits] [libcxx] r368664 - [libc++] Use [[nodiscard]] for lock_guard, as an extension

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


Author: ldionne
Date: Tue Aug 13 04:12:28 2019
New Revision: 368664

URL: http://llvm.org/viewvc/llvm-project?rev=368664&view=rev
Log:
[libc++] Use [[nodiscard]] for lock_guard, as an extension

Summary:
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

Reviewers: mclow.lists, EricWF

Subscribers: christof, jkorous, dexonsmith, libcxx-commits, Quuxplusone, lebedev.ri

Tags: #libc

Differential Revision: https://reviews.llvm.org/D65900

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

Modified: libcxx/trunk/docs/UsingLibcxx.rst
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/docs/UsingLibcxx.rst?rev=368664&r1=368663&r2=368664&view=diff
==============================================================================
--- libcxx/trunk/docs/UsingLibcxx.rst (original)
+++ libcxx/trunk/docs/UsingLibcxx.rst Tue Aug 13 04:12:28 2019
@@ -345,3 +345,4 @@ which no dialect declares as such (See t
 * ``search``
 * ``unique``
 * ``upper_bound``
+* ``lock_guard``'s constructors

Modified: libcxx/trunk/include/__mutex_base
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__mutex_base?rev=368664&r1=368663&r2=368664&view=diff
==============================================================================
--- libcxx/trunk/include/__mutex_base (original)
+++ libcxx/trunk/include/__mutex_base Tue Aug 13 04:12:28 2019
@@ -94,10 +94,11 @@ private:
     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

Modified: libcxx/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp?rev=368664&r1=368663&r2=368664&view=diff
==============================================================================
--- libcxx/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp (original)
+++ libcxx/trunk/test/libcxx/diagnostics/nodiscard_extensions.fail.cpp Tue Aug 13 04:12:28 2019
@@ -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

Added: libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp?rev=368664&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/thread/thread.lock/thread.lock.guard/nodiscard.fail.cpp Tue Aug 13 04:12:28 2019
@@ -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;
+}




More information about the libcxx-commits mailing list