Support: Add LLVM_NODISCARD with C++17's [[nodiscard]] semantics
authorJustin Bogner <mail@justinbogner.com>
Fri, 14 Oct 2016 22:04:17 +0000 (22:04 +0000)
committerJustin Bogner <mail@justinbogner.com>
Fri, 14 Oct 2016 22:04:17 +0000 (22:04 +0000)
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.

llvm-svn: 284286

llvm/include/llvm/Support/Compiler.h

index 3cf9910..c6e8875 100644 (file)
 #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)
 #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.