EricWF created this revision.
EricWF added reviewers: mclow.lists, chandlerc.
EricWF added a subscriber: cfe-commits.

The title says it all.

I just want to check that we agree on the general direction of this patch. 
Specifically that libc++ should be free to apply `no_discard` where we feel 
appropriate. If we agree then some other candidates are:

- Containers `empty()` and `size()` methods.
- `try_lock()` functions.
- other `release()` functions.

Additional suggestions welcome!

This patch fixes PR30898 (https://llvm.org/bugs/show_bug.cgi?id=30898)


https://reviews.llvm.org/D26596

Files:
  include/__config
  include/memory
  
test/libcxx/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.modifiers/release.fail.cpp
  
test/libcxx/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.modifiers/release.fail.cpp

Index: test/libcxx/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.modifiers/release.fail.cpp
===================================================================
--- /dev/null
+++ test/libcxx/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.modifiers/release.fail.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// unique_ptr
+
+// test release
+
+#include <memory>
+#include <cassert>
+
+int main()
+{
+    std::unique_ptr<int> p(new int(3));
+    int* i = p.get();
+    p.release(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}
+    delete i;
+}
Index: test/libcxx/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.modifiers/release.fail.cpp
===================================================================
--- /dev/null
+++ test/libcxx/utilities/memory/unique.ptr/unique.ptr.runtime/unique.ptr.runtime.modifiers/release.fail.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// unique_ptr
+
+// test release
+
+#include <memory>
+#include <cassert>
+
+int main()
+{
+    std::unique_ptr<int[]> p(new int[3]);
+    int* i = p.get();
+    p.release(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}
+    delete [] i;
+}
Index: include/memory
===================================================================
--- include/memory
+++ include/memory
@@ -2768,7 +2768,7 @@
         _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
         {return __ptr_.first() != nullptr;}
 
-    _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
+    _LIBCPP_NO_DISCARD _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
     {
         pointer __t = __ptr_.first();
         __ptr_.first() = pointer();
@@ -2959,7 +2959,7 @@
         _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
         {return __ptr_.first() != nullptr;}
 
-    _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
+    _LIBCPP_NO_DISCARD _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
     {
         pointer __t = __ptr_.first();
         __ptr_.first() = pointer();
Index: include/__config
===================================================================
--- include/__config
+++ include/__config
@@ -900,6 +900,14 @@
 #define _LIBCPP_SAFE_STATIC
 #endif
 
+#ifndef _LIBCPP_NO_DISCARD
+# if __has_attribute(warn_unused_result) || _GNUC_VER > 408
+#   define _LIBCPP_NO_DISCARD __attribute__((__warn_unused_result__))
+# else
+#   define _LIBCPP_NO_DISCARD
+# endif
+#endif // !defined(_LIBCPP_NO_DISCARD)
+
 #if !__has_builtin(__builtin_addressof) && _GNUC_VER < 700
 #define _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
 #endif
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to