[llvm] [llvm] Add LLVM_CTOR_NODISCARD (PR #65418)

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 5 15:30:28 PDT 2023


https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/65418:

[[nodiscard]] on constructors is a defect report against C++17. That means that it should be applied retroactively, though older compilers might not know about it and emit warnings. This adds back-compatibility macro.

>From c81cab4dcc7713976b5c2c3f1d358fdb86f3f9c7 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Tue, 5 Sep 2023 15:28:07 -0700
Subject: [PATCH] [llvm] Add LLVM_CTOR_NODISCARD

[[nodiscard]] on constructors is a defect report against C++17. That
means that it should be applied retroactively, though older compilers
might not know about it and emit warnings. This adds back-compatibility
macro.
---
 llvm/include/llvm/MC/MCInstPrinter.h |  5 +++--
 llvm/include/llvm/Support/Compiler.h | 10 ++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/MC/MCInstPrinter.h b/llvm/include/llvm/MC/MCInstPrinter.h
index 8c628256eecf404..d51379c9305cc51 100644
--- a/llvm/include/llvm/MC/MCInstPrinter.h
+++ b/llvm/include/llvm/MC/MCInstPrinter.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_MC_MCINSTPRINTER_H
 #define LLVM_MC_MCINSTPRINTER_H
 
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/Format.h"
 #include <cstdint>
 
@@ -97,8 +98,8 @@ class MCInstPrinter {
 
   class WithMarkup {
   public:
-    WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
-                             bool EnableColor);
+    LLVM_CTOR_NODISCARD WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
+                                   bool EnableColor);
     ~WithMarkup();
 
     template <typename T> WithMarkup &operator<<(T &O) {
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 10d5cec231a523c..8eb29ffdeee3492 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -307,6 +307,16 @@
 #define LLVM_GSL_POINTER
 #endif
 
+#if LLVM_HAS_CPP_ATTRIBUTE(gnu::visibility)
+#if __has_cpp_attribute(nodiscard) >= 201907L
+#define LLVM_CTOR_NODISCARD [[nodiscard]]
+#else
+#define LLVM_CTOR_NODISCARD
+#endif
+#else
+#define LLVM_CTOR_NODISCARD
+#endif
+
 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
 /// pedantic diagnostics.
 #ifdef __GNUC__



More information about the llvm-commits mailing list