[llvm] r284286 - Support: Add LLVM_NODISCARD with C++17's [[nodiscard]] semantics

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 14 15:04:17 PDT 2016


Author: bogner
Date: Fri Oct 14 17:04:17 2016
New Revision: 284286

URL: http://llvm.org/viewvc/llvm-project?rev=284286&view=rev
Log:
Support: Add LLVM_NODISCARD with C++17's [[nodiscard]] semantics

This is essentially a more powerful version of our current
LLVM_ATTRIBUTE_UNUSED_RESULT, in that it can also be applied to types
and generate warnings whenever an object of that type is returned by
value and the value is discarded.

I'll replace uses of LLVM_ATTRIBUTE_UNUSED_RESULT and remove that
macro in follow up commits.

Modified:
    llvm/trunk/include/llvm/Support/Compiler.h

Modified: llvm/trunk/include/llvm/Support/Compiler.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Compiler.h?rev=284286&r1=284285&r2=284286&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Compiler.h (original)
+++ llvm/trunk/include/llvm/Support/Compiler.h Fri Oct 14 17:04:17 2016
@@ -130,6 +130,7 @@
 #define LLVM_ATTRIBUTE_USED
 #endif
 
+/// LLVM_ATTRIBUTE_UNUSED_RESULT - Deprecated. Use LLVM_NODISCARD instead.
 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
 #elif defined(_MSC_VER)
@@ -138,6 +139,19 @@
 #define LLVM_ATTRIBUTE_UNUSED_RESULT
 #endif
 
+/// LLVM_NODISCARD - Warn if a type or return value is discarded.
+#if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
+#define LLVM_NODISCARD [[nodiscard]]
+#elif !__cplusplus
+// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
+// error when __has_cpp_attribute is given a scoped attribute in C mode.
+#define LLVM_NODISCARD
+#elif __has_cpp_attribute(clang::warn_unused_result)
+#define LLVM_NODISCARD [[clang::warn_unused_result]]
+#else
+#define LLVM_NODISCARD
+#endif
+
 // Some compilers warn about unused functions. When a function is sometimes
 // used or not depending on build settings (e.g. a function only called from
 // within "assert"), this attribute can be used to suppress such warnings.




More information about the llvm-commits mailing list