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